473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2524

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 mailing labels. I print mailing labels by...
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 fields from various tables in one document in Word...
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 the forms and then close the two windows that...
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 mailmerge? or just send the data directly? Any tips...
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 call methods and set properties in MS Word to...
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 = CreateObject("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 data entered through a form. the real question is...
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. Thanks in advance. Jude Private Sub...
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 code looks like the following: 'Start MS Word Dim...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.