ErrHandler (32001 & 32002)

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

ErrHandler (32001 & 32002)

Post by D Willett »

I can't trap the error 32001 in this code as my errhandler doesn't trap it. I also get error 32002, how can I trap both of them?

Code: Select all

Private Sub cmdSend_Click()
    Dim oWord As Object
    Dim oDoc As Object
    Dim strPath As String
    strPath = "C:\MM-Utilities\Order.doc"
    Dim WFrom As String
    
    WFrom = GetComputerName
    frmWait.Show
    Set oWord = CreateObject("word.application")
    Set oDoc = oWord.documents.Add("C:\MM-Utilities\Order.dot")

    With oWord.Selection.Find
        .Execute FindText:="#OrderNo#", ReplaceWith:=Me.Text1.Text & "   From: " & WFrom, Replace:=2
        .Execute FindText:="#Vehicle#", ReplaceWith:=Me.Text2.Text, Replace:=2
        .Execute FindText:="#Registration#", ReplaceWith:=Me.Text3.Text, Replace:=2
        .Execute FindText:="#Customer#", ReplaceWith:=Me.Text4.Text, Replace:=2
        .Execute FindText:="#DateOnSite#", ReplaceWith:=Me.Text5.Text, Replace:=2
        .Execute FindText:="#CompletionDate#", ReplaceWith:=Me.Text6.Text, Replace:=2
        .Execute FindText:="#DescriptionOfWorkRequired#", ReplaceWith:=Me.RichTextBox1.Text, Replace:=2
        .Execute FindText:="#Picture#"
        oWord.Selection.InlineShapes.AddPicture FileName:=Me.Text7.Text

    End With
    oDoc.Protect 2, , "mfg"
    oDoc.SaveAs "C:\mm-Utilities\Order.Doc"
    oDoc.Close False
    Set oDoc = Nothing
    oWord.NormalTemplate.Saved = True
    oWord.Quit
    Set oWord = Nothing
    Unload frmWait
    
    MAPISession1.SignOn
    With MAPIMessages1
        .SessionID = MAPISession1.SessionID
        .Compose
        .MsgSubject = "Order From " & CompName
        .MsgNoteText = "Official M&M Vehicle Repairs Ltd Order"
        .AttachmentPathName = strPath
        .Send True
    End With
exitHandler:
   On Error GoTo ErrHandler
   MAPISession1.SignOff
   Exit Sub

ErrHandler:
   If Err.Number = 32001 Then
    Resume Next
    End If
   Resume exitHandler
   
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ErrHandler (32001 & 32002)

Post by HansV »

You currently only trap errors in the exitHandler section, not in the main part of your procedure. Is that what you intended?
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: ErrHandler (32001 & 32002)

Post by D Willett »

Not sure if I see the break in the loop there Hans, I thought I had it covered as it is in the exithandler.
The problem arises when a mail is created then the user cancels it. No problem with sending.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ErrHandler (32001 & 32002)

Post by HansV »

Perhaps you can modify the error handling part in the following version to suit your needs:

Code: Select all

Private Sub cmdSend_Click()
    Dim oWord As Object
    Dim oDoc As Object
    Dim strPath As String
    Dim WFrom As String

    On Error GoTo ErrHandler
    strPath = "C:\MM-Utilities\Order.doc"
    WFrom = GetComputerName
    frmWait.Show
    Set oWord = CreateObject("word.application")
    Set oDoc = oWord.documents.Add("C:\MM-Utilities\Order.dot")

    With oWord.Selection.Find
        .Execute FindText:="#OrderNo#", ReplaceWith:=Me.Text1.Text & "   From: " & WFrom, Replace:=2
        .Execute FindText:="#Vehicle#", ReplaceWith:=Me.Text2.Text, Replace:=2
        .Execute FindText:="#Registration#", ReplaceWith:=Me.Text3.Text, Replace:=2
        .Execute FindText:="#Customer#", ReplaceWith:=Me.Text4.Text, Replace:=2
        .Execute FindText:="#DateOnSite#", ReplaceWith:=Me.Text5.Text, Replace:=2
        .Execute FindText:="#CompletionDate#", ReplaceWith:=Me.Text6.Text, Replace:=2
        .Execute FindText:="#DescriptionOfWorkRequired#", ReplaceWith:=Me.RichTextBox1.Text, Replace:=2
        .Execute FindText:="#Picture#"
        oWord.Selection.InlineShapes.AddPicture Filename:=Me.Text7.Text
    End With
    oDoc.Protect 2, , "mfg"
    oDoc.SaveAs "C:\mm-Utilities\Order.Doc"
    oDoc.Close False
    Set oDoc = Nothing
    oWord.NormalTemplate.Saved = True
    oWord.Quit
    Set oWord = Nothing
    Unload frmWait

    MAPISession1.SignOn
    With MAPIMessages1
        .SessionID = MAPISession1.SessionID
        .Compose
        .MsgSubject = "Order From " & CompName
        .MsgNoteText = "Official M&M Vehicle Repairs Ltd Order"
        .AttachmentPathName = strPath
        .Send True
    End With

