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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Late Binding of a MS Word Print Program

Status
Not open for further replies.

dashen

Programmer
Joined
Jul 14, 2005
Messages
233
Location
US
Code:
Private Sub printMsg()
    Dim objWrd As Object
    Dim objDoc As Object
    Dim sln As Object
    
    Set objWrd = CreateObject("Word.Application")

    'Setting Overtype value to False is very impotant when using selection
    objWrd.Options.Overtype = False

    Select Case objWrd.Version
        Case "9.0", "10.0", "11.0"
            Set objDoc = objWrd.Documents.Add(, , 1, True)
        Case "8.0"
            Set objDoc = objWrd.Documents.Add
        Case Else
            MsgBox "Unsupported Version of Word.", vbOKOnly + vbExclamation, "Install New Version"
            Exit Sub
    End Select

    Set sln = objWrd.Selection

    objDoc.Activate
   
    With sln
        .Paragraphs.Alignment = 0
        .Font.Name = "Arial"
        
        .Font.Size = cboFontSize.Text
                
        .TypeParagraph
        .BoldRun
        .TypeText ("<info>")
        .BoldRun
        .TypeText (label1.Caption)
        .ItalicRun
        .TypeText ("<moreinfo>")
        .ItalicRun
        
    End With
    
    objWrd.Visible = True
    objWrd.PrintOut
    
    Set sln = Nothing
    
    objDoc.Close (False)
    
    Set objDoc = Nothing
    
    MsgBox "Message Successfully Printed", vbOKOnly, "Print Successful"

End Sub

For some reason I am having issues with a piece of code which is very common to this. I am also having problems with a Spell Checker using Word as well, but I will post another thread for that.

This one throws a '462' error which means that an instance or a method of Word is not being called correctly or something, but I can not find it in the program.

I don't close the Word Application after use because the user may be using the application for other things. Would this be the problem and how would I get around it. Thanks in advanced.
 
Have you stepped through the code to see on which line the error occurs?
 
The error does not occur on my computer. This solution is already rolled out in the department. The error occurs sporadically and is hard to reproduce so it is very difficult to pinpoint.
 
<I don't close the Word Application after use because the user may be using the application for other things>

I don't think you need worry about that because this is an instance of Word your app has created. It should be independent of any instance of Word that is already open. (Your concern would be warranted if you had used GetObject rather than CreateObject.) I'm a little suprised you bother to make the instance of Word visible. I'm not saying this will solve your main problem but peronally I would try giving it a objWrd.Quit at the end.

 
I will let you know if that works
 
It would be good to introduce some error handling using Erl and plenty of line numbers to find out exactly where the error occurs (when it occurs).

I notice also;
your objWrd.Documents.Add(, , 1, True)

the 1 in this suggests the new document will be a wdNewWebPage; is that what you require? The constant required for a wdNewBlankDocument is 0 and zero is the default. So given that you may not need anything visible you can dispense with the () and just use objWrd.Documents.Add for any Word.version =>8.
 
VERY VERY INTERESTING!!! I will look into that
 
I've been testing your code and it seems to work, however I've beaten it down into this;

Private Sub printMsg()

Dim objWrd As Object

Set objWrd = CreateObject("Word.Application")

With objWrd
If Val(.Version) >= 8 Then
.Options.Overtype = False
.Documents.Add
With .Selection
.Paragraphs.Alignment = 0
.Font.Name = "Arial"
.Font.Size = cboFontSize.Text
.TypeParagraph
.BoldRun
.TypeText ("<info>")
.BoldRun
.TypeText (Label1.Caption)
.ItalicRun
.TypeText ("<moreinfo>")
.ItalicRun
End With
.PrintOut
.ActiveDocument.Close (False)
MsgBox "Message Successfully Printed", vbOKOnly, "Print Successful"
Else
MsgBox "Unsupported Version of Word.", vbOKOnly + vbExclamation, "Install New Version"
End If
.Quit
End With
Set objWrd = Nothing

End Sub

This may not solve your problem but its shorter and I think it may be a little more robust.

regards Hugh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top