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

Integrating Word into an Application 2

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
Code:
Dim objWrd As Object
    Set objWrd = CreateObject("Word.Application")
    'Dim objWrd As New Word.Application
    Dim objDoc As Word.Document
    Dim sln As Word.Selection

    '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 = wdAlignParagraphLeft
        .Font.Name = "Arial"
        .Font.Size = cboFontSize.Text + 2
        
        .BoldRun
        .TypeText (Chr(9) & lblDefName.Caption)
        .BoldRun
        
        .TypeParagraph
        .TypeParagraph
        
        .Font.Size = cboFontSize.Text
        
        '*NEWLINE*
        
        .BoldRun
        .TypeText (Chr(9) & " ")
        .BoldRun
        .TypeText (txtSPN.Text)
        
        .BoldRun
        .TypeText (" ")
        .BoldRun
        
        Dim i As Integer
        For i = 0 To lstCause.ListCount
            .TypeText (lstCause.List(i) & " ")
        Next i
        
        .TypeParagraph
        
        '*NEWLINE*
                
        .BoldRun
        .TypeText (Chr(9) & " ")
        .BoldRun
        .TypeText (txtDateAct.Text)
        
        .BoldRun
        .TypeText (" ")
        .BoldRun
        .TypeText (cboActType.Text)
        
        .BoldRun
        .TypeText (" ")
        .BoldRun
        .TypeText (txtLOS_lbl.Caption)
        
        .TypeParagraph
        
        .TypeParagraph
        
        '*NEWLINE*
        '*NEWLINE*
        
        .BoldRun
        .TypeText (" ")
        .BoldRun
        .TypeParagraph
        .TypeText (txtNotes.Text)
        .TypeParagraph
        
        If (txtRptDate <> vbNullString) Then
            .BoldRun
            .TypeText (" ")
            .BoldRun
            .TypeText (txtRptDate.Text)
            
            .TypeText ("    ")
        
            .BoldRun
            .TypeText (" ")
            .BoldRun
            .TypeText (txtRptTime.Text)
        End If
        
        .TypeParagraph
        .BoldRun
        .TypeText (" ")
        .BoldRun
        .TypeText (lblCSO.Caption)
        .ItalicRun
        .TypeText (" ")
        .ItalicRun
        
    End With
    
    objWrd.Visible = True
    objWrd.PrintOut
    objWrd.Visible = False
    MsgBox "Successfully Printed", vbOKOnly, "Print Successful"
    
    objDoc.Close (False)
    objWrd.Quit (False)

Anyone know where the '462' runtime error is occurring in this program?

The users are on different platforms (NT, 2000, XP) with different versions of Microsoft Word.
 
Cheesy man, doesn't help sorry.

I errror happened sporadically, and I can't duplicate the error on the test server. I am guessing the profile, or platform is okay.
 
Is it not running at all?

Does it run some of it? If so, where does it stop? Does it stop at the same place?

It looks like information is being picked up from a userform. What application is this originating from? What is the status of that userform?

The use of all those Selection instructions is very strange.

Gerry
My paintings and sculpture
 
The application is something I wrote in house for business needs.

It stopped after creation of the document, and halts the application. The word document is still open, so it never gets to quit.
 
Well, you have:
Code:
    Set objWrd = CreateObject("Word.Application")
where you make the instance of Word. This appears to be working, yes? You DO have an instance of Word.

You have:
Code:
        Case "9.0", "10.0", "11.0"
            Set objDoc = objWrd.Documents.Add(, , 1, True)
        Case "8.0"
            Set objDoc = objWrd.Documents.Add
where you set a Document object. This appears to be working, yes? You DO have a document?

Are you saying THIS is where it is halting? You have a document, but it remain blank? NONE of the text your code is putting in, is put in?

Gerry
My paintings and sculpture
 
The document comes out fine, it just doesn't print and will throw this '462' error for some reason on the print.

It could be tied to how I instantiate my documents because of the various version of Office on different platforms, but the weird thing is that the code runs sometimes and sometimes it doesn't and throws the '462'.

The document is created with all the appropriate details, it just throws the error, the application will halt, and the document doesn't print.

It seems like no one else seems to see any issues with the code either. I am getting really frustrated trying to figure out where in the code this is messed up. I have written other programs at different companies using similar code and thigns ran smoothly. This is really annoying.
 
BTW. The program references Microsoft Word 11.0 library.

10 and 9 aren't referenced, but this should be taken care of by my version controller. If I was to reference the other libraries, I would have to go to other platforms with those versions of Word and install Visual Studio and reference those libraries as well, but then the unrecognized libraries would be dropped right?

Is it because of referencing, or is it something else?
 
If your code must deal with different versions of Word then use late binding and don't reference any word library.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

Can you show me how to use Late Binding if I shouldn't use a reference? Thanks.

dashen
 
Dim objWrd As Object
Set objWrd = CreateObject("Word.Application")
Dim objDoc As Object
Dim sln As Object

Replace all wdXXX constants with their value.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Further note: use the Option Explicit instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
As usual PH has solid advice. Use late-binding, and perhaps remove the reference to Word 11.0.

BTW: even if you did want to be able to reference other version libraries, you certainly do not need to install Visual Studio to do so.

Gerry
My paintings and sculpture
 
Hey PHV,

Thanks for the advice.

wdXXX constants? What do you mean by this?
 
What do you mean by this?
Replace this:
.Paragraphs.Alignment = wdAlignParagraphLeft
with this:
.Paragraphs.Alignment = 0 ' 0=wdAlignParagraphLeft

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Stars for you guys!!!! Thanks for all the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top