ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Printing a Word doc from Excel VBA code (https://www.excelbanter.com/excel-programming/339838-printing-word-doc-excel-vba-code.html)

BruceInMiami

Printing a Word doc from Excel VBA code
 
Thanks to John Walkenbach's Excel 2003 Power Programming, I can populate a
Word template with Excel data and save the doc file, but I can't figure out
the code to print the document so that I don't have to reopen it.

Can anyone help?
Thanks,
Bruce

BruceInMiami

Printing a Word doc from Excel VBA code
 
I found the answer in a 7/19/05 reply by Henry. I guess my problem was not
the wrdApp.PrintOut code, but that I needed to give Word time to print it
before closing it:
wrdApp.PrintOut
Application.Wait Time + TimeValue("00:00:05")

If anyone has any further comments or clarifications I would appreciate it.
Thanks,
Bruce

"BruceInMiami" wrote:

Thanks to John Walkenbach's Excel 2003 Power Programming, I can populate a
Word template with Excel data and save the doc file, but I can't figure out
the code to print the document so that I don't have to reopen it.

Can anyone help?
Thanks,
Bruce


Dave Peterson

Printing a Word doc from Excel VBA code
 
There's an option in MSWord that you can turn off.

Tools|Options|print tab|uncheck background printing

Then word will wait until the print job finishes.

A longggg time ago, (word95???), background printing caused trouble printing
tables or shapes or something. As a habit, I always turn this option off.
(Although, newer versions of MSWord may have been fixed.)

In code, you could do something like:

Option Explicit
Sub Testme()

Dim WDApp As Object
Dim WDDoc As Object
Dim myDocName As String
Dim myPrintBackground As Boolean

myDocName = "c:\my documents\word\doc2.doc"

Set WDApp = CreateObject("Word.Application")
WDApp.Visible = True 'at least for testing!

Set WDDoc = WDApp.documents.Open(Filename:=myDocName)

myPrintBackground = WDApp.Options.printbackground
WDApp.Options.printbackground = False
WDDoc.PrintOut
WDApp.Options.printbackground = myPrintBackground
WDDoc.Close savechanges:=False

WDApp.Quit

Set WDDoc = Nothing
Set WDApp = Nothing

End Sub

If you step through the code, you should see a slight delay while the wdapp.quit
is finishing up.


BruceInMiami wrote:

I found the answer in a 7/19/05 reply by Henry. I guess my problem was not
the wrdApp.PrintOut code, but that I needed to give Word time to print it
before closing it:
wrdApp.PrintOut
Application.Wait Time + TimeValue("00:00:05")

If anyone has any further comments or clarifications I would appreciate it.
Thanks,
Bruce

"BruceInMiami" wrote:

Thanks to John Walkenbach's Excel 2003 Power Programming, I can populate a
Word template with Excel data and save the doc file, but I can't figure out
the code to print the document so that I don't have to reopen it.

Can anyone help?
Thanks,
Bruce


--

Dave Peterson


All times are GMT +1. The time now is 11:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com