views:

200

answers:

1

hi, i have a document mydocument.docx i want to print that from a button in excel sheet. both are in same folder.

i don't want users to see word document.They just click the button in excel and printing of word file starts.

this is actually very urgent. and i am totally new to this so if u can explain this in steps that would be so awesome.

i can create button in excel and make it to open empty vb. this is far i know.

thanks very much for your time.

+3  A: 

You can use the Word automation object model to gain programmatic access to Word.

In almost all cases, you'd be following these steps:

  1. Create the Word application object.
  2. Open a document.
  3. Do something with the document.
  4. Close the document.
  5. Quit the Word application.

Here is what the basic 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
bobbymcr
thanks that work perfect. is this possible to list printers so i can decide what printer to use.thanks
nickjohn
I assume you want to programmatically set the printer (as opposed to having the user interactively select it). See my edited answer for an example, using the ActivePrinter property.
bobbymcr
sweet very cool thanks
nickjohn
can you check this question too thanks http://stackoverflow.com/questions/1425843/how-to-mail-merge-data-from-two-sheets-in-one-excel-workbook-to-word
nickjohn