Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print word document from VBA

Status
Not open for further replies.

dgillz

Instructor
Mar 2, 2001
10,043
US
I am trying to print a word document from vba, but I keep getting an "Object variable or with block variable not set" error. Here is my code, the line in red is where I am getting the error:

Code:
Private Sub btn1_click
  Dim objDoc As Word.Document
  Dim objWord As Word.Application
  
  CreateObject ("Word.Application")
  [red]Set objDoc = objWord.Documents.Open("C:\MyDirectory\12345.doc")[/red]
  
  objDoc.PrintOut
  objWord.Quit
End Sub

Any insight on what the problem is would be appreciated.

Software Training, Implementation, Programming and Support for Macola Progression, Macola ES, Synergy, and Crystal Reports. Check out our Macola tools:
 
You have not instantiated objWord. Modify the line with CreateObject:
Set objWord=CreateObject("Word.Application")


combo
 
I did that and now I have "Run time error 429 - ActiveX component cannot create object".

Software Training, Implementation, Programming and Support for Macola Progression, Macola ES, Synergy, and Crystal Reports. Check out our Macola tools:
 
please post your most current code.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Private Sub btn1_click
Dim objDoc As Word.Document
Dim objWord As Word.Application

[red]CreateObject ("Word.Application")[/red]
Set objDoc = CreateObject("WordApplication")
Set objDoc = objWord.Documents.Open("C:\MyDirectory\12345.doc")

objDoc.PrintOut
objWord.Quit​
End Sub


Software Training, Implementation, Programming and Support for Macola Progression, Macola ES, Synergy, and Crystal Reports. Check out our Macola tools:
 

Code:
Private Sub btn1_click

   Dim objDoc As Word.Document
   Dim objWord As Word.Application

   Set [highlight #FCE94F]objWord[/highlight] = CreateObject("WordApplication")
   Set objDoc = objWord.Documents.Open("C:\MyDirectory\12345.doc")

   objDoc.PrintOut
   objWord.Quit
End Sub

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Shouldn’t that be:

Code:
Private Sub btn1_click

   Dim objDoc As [blue]Object[/blue]
   Dim objWord As [blue]Object[/blue]

   Set objWord = CreateObject("Word[highlight #FCE94F].[/highlight]Application")
   Set objDoc = objWord.Documents.Open("C:\MyDirectory\12345.doc")

   objDoc.PrintOut
   objWord.Quit
End Sub

???

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top