dolphins_9
asked on
Error 462 using Word within Visual Basic to print envelop
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(msWordT emplate, 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).Protected ForForms
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).List Items
i = i + 1
sDefenseAttyKey = oListItem.Key
sDefenseAttyID = Replace(sDefenseAttyKey, S_ID, S_BLANK)
Set oNode = GetXMLSingleNodeByXpath(mo DocInfoDom , "//DefenseAttorney/Attorne y[Employee ID='" & 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 = GetAddressAAACSZWithLineFe edsA(oNode )
sTemp = sDefenseAttyName & vbCr & sDefenseAttyAddress
ActiveDocument.Envelope.Pr intOut 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
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(msWordT
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).Protected
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).List
i = i + 1
sDefenseAttyKey = oListItem.Key
sDefenseAttyID = Replace(sDefenseAttyKey, S_ID, S_BLANK)
Set oNode = GetXMLSingleNodeByXpath(mo
If oNode Is Nothing Then
Err.Raise CUSTOM_ERR_NO_LOAD_WITNESS
Exit Sub
End If
'Get Witness Name and Address
sDefenseAttyName = GetNameFMLS(oNode)
sDefenseAttyAddress = GetAddressAAACSZWithLineFe
sTemp = sDefenseAttyName & vbCr & sDefenseAttyAddress
ActiveDocument.Envelope.Pr
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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.