Hi!
I have written some VB.Net (2008) to run a mailmerge... my problem being is that I want to make the instance of Word invisible to the user. The nearest that I van get is to minimise the Word application. The code that I have written is below.
Public Sub fred()
Dim sqlQry As String = "SELECT TOP 2 ContactName, ContactName AS ContactName1 from customers"
'Get the values from table
Dim ObjDatatable As DataTable = RetrieveData.GetData(sqlQry, DataBaseQueryType.Datatable)
Dim strTemplateFileName As String = "Love2shopReferrals.doc"
'Word merging
Dim objApp As Word.Application
objApp = CreateObject("Word.Application")
objApp.Visible = False
If IsNothing(objApp) = False Then
Dim strDocFileName As String
For Each row As DataRow In ObjDatatable.Rows
'Open the Document
objApp.Documents.Open(Path.Combine(ApplicationPath, strTemplateFileName))
objApp.WindowState = WdWindowState.wdWindowStateMinimize
objApp.Visible = False
strDocFileName = Convert.ToString(row(0)) & ".doc"
Dim intCount As Integer = 0
For Each MergeField As Word.MailMergeField In objApp.ActiveDocument.MailMerge.Fields
'Select the text
MergeField.Select()
'Type the text
objApp.Selection.TypeText(Convert.ToString(row(intCount)))
intCount += 1
Next
'Save the document
objApp.ActiveDocument.SaveAs(Path.Combine(ApplicationPath, strDocFileName))
'Close the document
objApp.Documents.Close()
Next
'Dont save the template changes
objApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
'Dispose
End If
objApp = Nothing
ObjDatatable.Dispose()
ObjDatatable = Nothing
NAR(objApp)
End Sub
If anyone can help me on this it would be appreciated!
Regards,
Alan
P.S. sorry about the code formatting!
I have written some VB.Net (2008) to run a mailmerge... my problem being is that I want to make the instance of Word invisible to the user. The nearest that I van get is to minimise the Word application. The code that I have written is below.
Public Sub fred()
Dim sqlQry As String = "SELECT TOP 2 ContactName, ContactName AS ContactName1 from customers"
'Get the values from table
Dim ObjDatatable As DataTable = RetrieveData.GetData(sqlQry, DataBaseQueryType.Datatable)
Dim strTemplateFileName As String = "Love2shopReferrals.doc"
'Word merging
Dim objApp As Word.Application
objApp = CreateObject("Word.Application")
objApp.Visible = False
If IsNothing(objApp) = False Then
Dim strDocFileName As String
For Each row As DataRow In ObjDatatable.Rows
'Open the Document
objApp.Documents.Open(Path.Combine(ApplicationPath, strTemplateFileName))
objApp.WindowState = WdWindowState.wdWindowStateMinimize
objApp.Visible = False
strDocFileName = Convert.ToString(row(0)) & ".doc"
Dim intCount As Integer = 0
For Each MergeField As Word.MailMergeField In objApp.ActiveDocument.MailMerge.Fields
'Select the text
MergeField.Select()
'Type the text
objApp.Selection.TypeText(Convert.ToString(row(intCount)))
intCount += 1
Next
'Save the document
objApp.ActiveDocument.SaveAs(Path.Combine(ApplicationPath, strDocFileName))
'Close the document
objApp.Documents.Close()
Next
'Dont save the template changes
objApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
'Dispose
End If
objApp = Nothing
ObjDatatable.Dispose()
ObjDatatable = Nothing
NAR(objApp)
End Sub
If anyone can help me on this it would be appreciated!
Regards,
Alan
P.S. sorry about the code formatting!