ExitHandler:
    On Error Resume Next
    MAPISession1.SignOff
    Exit Sub

ErrHandler:
    Select Case Err.Number
        Case 32001
            Resume Next
        Case 32002
            ' your code here
        Case Else
            MsgBox Err.Description, vbExclamation
            Resume ExitHandler
    End Select
End Sub
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: ErrHandler (32001 & 32002)

Post by D Willett »

Hi Hans

Not much luck, doesn't fix the error and still returns 32001... searched everywhere on Google.
The users may have to just put up with it I'm afraid !!
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ErrHandler (32001 & 32002)

Post by HansV »

That's strange - even when you're automating another application, error handling should work.
Make sure that you haven't set Error Trapping to "Break on All Errors" in the General tab of the Options dialog.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: ErrHandler (32001 & 32002)

Post by agibsonsw »

I've been reading around this; although I didn't find a full solution I can offer a couple of suggestions.

Error 32002 can arise if the .MsgNoteText is empty. Although you are supplying this text, it may not be registering in sync with your automation code. Try moving this statement higher-up. If not, you could step through the code; this will slow everything down and, if it works, indicate that the problem is a timing issue. If it's a timing issue then you might consider introducing artificial delays/loops (or DoEvents calls) within your code.

Specifying explicit Attachment index/position information may also help.

Code: Select all

Dim intDummy As Integer	'to discard DoEvents() integer
.Compose
.MsgNoteText = "Official M&M Vehicle Repairs Ltd Order"
.MsgSubject = "Order From " & CompName
.AttachmentIndex = 0
.AttachmentPosition = 0
.AttachmentPathName = strPath
intDummy = DoEvents()
.Send True
I suspect that DoEvents() can help with this kind of issue. But, as I mention, these are just some suggestions to try.. :cheers:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: ErrHandler (32001 & 32002)

Post by D Willett »

Hi. Thanks for the update code, it's still returning the 32001 error ( code attached ) and the Break on all errors isn't selected either.
So this is really puzzling to say the least lol.
I'll leave the code here in case someone see's a different angle on this. :scratch:

Code: Select all

Private Sub cmdSend_Click()
    Dim oWord As Object
    Dim oDoc As Object
    Dim strPath As String
    strPath = "C:\MM-Utilities\Order.doc"
    Dim WFrom As String
    Dim intDummy As Integer   'to discard DoEvents() integer
    
    WFrom = GetComputerName
    frmWait.Show
    Set oWord = CreateObject("word.application")
    Set oDoc = oWord.documents.Add("C:\MM-Utilities\Order.dot")

    With oWord.Selection.Find
        .Execute FindText:="#OrderNo#", ReplaceWith:=Me.Text1.Text & "   From: " & WFrom, Replace:=2
        .Execute FindText:="#Vehicle#", ReplaceWith:=Me.Text2.Text, Replace:=2
        .Execute FindText:="#Registration#", ReplaceWith:=Me.Text3.Text, Replace:=2
        .Execute FindText:="#Customer#", ReplaceWith:=Me.Text4.Text, Replace:=2
        .Execute FindText:="#DateOnSite#", ReplaceWith:=Me.Text5.Text, Replace:=2
        .Execute FindText:="#CompletionDate#", ReplaceWith:=Me.Text6.Text, Replace:=2
        .Execute FindText:="#DescriptionOfWorkRequired#", ReplaceWith:=Me.RichTextBox1.Text, Replace:=2
        .Execute FindText:="#Picture#"
        oWord.Selection.InlineShapes.AddPicture FileName:=Me.Text7.Text

    End With
    oDoc.Protect 2, , "mfg"
    oDoc.SaveAs "C:\mm-Utilities\Order.Doc"
    oDoc.Close False
    Set oDoc = Nothing
    oWord.NormalTemplate.Saved = True
    oWord.Quit
    Set oWord = Nothing
    Unload frmWait
    
    MAPISession1.SignOn
    With MAPIMessages1
        .SessionID = MAPISession1.SessionID
        .Compose
