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!

Outlook 2007 - Sub Grabbing Wrong Email Body

Status
Not open for further replies.

Googleplexity

Programmer
Nov 30, 2011
5
US
Basically to make a long story short this project is giving me grey hair fast with strange errors at every turn. Even on the most simple of tasks. I've recreated several forms using Infopath 2007. These forms are sent thru, and received in Outlook 2007. I have a rule in OL07 that will kick off a pretty simple script that will take the email, copy out attactments, and the infopath form body ([Copy Full Body from Sue Mosher][1]) into a new Task item. Depending on the answers I get for this question then I'll ask several others. But right now this is my problem:

I receive a new email. Rule kicks in and the script takes over, if I have no watches in place I get the body from the next most recent email in my inbox. If my inbox is empty I get no body at all. If step through the code it goes off without a hitch. I know the correct OL object is being passed to the copy full body routine but I'm not familiar enough with object models of either outlook or word to know where things are failing. Here is Sue's code:

Code:
    Sub CopyFullBody(sourceItem As Object, targetItem As Object)
      Dim objDoc As Word.Document
      Dim objSel As Word.Selection
      Dim objDoc2 As Word.Document
      Dim objSel2 As Word.Selection
      On Error Resume Next
        ' get a Word.Selection from the source item
      Set objDoc = sourceItem.GetInspector.WordEditor
      If Not objDoc Is Nothing Then
          Set objSel = objDoc.Windows(1).Selection
          objSel.WholeStory
          objSel.Copy
          Set objDoc2 = targetItem.GetInspector.WordEditor
              If Not objDoc2 Is Nothing Then
                  Set objSel2 = objDoc2.Windows(1).Selection
                  objSel2.PasteAndFormat wdPasteDefault
              Else
                MsgBox "Could not get Word.Document for " & _
                targetItem.Subject
              End If
      Else
        MsgBox "Could not get Word.Document for " & _
        sourceItem.Subject
      End If
      Set objDoc = Nothing
      Set objSel = Nothing
      Set objDoc2 = Nothing
      Set objSel2 = Nothing
    End Sub

I need this to grab the body from the incoming email (I pass that object in another bit of code). But it seems to be getting the body either of the next most recent, or it just occured to me, the mailitem that is selected.

If you all have any advice I'd be very appreciative, or have any questions, I'll answer as soon as I can.

Thanks.
 
If step through the code it goes off without a hitch.
You may need to force the code to wait at certain points to make sure some prior process is complete before running the next. I've had to do this on occasion. If you're linking in the Excel appliction, you can use the Excel.Application.Wait function. Otherwise, you can build (or find online) a custom fWait function - I've seen a few, and used both methods myself - Excel's function, and custom functions..

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
kjv1611,

Yeah I've unfortunately been through all that, I've since abandonded the approach I was taking in favor a simpler path (to outlook it seems anyway). I had a great discussion with an outlook mvp and forum mod over on the msdn forums ( sorry I don't know the tag to make it hyperlink offhand
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top