I was trying to use the sample code below, but I encountered two errors:
Application.DoEvents() and AutoText(entry). I am not sure how to correct the errors. I trying to use the sample code to create a mailmerge routine that accepts data from my SQL database and create a word doc that ce be previewed in the client browser. My OS is XP Pro and I am coding in VB.NET.
Protected Sub CreateWordMailMerge(ByVal strLabelName As String, _
ByVal wd_AlignmentCode As Word.WdCellVerticalAlignment, _
ByVal intOffset As Single, _
ByVal strFileName As String, _
ByVal bUseTemplate As Boolean)
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim x As Integer
Dim bOK As Boolean = False
Dim oAutoText As Word.AutoTextEntry
Try
'Start a new document in Word
oApp = CType(CreateObject("Word.Application"), Word.Application)
If bUseTemplate Then
Try
oDoc = oApp.Documents.Open(CType(strLabelName, System.Object), False, True, _
False, , , True, , , Word.WdOpenFormat.wdOpenFormatDocument, , True)
Catch MyException As System.Exception
MsgBox(String.Concat("Unable to open ", strLabelName, _
ControlChars.Cr.ToString, ControlChars.Cr.ToString, _
"Error Message: ", MyException.Message, ControlChars.Cr.ToString, _
"Source: ", MyException.Source, ControlChars.Cr.ToString, _
"InnerException: ", MyException.InnerException.Message, ControlChars.Cr.ToString), _
MsgBoxStyle.Critical, "Label Template Open Failure")
CType(oApp, Word._Application).Quit()
oApp = Nothing
Exit Sub
End Try
Else
oDoc = oApp.Documents.Add
End If
oApp.Visible = True ' Make MS Word visible
Application.DoEvents()
' Do MailMerge
With oDoc.MailMerge
If Not bUseTemplate Then
With .Fields
.Add(oApp.Selection.Range, "Line1")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "Line2")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "Line3")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "Line4")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "Line5")
End With
oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("wordAutoT extTemp", oDoc.Content)
oDoc.Content.Delete()
End If ' If bUseTemplate
' Set up the mail merge type as mailing labels and use
' a tab-delimited text file as the data source.
.MainDocumentType = WdMailMergeMainDocType.wdFormLetters
.OpenDataSource(strFileName, Word.WdOpenFormat.wdOpenFormatText, False, True, False, False)
If Not bUseTemplate Then
'
'Create the new document for the labels using the
AutoText(entry)
'
oApp.MailingLabel.CreateNewDocument(CType(strLabel Name, String), "", "wordAutoTextTemp", False, Word.WdPaperTray.wdPrinterManualFeed)
End If ' If bUseTemplate
'
'Execute the mail merge to generate the labels.
.Destination = WdMailMergeDestination.wdSendToNewDocument
.Execute()
'
If Not bUseTemplate Then
'Delete the AutoText entry
oAutoText.Delete()
End If ' If bUseTemplate
End With
'
'Close the original document so that
'the Mail Merge results are displayed
CType(oDoc, Word._Document).Close(False)
'
If Not bUseTemplate Then
' Apply vertical alignment
For x = 1 To oApp.ActiveDocument.Tables.Count
oApp.ActiveDocument.Tables.Item(x).Range.Cells.Ver ticalAlignment = wd_AlignmentCode
Next
Application.DoEvents()
For x = 1 To oApp.ActiveDocument.Tables.Count
oApp.ActiveDocument.Tables.Item(x).LeftPadding = oApp.InchesToPoints(intOffset)
oApp.ActiveDocument.Tables.Item(x).Rows.LeftIndent = oApp.InchesToPoints(intOffset)
Next
Application.DoEvents()
End If
If oApp.ActiveWindow.View.Type <> Word.WdViewType.wdPrintView Then
If oApp.ActiveWindow.View.SplitSpecial = Word.WdSpecialPane.wdPaneNone Then
oApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintPreview
Else
oApp.ActiveWindow.View.Type = WdViewType.wdPrintPreview
End If
End If
'
'Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved = True
oApp = Nothing
oDoc = Nothing
Catch MyException As System.Runtime.InteropServices.COMException
MsgBox(String.Concat("Error occured while communicating with MS Word(", ")) ControlChars.Cr.ToString, ControlChars.Cr.ToString, ErrorCode: ", MyException.ErrorCode, ControlChars.Cr.ToString, "Error Message: ", MyException.Message, ControlChars.Cr.ToString, "Source: ", MyException.Source, ControlChars.Cr.ToString, "StackTrace: ", MyException.StackTrace, ControlChars.Cr.ToString), MsgBoxStyle.Critical, "MS Word Problem")
End Try
End Sub