
Automating Mail Merge with VB.Net and Office XP and Office 2000 produces mixed results
I perform merges automatically from my SQL databases in VB.Net by creating a
text (delimited) file then opening the datasource through code. In VB6
(which I translated the code from) this process works fine on either word
2000 or Word 2002... From VB.Net, the process works on Word XP, but will not
open the datasource file when using Word 2000. I have made sure to use the
lowest common denomenator features to ensure compatibility.
Here is the code that is crashing
Select Case myValue3
Case 0
myValue3 = CInt(Int((14923 * Rnd()) + 1))
FileOpen(1, Application.StartupPath & "\merging\" & CStr(myValue3) & ".txt",
OpenMode.Output)
adoMain.Open("Select * from WordMerge1 where contactid = 0", adoConnection)
For gencount = 1 To adoMain.Fields.Count - 1
Write(1, adoMain.Fields(gencount).Name)
Next
adoMain.Close()
WriteLine(1)
For Each lstRecipient In ListView1.Items
Select Case lstRecipient.SubItems(2).Text
Case Is > 0
adoMerge.Open("Select * from wordmerge2 where contactID = " &
lstRecipient.Tag & " and mrgprojectid = " & lstRecipient.SubItems(2).Text,
adoConnection)
Case Else
adoMerge.Open("Select * from wordmerge1 where contactID = " &
lstRecipient.Tag, adoConnection)
End Select
For gencount = 1 To adoMerge.Fields.Count - 1
Select Case IsDBNull(adoMerge(gencount).Value)
Case False
Write(1, adoMerge(gencount).Value)
Case True
Write(1, "")
End Select
Next
adoMerge.Close()
WriteLine(1)
Next
FileClose(1)
Case Else
myValue4 = myValue3 + 1
FileCopy(Application.StartupPath & "\merging\" & CStr(myValue3) & ".txt",
Application.StartupPath & "\merging\" & CStr(myValue4) & ".txt")
myValue3 = myValue3 + 1
End Select
With objWord.Selection
If objWord.ActiveDocument.MailMerge.State = 2 Then
objWord.ActiveDocument.MailMerge.MainDocumentType = -1
End If
objWord.ActiveDocument.MailMerge.MainDocumentType = 0
Select Case myValue4
Case Is > 0
objWord.ActiveDocument.MailMerge.OpenDataSource(Name:=Application.StartupPat
h & "\merging\" & CStr(myValue4) & ".txt", ConfirmConversions:=False)
Case Else
objWord.ActiveDocument.MailMerge.OpenDataSource(Name:=Application.StartupPat
h & "\merging\" & CStr(myValue3) & ".txt", ConfirmConversions:=False)
End Select
objWord.ActiveDocument.MailMerge.EditMainDocument()
Help
Philip Carter