error 4198 when closing document

R

Rinze Smit

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
 
D

David Sisson

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
 
R

Rinze Smit

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

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
 
D

David Sisson

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")
 
R

Rinze Smit

Thanks,

I'm gonna try next week.
I'll let you know how it went.
Happy holidyas.

Rinze
 
R

Rinze Smit

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top