-
Create Envelopes
I have written a vba code to create envelopes in word.
I am taking All the details from outlook and creting envelopes forthose in word.
My code is working on xp(office 2003), But not on Windows 2000 prof.(office 2000).
Code:
Dim oApp As Word.Application
Dim oDoc As Word.Document
'Start a new document in Word
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Add
With oDoc.MailMerge
oApp.DisplayAlerts = False
'Insert the mail merge fields temporarily so that
'you can use the range containing the merge fields as a layout
'for your envelopes -- to use this as a layout, you can add it
'as an AutoText entry.
With .Fields
.Add oApp.Selection.Range, "Contact_Name"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Address"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "City"
oApp.Selection.TypeText ","
.Add oApp.Selection.Range, "State"
oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "Postal_Code"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "Country"
End With
Dim oAutoText As Word.AutoTextEntry
Set oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyEnvelopeLayout", oDoc.Content)
oDoc.Content.Delete 'Merge fields in document no longer needed now
'that the AutoText entry for the envelope layout
'has been added so delete it.
'Set up the mail merge type as mailing envelopes and use
'a tab-delimited text file as the data source.
.MainDocumentType = wdEnvelopes
.OpenDataSource Name:=CC 'Specify your data source here
addr = lstenvelopes.Text
ice = Left(addr, Len(addr) - InStr(addr, "("))
sp = Trim(ice)
oDoc.MailMerge.MainDocumentType = wdEnvelopes
oDoc.Envelope.Insert ReturnAddress:=" "
oDoc.Envelope.DefaultSize = sp
oApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
oApp.Selection.TypeText txtAddress.Text
'Create the new document for the envelopes using the AutoText entry
oDoc.Envelope.Insert Address:="", _
AutoText:="MyEnvelopeLayout"
'Execute the mail merge to generate the envelopes.
.Destination = wdSendToNewDocument
.Execute
'Delete the AutoText entry you added
oAutoText.Delete
End With
I am getting error on .execute
Run-time error '4605':
This method or property is not availale bcause the current mail merge main document needs a datasource.
What would be the problem?
-
Re: Create Envelopes
Hi !
I have modified code a bit.
I moved this line below
Code:
.OpenDataSource Name:=CC
Now Envelope is created but without the return address which is placed at the top left corner of the envelope.
Another poblem is Temporary wordfile which i have created its not deleting.
giving error: Permission denied.
The entire code is working fine on xp(office 2003).
If anyone can help me......