Import Outlook Email Body Without Signature (1 Viewer)

Access or E

New member
Local time
Today, 16:47
Joined
Aug 29, 2022
Messages
12
Does anyone know how to do this? Whenever I import the email body, it includes the signature, which I don't want. The code using the Word bookmark does not work.

Code:
Dim applOutlook As Outlook.Application
Dim nsOutlook As Outlook.NameSpace
Dim cfOutlook As Outlook.Folder
Dim ifOutlook As Outlook.Folder
Dim AR_Folder As Outlook.Folder
Dim strAR_Body As String
Dim MSG As Outlook.MailItem

'Create a new instance of the Outlook application. Set the Application object as follows:
Set applOutlook = New Outlook.Application

'use the GetNameSpace method to instantiate (ie. create an instance) a NameSpace object variable, to access existing Outlook items. Set the NameSpace object as follows:
Set nsOutlook = applOutlook.GetNamespace("MAPI")

'assign the object variable cfOutlook to the default Contacts folder:
'Set cfOutlook = nsOutlook.GetDefaultFolder(olFolderContacts)
'MsgBox cfOutlook

'assign the object variable ifOutlook to the default Inbox folder:
Set ifOutlook = nsOutlook.GetDefaultFolder(olFolderInbox)
Set AR_Folder = ifOutlook.Folders("AR_Import")

Dim objDoc As Word.Document
Dim objBkm As Word.Bookmark




For Each MSG In AR_Folder.Items
    Set objDoc = MSG.GetInspector.WordEditor
    Set objBkm = objDoc.Bookmarks("_MailAutoSig")
    MSG.BodyFormat = olFormatHTML
    MSG.HTMLBody = "<HTML><BODY>" & strBody & "</BODY></HTML>" & MSG.HTMLBody

    
    If Not objBkm Is Nothing Then
        objBkm.Select
        objDoc.Windows(1).Selection.Delete
    End If
    
    
    Debug.Print MSG.Subject & vbCrLf & vbCrLf
    'MSG.Body = Replace(MSG.Body, Signature, "")
    MSG.Body = Left(MSG.Body, Len(MSG.Body) - SignatureLength)
    Debug.Print MSG.Body
    Debug.Print "Email to: " & MSG.To
    Debug.Print "Sender: " & MSG.Sender
    'Debug.Print MSG.SenderEmailAddress
    'Debug.Print MSG.BodyFormat
    'Debug.Print MSG.EntryID
    'Debug.Print MSG.HTMLBody
    Debug.Print MSG.ReceivedTime
    'Debug.Print MSG.RTFBody
    
Next
 
Obviously based on the method you attempted, you know that a signature is an actual part of the message body (unlike the To list or Subject line, which are distinct message entities). I THINK that what you actually tried to do, given your bookmark operation, is delete the bookmark, not the line. Which clearly isn't what you WANTED to do.

Assuming that the signature line can be found, I might try to determine the paragraph number. You do that by creating a range that starts with position 0 and ends with someplace on the line where you are interested. It MIGHT work with the bookmark as a position marker, I don't know for sure. The code I am using user is extracted OUT OF CONTEXT from a Genealogy app I wrote some time ago and is presented here solely to give you an idea to play with.

Code:
DIM wddoc = Word.Document
...
WhichPgh = wddoc.Range(0, {position of interest}.Range.End).Paragraphs.Count

That tells you an integer paragraph number for the position of interest by counting the paragraphs between 0 (the start of the document) and the place you provide as a position of interest.

Remember, for WORD, a paragraph is the content between consecutive hard NewLine markers, even if it is empty. Presumably, the thing you want to delete will be in that paragraph you just located. So if you have a paragraph number then the start and end of that paragraph can become a formal Range. Create a new range variable that contains the text of interest.

Code:
DIM WdRng = Word.Range
...
WdRng.SetRange Start:=wddoc.Paragraphs(lPgh).Range.Start, End:=wddoc.Paragraphs(lPgh).Range.End - 1

Once you have the range captured as a range variable, you can work with it to do things like deleting or erasing or other word-like things.
 
Thanks for the response Doc_Man. Is there any way of separating the signature from the email body without using Word? That was just a last ditch effort since I couldn't figure out how to do it with Access vba. Thanks in advance.
 
When I was sending emails out from Access, I had to split the signature file for a reason I cannot remember now, as it has been a few years.
If the email is html then I was looking for a code element and split on that.

Code:
    'Get the signature if it exists
    If Dir(strSigPath) <> "" Then
        strSignature = GetBoiler(strSigPath)
        intBody = InStr(strSignature, "<div class=WordSection1>")
        'intBody = InStr(strSignature, "<BODY>")
        strHeader = Left(strSignature, intBody + 24) ' 5
        strFooter = Mid(strSignature, intBody + 24) ' 6
    End If
...................

            .HTMLBody = strHeader & "<table border = '0' cellpadding = '5' cellspacing = '5'>"
............
            .HTMLBody = .HTMLBody & "</table>" & strFooter

This is the signature html file

1684501637275.png
 
Thanks for the response Doc_Man. Is there any way of separating the signature from the email body without using Word? That was just a last ditch effort since I couldn't figure out how to do it with Access vba. Thanks in advance.

Here is the problem: IF you are importing this and want to edit it, you have only two choices. Using a text processor (e.g. Word) is one of them. We just discussed that. You don't seem to like it.

The other solution is also one you didn't seem to like - VBA programming. Access is not primarily a word processing utility itself, so to modify a file you have to "roll your own" text editing via programming - which for Access means VBA. You COULD perhaps open the file. Then, using a VBA loop to do read/write operations, watch for whatever acts as a signature marker. Then don't do the write operation for that line or segment. As pointed out by Gasman, you COULD have an included literal HTML signature sequence or you can have an embedded reference to a signature file.

In either case, because the signature is part of the body whereas other elements are not (To:, CC:, BCC:, Subject:), it is not trivial unless you have a marker and the ability to edit. Either method is going to be highly tedious because for the signature, you are picking things apart.
 

Users who are viewing this thread

Back
Top Bottom