Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Function processFile(ByVal datafile As String, ByVal fileOut As String) As String
- Dim ans As String = String.Empty
- errorLog = "C:TempTemplate_error.log"
- If (File.Exists(datafile)) Then
- Try
- ' Create an instance of Word and make it invisible.'
- wrdApp = CreateObject("Word.Application")
- wrdApp.Visible = False
- ' Add a new document.'
- wrdDoc = wrdApp.Documents.Add(fileOut & ".template")
- wrdDoc.Select()
- Dim wrdSelection As Word.Selection
- Dim wrdMailMerge As Word.MailMerge
- wrdDoc.MailMerge.OpenDataSource(datafile)
- wrdSelection = wrdApp.Selection()
- wrdMailMerge = wrdDoc.MailMerge()
- With wrdMailMerge
- .Execute()
- End With
- wrdDoc.SaveAs(fileOut)
- wrdApp.Quit(False)
- ' Release References.'
- wrdSelection = Nothing
- wrdMailMerge = Nothing
- wrdDoc = Nothing
- wrdApp = Nothing
- ans = "Merged OK"
- Call writeToLogFile(errorLog, "This worked, written to " & fileOut)
- Catch ex As Exception
- ans = "error : exception thrown " & ex.ToString
- Call writeToLogFile(errorLog, ans)
- End Try
- Else
- ans = "error ; unable to open Date File : " & datafile
- If (logErrors) Then
- Call writeToLogFile(errorLog, "The specified source csv file does not exist. Unable " & _
- "to process it. Filename provided: " & datafile)
- End If
- End If
- Return ans
- End Function
- wrdDoc = wrdApp.Documents.Add(TemplateFileName)
- wrdDoc.Select()
- Dim wrdSelection As Word.Selection
- Dim wrdMailMerge As Word.MailMerge
- wrdDoc.MailMerge.OpenDataSource(DataFileName)
- wrdSelection = wrdApp.Selection()
- wrdMailMerge = wrdDoc.MailMerge()
- With wrdMailMerge
- .Execute()
- End With
- ' This is the wrong thing to do. It just re-saves the template file you opened. '
- 'wrdDoc.SaveAs(OutputFileName) '
- ' The resulting merged document is actually stored '
- ' in the MailMerge object, so you have to save that '
- wrdMailMerge.Application.ActiveDocument.SaveAs(OutputFileName)
- wrdApp.Quit(False)
- wrdDoc.MailMerge.OpenDataSource(templateName)
- data = "C:My Data.xls"
- wrdDoc.MailMerge.OpenDataSource(data)
Add Comment
Please, Sign In to add comment