opening & writing to Word from Excel

M

Mike Molyneaux

I'm running Excel & Word 2000 on nt4, sp6

I'm trying to create a mailmerge file from Excel with data
from the excel sheet.

I get a 'variable not defined' compiler error on
the 'wdMailingLabels' value of
the '.ActiveDocument.MailMerge.MainDocumentType = ' line.

I expect similar compile errors on other values. How can I
find what I need to code these in order to execute the
macro?


Dim wordApp As Object
Set wordApp = CreateObject("Word.Application")

With wordApp

.ActiveDocument.MailMerge.MainDocumentType =
wdMailingLabels

End With

wordApp.Quit
Set wordApp = Nothing


thanks for any help
 
D

Debra Dalgleish

In Word, you can type a question mark and constant name in the Immediate
window, e.g.:
?wdMailingLabels

then press the Enter key, to determine the value of the constant.

In Excel, list the constants at the top of the module, and the code
should run correctly:

'================================
Option Explicit
Const wdMailingLabels As Long = 1

Sub MergeDoc()
Dim WdApp As Object

Selection.Copy
On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WdApp = CreateObject("Word.Application")
End If

With WdApp
.Visible = True
.Documents.Add DocumentType:=0
.ActiveDocument.MailMerge.MainDocumentType _
= wdMailingLabels
End With
' Set WdApp = Nothing

End Sub
'==========================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top