Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB 2008 combine word docs into one then use for mail merge.

Status
Not open for further replies.

tdrclan

Programmer
Sep 18, 2007
52
US
I need help in coding VB 2008 so that it can combine several word document into one to be used for a mail merge template.

I need to:
1) combine the word doc (does not work see note 1)
2) use this new doc a for the mail merge (works, only for the rewrite doc)
3) create a pdf. (works, only for the rewrite doc)

note 1:
say we had a document package that could contain one or more of the following (main1, pt1, pt2, pt3, pt4, pt5) based on state.

ex.
for NY we would need (main1, pt1, pt2, pt3)
for VA we would need (main1, pt1, pt2, pt4, pt5)
for the rest of the states we would need (main1, pt3, pt5)

here is my code.

Sub mergeDocs()
Dim jeff As New Word.Application
Dim d As Word.Document
Dim PrinterName As String
Dim strconn As String
Dim count As Integer = 0

Dim sqlConn As New SqlConnection("Data Source=sport;Integrated Security=SSPI;Initial Catalog=sportsdb")
Dim sqlCmd As SqlCommand
Dim sqlRead As SqlDataReader
sqlConn.Open()

sqlCmd = New SqlCommand("SELECT DocId,saveAS FROM iDocPrintRewriteBills Where bundletype = 'Rewrite' GROUP BY DocId, saveAS", sqlConn)
sqlRead = sqlCmd.ExecuteReader()

d = jeff.Documents.Open("C:\Rewrite.doc")

' this is the code I"m trying to add.
jeff.ActiveDocument.Merge(FileName:="C:\Agent Name Coversheet.doc")
' even tried .addfiles

strconn = "DSN=sportsDB;uid=sport;pwd=;"

PrinterName = jeff.ActivePrinter
jeff.ActivePrinter = "\\printer7"

While sqlRead.Read()
d.MailMerge.OpenDataSource(Name:="", Connection:=strconn, SQLStatement:="SELECT * FROM iDocPrintRewriteBills WHERE DocId = '" & sqlRead("DocId").ToString & "'")
d.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
d.MailMerge.Execute()

count = count + 1
Console.WriteLine("Generating " & count & " of " & rewritecount & " Rewrite PDF files..." & sqlRead("saveAS") & " " & FormatPercent(count / rewritecount) & " total completed")
jeff.ActiveDocument.ExportAsFixedFormat(OutputFileName:=My.Settings.pdfPath & sqlRead("saveAS"), ExportFormat:=Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=Word.WdExportOptimizeFor.wdExportOptimizeForPrint, BitmapMissingFonts:=True, UseISO19005_1:=True)
jeff.ActiveDocument.Close(False)
End While
d.Close(False)
jeff.Quit(False)
sqlConn.Close()

End Sub

--------------------- end code ----------------

my guess is that I'm adding the second doc into the ozone not the main doc.

TIA

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top