Help - EXCEL VBA to print file in MS Word - does not work

ilya49

New Member
Joined
Nov 24, 2011
Messages
9
Hi All,

I am trying to write a VBA code that would open MS word mail merge and using date from excell would populate the fields and print the file, and close MS word.

I have the following code:

Code:
Sub Open_Word_Document()

Dim objWord, WordApp As Object
ActiveWorkbook.Save
 
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
 
objWord.Documents.Open "C:\docs\Word.doc"
With objWord.ActiveDocument.MailMerge
        .OpenDataSource Name:=ActiveWorkbook.Path & "\" & ActiveWorkbook.Name, ReadOnly:=True, sqlstatement:="select * from [Output$]"
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        .Execute Pause:=False
End With

  objWord.PrintOut Filename:="", Range:=wdPrintRangeOfPages, Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="1", PageType:=wdPrintAllPages, _
         ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
        False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
        
objWord.Quit SaveChanges:=wdDoNotSaveChanges

End Sub


For some reason the script would not print the file. However, if i erase
"objWord.Quit SaveChanges:=wdDoNotSaveChanges", then the script would open, populate, and print. But it would not close Ms Word after the operation is done.

Anybody can help out?

Thanks.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top