Solved

Error 462 using Word within Visual Basic to print envelop

Posted on 2003-11-03
6
760 Views
Last Modified: 2010-08-05
Here is the code:  
Receive error 462 when trying to print envelops using Word within VB 6.

Public Sub PrintEnvelopes()
    '**Error Handling***
    On Error GoTo ErrHandler
    Dim sSource As String
    sSource = "PrintEnvelopes"
    '**Error Handling***
   
    Dim i                       As Integer
   
    Dim oApp                    As Word.Application
    Dim oDoc                    As Word.Document
    Dim oWordTemplate           As Word.Document
   
    Dim oListItem               As ListItem
    Dim sDefenseAttyKey         As String
    Dim sDefenseAttyID          As String
    Dim oNode                   As MSXML2.IXMLDOMNode
    Dim oSection                As Word.Section
    Dim iSectionCount           As Integer
    Dim aAllSections()          As Boolean
   
    Dim sDefenseAttyName        As String
    Dim sTemp                   As String
    Dim sDefenseAttyAddress     As String
   
    Dim strMessage              As String
    Dim sFileName               As String
 
    Set oApp = GetCurrentWordInstance
    If oApp Is Nothing Then
         Set oApp = New Word.Application
    End If

    ' There must be an ACTIVE WORD document, to print envelope(s).
    Set oDoc = oApp.Documents.Add(msWordTemplate, False, wdNewBlankDocument, True)
    With oDoc
        If oDoc.ProtectionType <> wdNoProtection Then
            iSectionCount = oDoc.Sections.Count
            ReDim aProtectedSections(1 To iSectionCount)
            For i = 1 To iSectionCount
                aProtectedSections(i) = oDoc.Sections(i).ProtectedForForms
            Next i
            .Unprotect
        End If
        'Clear out the contents
        oDoc.Content.Delete
        oDoc.Activate
    End With
   'Set Envelope = CreateObject(Word.Envelope)
    i = 0
    Screen.MousePointer = vbHourglass
    For Each oListItem In lvSelect(LV_SELECTED).ListItems
        i = i + 1
        sDefenseAttyKey = oListItem.Key
        sDefenseAttyID = Replace(sDefenseAttyKey, S_ID, S_BLANK)
        Set oNode = GetXMLSingleNodeByXpath(moDocInfoDom, "//DefenseAttorney/Attorney[EmployeeID='" & sDefenseAttyID & "']")
        If oNode Is Nothing Then
            Err.Raise CUSTOM_ERR_NO_LOAD_WITNESS_XML, sSource, CUSTOM_ERR_NO_LOAD_WITNESS_XML_DESC
            Exit Sub
        End If
        'Get Witness Name and Address
        sDefenseAttyName = GetNameFMLS(oNode)
        sDefenseAttyAddress = GetAddressAAACSZWithLineFeedsA(oNode)
        sTemp = sDefenseAttyName & vbCr & sDefenseAttyAddress
        ActiveDocument.Envelope.PrintOut False, Address:=sTemp
    Next
   
    'Save the word template and delete it, so that user doesn't see the word document and freak out
    sFileName = CreateSubDocName()
    If gsWordDocumentsPath <> S_BLANK Then
       sFileName = gsWordDocumentsPath & "\" & sFileName
    End If
    oDoc.SaveAs sFileName
    oDoc.Close
    Kill sFileName
    Screen.MousePointer = vbDefault
   
    If i = 1 Then
        strMessage = "1 envelope printed"
    Else
        strMessage = i & " envelopes printed"
    End If
    MsgBox strMessage
   
CleanUp:
    Set oDoc = Nothing
    Screen.MousePointer = vbDefault
    If gbWordLaunchedByThisApp Then oApp.Quit
    Set oApp = Nothing
    Exit Sub
ErrHandler:
    If Err.Number = "462" Then
        MsgBox "Close this window. Save Case. Close CAIS. Try again to print envelope."
       
        sFileName = CreateSubDocName()
        If gsWordDocumentsPath <> S_BLANK Then
            sFileName = gsWordDocumentsPath & "\" & sFileName
        End If
        oDoc.SaveAs sFileName
        oDoc.Close
        Kill sFileName
        GoTo CleanUp
    End If
    Err.Raise Err.Number, sSource, Err.Description
    GoTo CleanUp
 
 
End Sub
0
Comment
Question by:dolphins_9
  • 3
6 Comments
 
LVL 5

Expert Comment

by:dc197
ID: 9674211
You mustn't run word in the background.
If you close a second instance of word, ALL instances (including the one inside your VB prog) are killed.

You can open a word doc but not close it.
This is a bug with Word.
0
 
LVL 5

Accepted Solution

by:
dc197 earned 250 total points
ID: 9674231
I would also use the following binding:


   Dim oApp                    As NEW  Word.Application
   Dim oDoc                    As Word.Document    
   Set oDoc = oApp.Documents.Add(msWordTemplate, False, wdNewBlankDocument, True)



This bypasses the GetCurrentWordInstance and should create a new word instance for each instance of your program.
Remeber to clean up
oDoc.Close
oApp.Quit
Set oApp = nothing
set odoc = nothing
0
 
LVL 5

Expert Comment

by:dc197
ID: 10164640
Dolphins, this is the answer.
I used to use word within my apps at work, but got so {{angry at }} the 462 errors that I do not use word any more.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now