473,882 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
I was trying to use the sample code below, but I encountered two errors:
Application.DoE vents() 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 CreateWordMailM erge(ByVal strLabelName As String, _
ByVal wd_AlignmentCod e As Word.WdCellVert icalAlignment, _
ByVal intOffset As Single, _
ByVal strFileName As String, _
ByVal bUseTemplate As Boolean)

Dim oApp As Word.Applicatio n
Dim oDoc As Word.Document
Dim x As Integer
Dim bOK As Boolean = False
Dim oAutoText As Word.AutoTextEn try

Try
'Start a new document in Word
oApp = CType(CreateObj ect("Word.Appli cation"), Word.Applicatio n)
If bUseTemplate Then
Try
oDoc = oApp.Documents. Open(CType(strL abelName, System.Object), False, True, _
False, , , True, , , Word.WdOpenForm at.wdOpenFormat Document, , True)

Catch MyException As System.Exceptio n
MsgBox(String.C oncat("Unable to open ", strLabelName, _
ControlChars.Cr .ToString, ControlChars.Cr .ToString, _
"Error Message: ", MyException.Mes sage, ControlChars.Cr .ToString, _
"Source: ", MyException.Sou rce, ControlChars.Cr .ToString, _
"InnerException : ", MyException.Inn erException.Mes sage, ControlChars.Cr .ToString), _
MsgBoxStyle.Cri tical, "Label Template Open Failure")
CType(oApp, Word._Applicati on).Quit()

oApp = Nothing

Exit Sub

End Try
Else
oDoc = oApp.Documents. Add

End If

oApp.Visible = True ' Make MS Word visible
Application.DoE vents()

' Do MailMerge
With oDoc.MailMerge
If Not bUseTemplate Then
With .Fields
.Add(oApp.Selec tion.Range, "Line1")
oApp.Selection. TypeParagraph()
.Add(oApp.Selec tion.Range, "Line2")
oApp.Selection. TypeParagraph()
.Add(oApp.Selec tion.Range, "Line3")
oApp.Selection. TypeParagraph()
.Add(oApp.Selec tion.Range, "Line4")
oApp.Selection. TypeParagraph()
.Add(oApp.Selec tion.Range, "Line5")

End With
oAutoText = oApp.NormalTemp late.AutoTextEn tries.Add("word AutoTextTemp", oDoc.Content)
oDoc.Content.De lete()

End If ' If bUseTemplate

' Set up the mail merge type as mailing labels and use
' a tab-delimited text file as the data source.
.MainDocumentTy pe = WdMailMergeMain DocType.wdFormL etters
.OpenDataSource (strFileName, Word.WdOpenForm at.wdOpenFormat Text, False, True, False, False)

If Not bUseTemplate Then
'
'Create the new document for the labels using the
AutoText(entry)
'
oApp.MailingLab el.CreateNewDoc ument(CType(str LabelName, String), "", "wordAutoTextTe mp", False, Word.WdPaperTra y.wdPrinterManu alFeed)

End If ' If bUseTemplate
'
'Execute the mail merge to generate the labels.
.Destination = WdMailMergeDest ination.wdSendT oNewDocument
.Execute()
'
If Not bUseTemplate Then
'Delete the AutoText entry
oAutoText.Delet e()

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.ActiveDocu ment.Tables.Cou nt
oApp.ActiveDocu ment.Tables.Ite m(x).Range.Cell s.VerticalAlign ment = wd_AlignmentCod e

Next

Application.DoE vents()

For x = 1 To oApp.ActiveDocu ment.Tables.Cou nt
oApp.ActiveDocu ment.Tables.Ite m(x).LeftPaddin g = oApp.InchesToPo ints(intOffset)
oApp.ActiveDocu ment.Tables.Ite m(x).Rows.LeftI ndent = oApp.InchesToPo ints(intOffset)

Next

Application.DoE vents()

End If

If oApp.ActiveWind ow.View.Type <> Word.WdViewType .wdPrintView Then
If oApp.ActiveWind ow.View.SplitSp ecial = Word.WdSpecialP ane.wdPaneNone Then
oApp.ActiveWind ow.ActivePane.V iew.Type = WdViewType.wdPr intPreview

Else
oApp.ActiveWind ow.View.Type = WdViewType.wdPr intPreview

End If

End If
'
'Prevent save to Normal template when user exits Word
oApp.NormalTemp late.Saved = True
oApp = Nothing
oDoc = Nothing

Catch MyException As System.Runtime. InteropServices .COMException
MsgBox(String.C oncat("Error occured while communicating with MS Word(", ")) ControlChars.Cr .ToString, ControlChars.Cr .ToString, ErrorCode: ", MyException.Err orCode, ControlChars.Cr .ToString, "Error Message: ", MyException.Mes sage, ControlChars.Cr .ToString, "Source: ", MyException.Sou rce, ControlChars.Cr .ToString, "StackTrace : ", MyException.Sta ckTrace, ControlChars.Cr .ToString), MsgBoxStyle.Cri tical, "MS Word Problem")

End Try

End Sub
Dec 10 '09 #1
0 2551

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

Similar topics

3
4331
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 using a report I created and then printing them on a continuous-feed dot matrix printer. I used the Label Wizard, chose the query I created, picked the 'Avery USA 4146' (1 7/16" x 4") - 3 across, paper size: 'Fanfold 358mm x 12 in', and Printer:...
1
4967
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 1. Query..I want to keep the fields seperatred. I do not want to sent on field with all accumulated languages from one person to Word. Each language should appear in the document in a separate cell Cross tables are not delivering the result I...
4
12059
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 open of Word. The Mail Merge takes place, and the sticking point is that I have to manually close the two Word Windows. Can someone please tell me what I'm not doing correctly. Below is the code I am using...
0
1166
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 would be much appreciated. Chris
2
13540
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 execute a MailMerge to create mailing labels or customized letters. A label name known to MS Word MailMerge mailing label wizard may be used or a template file containing the field names Line1 thru Line5 for each record to be printed. If a...
0
973
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 = app1.Documents.Add(documenttype:=Documento_Tipo_Documento) 'Word.WdDocumentType.wdTypeDocument doc1.MailMerge.MainDocumentType = DocumentoPrincipal_Tipo_EtiquetasDistribucion
4
12459
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
2
3346
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 Command1_Click()
3
5012
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 objWord As Word.Application Set objWord = CreateObject("Word.Application") 'Make Application visible With objWord .Visible = True ActiveDocument.MailMerge.MainDocumentType = wdMailingLabels
0
9931
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10827
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9556
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7954
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7113
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4599
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.