Hello,
I'm trying to build a template with some formfield-elements.
I need a combobox with more than 25 items, so I use the formfield-
workaround.
I have a formfield which calls a macro "NaarCombo"
-------------------------------
Sub NaarCombo()
Load frmCombo
frmCombo.Show
Application.ScreenUpdating = True
Documents("organisatieonderdelen").Close
'SaveChanges:=wdDoNotSaveChanges
End Sub
-------------------------------
Then in the initialize I fill the combobox. This part works fine.
However, when I try to close the sourcedocument, I get error 4198.
I think this error occurs because the file is still somehow in use.
But I can't see why.
I've tried to change the location of the code for closing the
document. fx: I placed it in sub "NaarCombo". However, this didn't
work. I still got the same error.
I've also tried copying it to "document_close"
This works, but if there are no other documents open, it also closes
Word....
Another option would be to use an Exceldocument as source, but I think
that will be 'slower'.
Can anyone give me some tips in this?
The code in Userform_intialize::::
----------------------------------------
Private Sub UserForm_Initialize()
cmbOrgOnd.ColumnCount = 1
'Load data into ComboBox
' Deze makro laadt de gegevens uit het bestand
organisatieonderdelen.doc in een keuzelijst van het document.
Dim i As Integer, j As Integer, myitem As Range, m As Long, n As Long
'dan de gegevens vanuit de array in de dropdownbox laden.
'dropdownbox moet altijd de naam "OrgOnd" hebben.
strTargetname = ActiveDocument.Name
Application.ScreenUpdating = False
' Open het bestand met de organisatieonderdelen
Set sourcedoc = Documents.Open(FileName:="G:\SjablonenStart-Dir
\databases\organisatieonderdelen.doc")
' Het aantal rijen ophalen
i = sourcedoc.Tables(1).Rows.Count
' Definieer de array waar de organisatieonderdelen in worden
opgeslagen
Dim MyArray() As Variant
'Laad gegevens uit de tabel in de Array
ReDim MyArray(i)
For n = 1 To i + 1
Set myitem = sourcedoc.Tables(1).Cell(n, 1).Range
myitem.End = myitem.End - 1
MyArray(n - 1) = myitem.Text
Next n
' Sluit het bestand met de organisatieonderdelen
cmbOrgOnd.List() = MyArray()
'I've tried to use several other codefragments before the real
'close-statement'. Nothing worked.
'sourcedoc.Saved = True
'Documents("G:\SjablonenStart-Dir\databases
\organisatieonderdelen.doc (Alleen-lezen)").Close
'sourcedoc.Activate
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Documents(strTargetname).Activate
End Sub
--------------------------------
Many thanks in advance
Rinze Smit
Revalidatie Friesland
David Sisson - 19 Dec 2007 22:37 GMT
It all looks correct.
So when you run this and it errors out, you select debug, the
SourceDoc.Close line is highlighted? And if you press F8, it gives
the same error on the same line?
Grabbing at straws here, but try to define SourceDoc
Dim SourceDoc as Document
Rinze Smit - 20 Dec 2007 07:55 GMT
> It all looks correct.
>
[quoted text clipped - 5 lines]
>
> Dim SourceDoc as Document
David,
thanks for your reply,
When debugging, the error does not occur!! So the code runs fine when
using F8.
Sourcedoc was defined. First I defined it within the same sub. Then I
defined it on module level... (cause I moved the 'close' statement).
However.... the result was the same. In debugmode the code runs fine.
Running it 'live': I get error 4198.
Rinze
David Sisson - 20 Dec 2007 17:24 GMT
Usually when it runs in the editor, but not at runtime, there is a
timing issue.
You might try
cmbOrgOnd.List() = MyArray()
DoEvents
Or You could add a delay and give Word time to process the userform
array. (I can only assume this is the culprit.)
cmbOrgOnd.List() = MyArray()
Application.OnTime When:=Now + TimeValue("00:00:03")
Rinze Smit - 20 Dec 2007 19:30 GMT
Thanks,
I'm gonna try next week.
I'll let you know how it went.
Happy holidyas.
Rinze
> Usually when it runs in the editor, but not at runtime, there is a
> timing issue.
[quoted text clipped - 9 lines]
> cmbOrgOnd.List() = MyArray()
> Application.OnTime When:=Now + TimeValue("00:00:03")
Rinze Smit - 28 Dec 2007 14:50 GMT
Well,
it didn't work. Since we're on a bit of a deadline, I decided to use
an Exceldocument.
That works out fine for us now.
Greetings,
Rinze Smit
> Thanks,
>
[quoted text clipped - 19 lines]
>
> - Tekst uit oorspronkelijk bericht weergeven -