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

Re: Mailmerge from excel by Doug

Doug
Sun Mar 05 04:19:18 CST 2006

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

--
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

"stevew" <sagwalker@blueyonder.co.uk> wrote in message
news:PnyOf.115300$YJ4.4981@fe2.news.blueyonder.co.uk...
>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
>
>
>



Re: Mailmerge from excel by stevew

stevew
Sun Mar 05 05:50:15 CST 2006

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

"Doug Robbins - Word MVP" <dkr@REMOVETHISmvps.org> wrote in message
news:%23l3lH5DQGHA.224@TK2MSFTNGP10.phx.gbl...
> 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
>
> --
> 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
>
>SNIP



Re: Mailmerge from excel by Graham

Graham
Sun Mar 05 07:08:10 CST 2006

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

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

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

stevew wrote:
> 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
>
> "Doug Robbins - Word MVP" <dkr@REMOVETHISmvps.org> wrote in message
> news:%23l3lH5DQGHA.224@TK2MSFTNGP10.phx.gbl...
>> 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
>>
>> --
>> 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
>>
>> SNIP



Re: Mailmerge from excel by stevew

stevew
Sun Mar 05 07:22:38 CST 2006

Cheers Graham worked a treat.

Appreciate your response.

Steve Walker

"Graham Mayor" <gmayor@mvps.org> wrote in message
news:%236ILXXFQGHA.4452@TK2MSFTNGP10.phx.gbl...
> 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
>
> --
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor - Word MVP
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
> stevew wrote:
>> 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
>>
>> "Doug Robbins - Word MVP" <dkr@REMOVETHISmvps.org> wrote in message
>> news:%23l3lH5DQGHA.224@TK2MSFTNGP10.phx.gbl...
>>> 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
>>>
>>> --
>>> 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
>>>
>>> SNIP
>
>