Share via

Visual Basic - including the document header in macro

Anonymous
2014-01-31T17:17:03+00:00

I am trying to send a Word document as an email without it being an attachment.  I am using Visual Basic to create the macro.  When I use the macro, it copies the document text perfectly into a new email message.  However, it is losing the header and page border.  It seems the Selection.WholeStory is not taking the header and border.   I have tried to add those selections into the macro, but I'm not doing it right.  Can anyone help with me by telling me how I need to add those items to the macro?  Thank you!

Sub SendDocAsMail()

Dim oOutlookApp As Outlook.Application

Dim oItem As Outlook.MailItem

On Error Resume Next

'Start Outlook if it isn't running

Set oOutlookApp = GetObject(, "Outlook.Application")

If Err <> 0 Then

    Set oOutlookApp = CreateObject("Outlook.Application")

End If

'Create a new message

Set oItem = oOutlookApp.CreateItem(olMailItem)

'Allow the user to write a short intro and put it at the top of the body

Dim msgIntro As String

msgIntro = InputBox("Write a short intro to put above your default " & _

            "signature and current document." & vbCrLf & vbCrLf & _

            "Press Cancel to create the mail without intro and " & _

            "signature.", "Intro")

'Copy the open document

Selection.WholeStory

Selection.Copy

Selection.End = True

'Set the WordEditor

Dim objInsp As Outlook.Inspector

Dim wdEditor As Word.Document

Set objInsp = oItem.GetInspector

Set wdEditor = objInsp.WordEditor

'Write the intro if specified

Dim i As Integer

If msgIntro = IsNothing Then

    i = 1

    'Comment the next line to leave your default signature below the document

    wdEditor.Content.Delete

Else

    'Write the intro above the signature

    wdEditor.Characters(1).InsertBefore (msgIntro)

    i = wdEditor.Characters.Count

    wdEditor.Characters(i).InlineShapes.AddHorizontalLineStandard

    wdEditor.Characters(i + 1).InsertParagraph

    i = i + 2

End If

'Place the current document under the intro and signature

wdEditor.Characters(i).PasteAndFormat (wdFormatOriginalFormatting)

'Display the message

oItem.Display

'Clean up

Set oItem = Nothing

Set oOutlookApp = Nothing

Set objInsp = Nothing

Set wdEditor = Nothing

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2014-01-31T22:56:21+00:00

Although you can retrieve a document's headers & footers easily enough, adding those to an email body is fraught with difficulty, as an email message doesn't support anything like a Word page layout. Accordingly, you'd have to either have the header appear just once (presumably at the start of the inserted document) or manually insert it into the email text wherever you want it to appear - which could also result in it appearing in odd places when printed by the recipient. Then there's the matter of a Word document having as many as three headers & footers per Section (of which a document can have many) that you might need to copy.

That said, the code to copy both the body content and the document's 1st Section primary header would look something like:

'Copy the open document

ActiveDocument.Range.Copy

'Insert into email body

...

'Place the current document under the intro and signature

wdEditor.Characters(i).PasteAndFormat (wdFormatOriginalFormatting)

'Copy the document's 1st Section primary header

ActiveDocument.Sections.First.Headers(wdHeaderFooterPrimary).Range.Copy

'Insert into email body

The first part of the above code would replace your:

'Copy the open document

Selection.WholeStory

Selection.Copy

Selection.End = True

I'll leave it to you to sort out where you'll insert the header. If you're inserting it only once, you might want to copy & paste it first, then do the body content.

Was this answer helpful?

0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-06-07T00:36:26+00:00

    Thank you!

    Was this answer helpful?

    0 comments No comments
  2. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2014-06-06T22:49:49+00:00

    Doesn't everyone have the ability to save a Word 2010 document as a .pdf?  Another question on this topic: If the screenshot is a little blurry, doesn't that blurriness disappear in the prinout?

    The issue discussed in this thread was how to include the document header/footer as part of the email body. That has nothing to do with attaching a file. And, if you're going to attach one, it really doesn't matter whether it's in PDF format.

    As for your question, that depends on the image format and printer. EPS images often display a low-res version but contain a hi-res version for printing on postscript-enabled printers. Otherwise, what you see is what you'll get.

    Was this answer helpful?

    0 comments No comments