Mailmerge error?

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
 
J

JGM

Hi Betina,

Have you tried replacing:by
.ActiveDocument.Close SaveChanges:=WordObj.wdDoNotSaveChanges
?
(You said that was the line creating the problem, right?)
Just curoius...
 

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