472,353 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

How to create mailing labels or customized letters using MS Word MailMerge

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
Dec 10 '09 #1
0 2433

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Grim Reaper | last post by:
I print mailing labels out of Access 2000 databases about 3 to 4 times a week. I have been having problems with one thing since I have been printing...
1
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various...
4
by: Ray Todd Jr | last post by:
I can't get ths code to correctly execute. After I have clicked on a Access Form, I want the code to open the word document, merge the data, print...
0
by: Not Me | last post by:
Hi, After viewing data using a gridview (asp.net 2) I would like to send this to a word template for address labels. Can I do this using a...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and...
0
by: Alejandra Parra | last post by:
I'm merging labels in code (in VB .Net) I have the next code: Dim doc1 As New Object 'Dim doc1 As New Word.Document doc1 =...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the...
2
by: jojopangit | last post by:
hi, i have the following command and generated an error : "word was unable to open the data source" any help will be greatly appreciated. ...
3
by: brat33 | last post by:
I am trying to use a command button to automate a process for creating mailing labels from a query in Access 2007, going into word 2007. Current...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.