Print Word document without opening it with Excel VBA

I want to print a Word document, mydocument.docx, using a button on an Excel sheet. Both are in the same folder.

I don't want users to see the Word document . They just click a button in Excel.

I can create a button in Excel and make it open with a blank VB. This is as much as I know. If you can explain it in stages, that would be so great.

+2


source to share


2 answers


You can use the Word Automation Object Model to programmatically access Word.

In almost all cases, you should follow these steps:

  • Create a Word application object.
  • Open your document.
  • Do something with the document.
  • Close the document.
  • Close Word.


This is what the main VBA code looks like:

' Step 1
Dim objWord
Set objWord = CreateObject("Word.Application")
' Hidden window!
objWord.Visible = False
' Save the original printer, otherwise you will reset the system default!
Dim previousPrinter
Set previousPrinter = objWord.ActivePrinter
objWord.ActivePrinter = "My Printer Name"

' Step 2
Dim objDoc
Set objDoc = objWord.Documents.Open("C:\Test\SomeDocument.docx")

' Step 3 -- in this case, print out the document without any prompts
objDoc.PrintOut
' Restore the original printer
objWord.ActivePrinter = previousPrinter

' Step 4
objDoc.Close

' Step 5
objWord.Quit

      

+5


source


How can I get automatic printing of Word document which is linked to Excel sheet via mail merge. Because this print command prints the "active page" while my entries will grow with each new entry.



For example, if I create the first record and link it to a word document and also use this "auto-print code". this will work fine for me, but when I create the second record and use the "auto print" command, the macro will still print the 1st record.

0


source







All Articles