.MsgNoteText = "Official M&M Vehicle Repairs Ltd Order"
.MsgSubject = "Order From " & CompName
.AttachmentIndex = 0
.AttachmentPosition = 0
.AttachmentPathName = strPath
intDummy = DoEvents()
.Send True
        On Error GoTo ErrHandler
        
    End With
    
exitHandler:
    On Error Resume Next
    MAPISession1.SignOff
    Exit Sub

ErrHandler:
    Select Case Err.Number
        Case 32001
        MsgBox "Email Cancelled"
            Resume Next
        Case 32002
        MsgBox "Email Error"
            Resume Next
        Case Else
            MsgBox Err.Description, vbExclamation
            Resume exitHandler
    End Select
  
End Sub
Cheers ...

Dave.

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: ErrHandler (32001 & 32002)

Post by D Willett »

Fixed it !!!!!!!!!!!!!!

Using the above code it seems the error lies in the routine above the MapiSession1.SignOn.
I've looked at other code that works within my project which allows me to cancel an email in the same way.
Printing both routines and studying side by side I set out to make a fresh framework around the main workings of the routine.
Got it in the first change. Took the "On Error GoTo ErrHandler" out and placed on the first line of code.. fixed instantly ... :clapping:
So thanks to you guy's and making me look further into the problem.

Code: Select all

Private Sub cmdSend_Click()
On Error GoTo ErrHandler ' ERROR TRAP HERE...............
    Dim oWord As Object
    Dim oDoc As Object
    Dim strPath As String
    strPath = "C:\MM-Utilities\Order.doc"
    Dim WFrom As String
    Dim intDummy As Integer   'to discard DoEvents() integer
    
    WFrom = GetComputerName
    frmWait.Show
    Set oWord = CreateObject("word.application")
    Set oDoc = oWord.documents.Add("C:\MM-Utilities\Order.dot")

    With oWord.Selection.Find
        .Execute FindText:="#OrderNo#", ReplaceWith:=Me.Text1.Text & "   From: " & WFrom, Replace:=2
        .Execute FindText:="#Vehicle#", ReplaceWith:=Me.Text2.Text, Replace:=2
        .Execute FindText:="#Registration#", ReplaceWith:=Me.Text3.Text, Replace:=2
        .Execute FindText:="#Customer#", ReplaceWith:=Me.Text4.Text, Replace:=2
        .Execute FindText:="#DateOnSite#", ReplaceWith:=Me.Text5.Text, Replace:=2
        .Execute FindText:="#CompletionDate#", ReplaceWith:=Me.Text6.Text, Replace:=2
        .Execute FindText:="#DescriptionOfWorkRequired#", ReplaceWith:=Me.RichTextBox1.Text, Replace:=2
        .Execute FindText:="#Picture#"
        oWord.Selection.InlineShapes.AddPicture FileName:=Me.Text7.Text

    End With
    oDoc.Protect 2, , "mfg"
    oDoc.SaveAs "C:\mm-Utilities\Order.Doc"
    oDoc.Close False
    Set oDoc = Nothing
    oWord.NormalTemplate.Saved = True
    oWord.Quit
    Set oWord = Nothing
    Unload frmWait
    
    MAPISession1.SignOn
    With MAPIMessages1
        .SessionID = MAPISession1.SessionID
        .Compose
        .MsgNoteText = "Official M&M Vehicle Repairs Ltd Order"
        .MsgSubject = "Order From " & CompName
        .AttachmentIndex = 0
        .AttachmentPosition = 0
        .AttachmentPathName = strPath
        intDummy = DoEvents()
        .Send True
        Unload Me

        
    End With
    
exitHandler:
    On Error Resume Next
    MAPISession1.SignOff
    Exit Sub

ErrHandler:
    Select Case Err.Number
        Case 32001
        MsgBox "Email Cancelled", vbInformation, "MIC"
            Resume Next
        Case 32002
        MsgBox "Email Error", vbInformation, "MIC"
            Resume Next
        Case Else
            MsgBox Err.Description, vbExclamation
            Resume exitHandler
    End Select
  
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ErrHandler (32001 & 32002)

Post by HansV »

Hi Dave,

That's what I suggested in Post=66912 higher up in this thread...
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: ErrHandler (32001 & 32002)

Post by D Willett »

:bif:
So you did........ I hadn't noticed it !! I only saw the select statements at the end of the routine.... would have saved me a couple of sleepless nights..
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ErrHandler (32001 & 32002)

Post by HansV »

Oh dear... :snore: :yawn:
Best wishes,
Hans