• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9732
  • Last Modified:

Print word document from VB6

I would like to print a word document via VB6.  During the print process, I don't want any word dialog bosxs to appear.

The following snippet prints the document, but triggers multiple word dialog boxes:

Private Sub Command1_Click()
   Dim WordObj As Object
   Set WordObj = CreateObject("Word.Application")
   WordObj.Documents.Open "g:\my documents\sample.doc"
   WordObj.PrintOut Background:=False
   WordObj.Quit
   Set WordObj = Nothing
End Sub

Also, I've just started looking at Automation and I'm wondering why I don't see some of these classes/methods in the VB Object Browser.  In the VB IDE,I have a reference to word and I see Documents.Open, but not PrintOut or Quit.  Do they appear in some other class?  I'm an absolute Automation novice.  Is there a good basic tutorial available anywhere?

Thanks for any help.
0
lennyh
Asked:
lennyh
  • 5
  • 2
  • 2
  • +1
1 Solution
 
Mikal613Commented:
Option Explicit
Private Declare Function ShellExecuteAny Lib "shell32.dll" Alias "ShellExecuteA" ( _
                         ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
                         ByVal lpParameters As Any, ByVal lpDirectory As Any, ByVal nShowCmd As Long) _
                         As Long

Private Const SW_SHOWMINNOACTIVE = 7

Private Sub Command1_Click()

    Dim lRet As Long
    Dim l As Long
   
    'executing the ShellExecute print Windows method
    lRet = ShellExecuteAny(l, "print", "g:\my documents\sample.doc", ByVal 0&, ByVal 0&, SW_SHOWMINNOACTIVE)

End Sub

0
 
karthislmCommented:
Hi,

Add the following line after creating the word application object.

WordObj.DisplayAlerts=0

This line will stop displaying word dialog boxes. Try this and let me know, whether it works.

Karthi
0
 
GrahamSkanCommented:
To address the Automation side of the question.

Your code is using a hybrid between Early and Late binding.

Setting a reference to the relevant object library means that you can use early binding. I always use that for development because you can then use intellisense, the object browser and the help file facility.

Some developers report version issues with early binding, so you may want to convert to late binding after development is complete. This only needs minimal changes at the top of the procedure.

This is pure early bindng (reference necessary)
Private Sub Command1_Click()
   Dim WordObj As Word.Application
   Dim Doc as Word.Doc
   Set WordObj = New Word.Application
   Set Doc = WordObj.Documents.Open("g:\my documents\sample.doc")
   WordObj.PrintOut Background:=False
   WordObj.Quit
   Set WordObj = Nothing
End Sub

This is late binding (no reference necessary)
Private Sub Command1_Click()
   Dim WordObj As Object
   Dim Doc As Object
   Set WordObj = CreateObject("Word.Application")
 'rest of code is unchanged
   Set Doc = WordObj.Documents.Open("g:\my documents\sample.doc")
   WordObj.PrintOut Background:=False
   WordObj.Quit
   Set WordObj = Nothing
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lennyhAuthor Commented:
Thanks to all responders for helpful info.

Mikal613: Your shell snippet works nicely , but has the limitation that it flashes the Word GUI on the display prior to triggering the print.  Is there a way to eliminate that?  Also, can you tell me if there are any inherent advantages -- one way or another -- of using shell vs. automation?

Karthi:  The added line reduced thte number of generate dialog boxes, but one still remains: "Do you want to save changes to..."  Is there a way to eliminate that one as well.

GrahamScan: When I tried the early binding example, I got an error "User defined type not defined"  on the "Dim Doc as Word.Doc" statement.  I  am apparently overlooking something.  Can you clarify?

Also, I'm still a bit puzzled as to why I see no reference to  "PrintOut" in the Object Browser.

Lenny

0
 
GrahamSkanCommented:
Sorry, That should be Word.Document
0
 
GrahamSkanCommented:
If you don't want to save the document, you can set its Saved property to True.
0
 
GrahamSkanCommented:
I only get a printer-specific progress window when I use this code. Note that I am not setting the DisplayAlerts property.

Private Sub Command1_Click()
   Dim WordObj As Word.Application
   Dim Doc As Word.Document
   Set WordObj = New Word.Application
   Set Doc = WordObj.Documents.Open("C:\Documents and Settings\Graham Skan\My Documents\doc1.doc")
   WordObj.PrintOut Background:=False
   Doc.Saved = True
   WordObj.Quit
   Set WordObj = Nothing
End Sub

Also I have no problem finding the Printout and the Quit methods either in Intellisense or the Object browser.

Word 2000 (SP3), VB6(SP5), Windows 2000(SP4)
0
 
Mikal613Commented:
well with shell you dont have to worry about opening the a word object and closing it.
0
 
lennyhAuthor Commented:
Thanks to all responders for helpful information.

GrahamSkan: Your solution worked perfectly.  Also, I eventually found PrintOut and Quit in the Object Browser (I hadn't known enough to select the Appication class ;-{)

Where would one look to find a description/explanation of all of the word application properties and methods?
0
 
GrahamSkanCommented:
They are reasonably well documented in Word VBA help.

Highlight the name of a Word object, such as Document, and press F1. This should open the right help file.

Note that if you have a reference to the Excel library (in Word VBA), and you select an object with a name in common, e.g. Range, you will be offered the Excel objects help.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now