Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

Mailmerge from excel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
stevew - 05 Mar 2006 09:42 GMT
I am trying to create a mailmerge in excel with the click of a button. When
the button is clicked it opens word sucessfully.

Sub OpenWord()
   Dim wdApp As Object
   Dim wdDoc As Object

   Set wdApp = CreateObject("Word.application")
   Set wdDoc = wdApp.Documents.Open _
     (Filename:="C:\Documents and Settings\steve\My
Documents\MyTestDoc.doc")
   wdDoc.Close savechanges:=False
   Set wdDoc = Nothing
   wdApp.Quit
   Set wdApp = Nothing
End Sub

The code I have placed in Word gives me an error (Ambiguous selection) at
the second line .Destination=wdsendToPrinter.

Private Sub Document_Open()

With ActiveDocument.mailmerge
       .Destination = wdSendToPrinter
       .SuppressBlankLines = True
       With .DataSource
           .FirstRecord = wdDefaultFirstRecord
           .LastRecord = wdDefaultLastRecord
       End With
       .Execute Pause:=False
   End With

End Sub

I'm making the assumption (new to VBA in excel and word) that the document
is not
active but the spreadsheet is and therefore will not print. Not sure how to
solve this.

If anyone can give me any assistance it would be very much appreciated.

Steve Walker
Doug Robbins - Word MVP - 05 Mar 2006 10:19 GMT
Try having all of the code in Excel by using:

Sub OpenWord()
   Dim wdApp As Object
   Dim wdDoc As Object

   Set wdApp = CreateObject("Word.application")
   Set wdDoc = wdApp.Documents.Open _
     (Filename:="C:\Documents and Settings\steve\My _
Documents\MyTestDoc.doc")
   If wdDoc.MailMerge.State = wdMainAndDataSource Then
       With wdDoc.MailMerge
           .Destination = wdSendToPrinter
           .SuppressBlankLines = True
           With .DataSource
               .FirstRecord = wdDefaultFirstRecord
               .LastRecord = wdDefaultLastRecord
           End With
           .Execute Pause:=False
       End With
   Else
       MsgBox "The document that you opened does not have a datasource
attached to it."
   End If
   wdDoc.Close savechanges:=False
   Set wdDoc = Nothing
   wdApp.Quit
   Set wdApp = Nothing
End Sub

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I am trying to create a mailmerge in excel with the click of a button. When
> the button is clicked it opens word sucessfully.
[quoted text clipped - 39 lines]
>
> Steve Walker
stevew - 05 Mar 2006 11:50 GMT
Thanks Doug

Works and tells me that I have no data source. Opened the document without
going through the spreadsheet and the SQL check is running. Would like to
turn off this check, which should I hope solve the problem, tried
tools>options etc and cannot find the appropriate command in word.

If you could assist would appreciate your help on this one.

grateful

Steve

> Try having all of the code in Excel by using:
>
[quoted text clipped - 25 lines]
>    Set wdApp = Nothing
> End Sub
Graham Mayor - 05 Mar 2006 13:08 GMT
You receive the "Opening this will run the following SQL command" message
when you open a Word mail merge main document that is linked to a data
source - http://support.microsoft.com/?kbid=825765

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Thanks Doug
>
[quoted text clipped - 48 lines]
>>
>> SNIP
stevew - 05 Mar 2006 13:22 GMT
Cheers Graham worked a treat.

Appreciate your response.

Steve Walker

> You receive the "Opening this will run the following SQL command" message
> when you open a Word mail merge main document that is linked to a data
[quoted text clipped - 52 lines]
>>>
>>> SNIP
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.