Mailmerge error

  • Thread starter Betina Y Andersen
  • Start date
B

Betina Y Andersen

I have a dll which does some mailmerge for me, the dll is called from VB6.
It work fine with word 2002 but it fails in this routine I have here below.
The line that fails is
Kill mdataDocNavn, it deletes the temp file created. but it does not fail on
first run but from secund run and more, I found that the line
..ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges on first run closes
the tempfile created by the OpenDataSource function, but only on first run,
so later when I try to delete my tempfile it is open and I cannot access it,
it is open but not in the Documents collection, so what do I do?


Public Sub MergeAndPrint(Optional VisWord As Variant)
Dim i As Integer
Dim i2 As Integer
Dim VisDokument As Boolean
Dim boolFillInFelter As Boolean
Dim testField As Word.Field

On Error GoTo errorrut:

If Not IsMissing(VisWord) Then
VisDokument = CBool(VisWord)
Else
VisDokument = True
End If

With WordObj

.ActiveDocument.SaveAs FileName:=mDataDocNavn, FILEFormat:=0,
LOCKCOMMENTS:=False, Password:="", ADDTORECENTFILES:=False,
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=0,
SAVENATIVEPICTUREFORMAT:=0, SaveFormsData:=0
.ActiveDocument.Close
.Windows(masterdok).Activate
'Indsat 9-10-2002 for at løse problemet med manglende menu
funktioner
'hvis der findes Fill-In felter i dokumentet
boolFillInFelter = False
For Each testField In .ActiveDocument.Fields
If InStr(testField.Code, "FILLIN") Then
boolFillInFelter = True
Exit For
End If
Next testField
'*************************************************************
.ActiveDocument.MailMerge.MainDocumentType = 0
With .Selection.Find
.ClearFormatting
.Text = "\* FLETFORMAT"
.Replacement.Text = "\* MERGEFORMAT"
.Replacement.ClearFormatting
.Execute Replace:=wdReplaceAll
End With

If NormalFeltCollection.Count > 0 Or ListPackCollection.Count > 0
Then
.ActiveDocument.MailMerge.OpenDataSource Name:=mDataDocNavn,
ConfirmConversions:=0, ReadOnly:=0, LINKTOSOURCE:=1, ADDTORECENTFILES:=0,
PASSWORDDOCUMENT:="", PASSWORDTEMPLATE:="", Revert:=0,
WRITEPASSWORDDOCUMENT:="", WRITEPASSWORDTEMPLATE:="", Connection:="",
SQLSTATEMENT:="", SQLSTATEMENT1:=""
.ActiveDocument.MailMerge.Destination = wdSendToNewDocument

<SOME CODE>

.ActiveDocument.MailMerge.Execute

<SOME CODE>

'**********************************************************
End If

.Windows(masterdok).Activate
.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End With

If NormalFeltCollection.Count > 0 Or ListPackCollection.Count > 0 Then
<Lots of replacements>
For i = 1 To WordObj.ActiveDocument.Sections.Count
For i2 = 1 To WordObj.ActiveDocument.Sections(i).Headers.Count
With
WordObj.ActiveDocument.Sections(i).Headers(i2).Range.Fields
.Update
End With
Next i2
For i2 = 1 To WordObj.ActiveDocument.Sections(i).Footers.Count
With
WordObj.ActiveDocument.Sections(i).Footers(i2).Range.Fields
.Update
End With
Next i2

Next i
WordObj.ActiveWindow.View.ShowFieldCodes = False
End If

Kill mDataDocNavn
If VisDokument Then
WordObj.Visible = True
End If
Exit Sub
errorrut:
MsgBox (Err.Description)
'Resume
If Errors.Count > 1 Then
For i = 0 To Errors.Count - 1
MsgBox (Errors(i).Description)
Next
End If
End Sub
 
D

Dianne Siebold

I have a DLL that does the same thing, the difference might be that my mDataDoc is a file that is generated with a unique name each time. Below is a copy of the routine I use to clean up the original template and data docs if the merge is successful and it closes Word if there's a failure.

Private Function CleanUpDocs() As Boolean

On Error Resume Next
'Close the template doc
mWordMergeDoc.Saved = True
mWordMergeDoc.Close False
mDataDoc.Saved = True
mDataDoc.Close

Set mWordMailMerge = Nothing
Set mWordMergeDoc = Nothing
Set mDataDoc = Nothing

mTemplateFile = ""
Kill mDataFile

If mFailed = True Then
mWord.Quit
Set mWord = Nothing
End If
mFailed = False

End Function
 
B

Betina Andersen

The temp document does get closed, but after running the OpenDataSource
command it gets opened again and not closed when I close the
"masterdocument". I have now found out that this only happens when I use
GetObject and not CreateObject, unfortunately for me I cannot change that
since the createobject is slower than getobject. Does anyone know why the
bewhaviour is different?

regards Betina

Dianne Siebold said:
I have a DLL that does the same thing, the difference might be that my
mDataDoc is a file that is generated with a unique name each time. Below is
a copy of the routine I use to clean up the original template and data docs
if the merge is successful and it closes Word if there's a failure.
 
B

Betina Andersen

I can see I forgot to mention that the problem is in Word 2003, sorry :)
It works fine in Word 2002 but not in Word 2003.
regards Betina
 

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