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 strongm 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 VB6

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
Hi guys, I don't post that often, but any help will be appreciated.

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)

This is a segment of code that I wrote (with some of the information blanked out) that seems to be hitting a error code '462'. This deals directly with VB6 and Microsoft Word. The users are on different platforms in Microsoft (including NT, 2000, and XP). The Microsoft Office Suites are different versions as well. I really need help. I can't seem to pinpoint to code error.

Also, I am using a spell checker module of code I got online.
'Copyright © 2005 by RobDog888 (VB/Office Guru™). All Rights reserved.

The spell checker works well, but does not work when someones Outlook Email Checker is open (tied to word). Any ideas if there is a way to fix this? Thanks.
 
If you get error(462):

This error happens if

not(objWrd Is nothing)

and

the ActiveX-Server (Word) is no longer available (terminated)

Check if word is running when the error occurs to verify this statement. There must be a reason why word quits too early.
 
Thanks SmallTalker. If you look at how I instantiate the objWord Object, I cast it as a Word.Application using CreateObject.

Also when I look up the '462' error, it was suggested from

That I wasn't declaring an object's method correctly. I have looked through my code and I don't see where that is happening. Also, the word application does not close, but the application I wrote halts after the error.
 
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.
 
Also, it doesn't get to the print job
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top