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