Results 1 to 5 of 5

Thread: [Outlook] [Resolved] Add Header and Footer to Attachment, then Print

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    6

    Resolved [Outlook] [Resolved] Add Header and Footer to Attachment, then Print

    I would like to create a macro script in Outlook that adds a Header and Footer to an Attachment and then prints it. Any Idea how to do this?


    Here's the process I want it to have:
    - User clicks a 'Print with Header/Footer' Button while Email is opened => a small window pops up with two fields (Header and footer) and Two buttons (Print and Cancel)
    -The Two fields can be changed, but default they should contain:
    -> Header Field: Email Address of the Sender and the Subject
    -> Footer Field: Maybe the Today's Date

    - Once 'Print' is clicked, the Two fields are set as the Header/Footer of the Attachment, given that the Attachment is a Doc, Pdf, or any other Document that supports a Header/Footer


    I know that is a lot, but I really hope someone can help me achieve this little Script
    Last edited by blinness; Dec 16th, 2012 at 11:12 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,906

    Re: [Outlook] Add Header and Footer to Attachment, then Print

    given that the Attachment is a Doc, Pdf, or any other Document that supports a Header/Footer
    easy enough to do for word or excel by automating the appropriate application, but for pdf you would need something like acrobat installed, unless it can be opened in word, as text or image, then add header and footer
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    6

    Re: [Outlook] Add Header and Footer to Attachment, then Print

    Quote Originally Posted by westconn1 View Post
    easy enough to do for word or excel by automating the appropriate application, but for pdf you would need something like acrobat installed, unless it can be opened in word, as text or image, then add header and footer
    how would you automate the appropriate application? could you give me a sample code?
    i looked around but wasn't able to find a good tutorial

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,906

    Re: [Outlook] Add Header and Footer to Attachment, then Print

    i looked around but wasn't able to find a good tutorial
    try the faq thread at the top of this forum

    you could either first save the attachment to disk as a file then open, or possibly paste the attachment into an open new document, then edit and print, save or not as required
    for a .doc attachment, try doing it manually first, while recording a macro in word

    how would you automate the appropriate application? could you give me a sample code?
    for .Doc saved to disk
    Code:
    set wd = createobject("word.application")
    set doc = wd.documents.open("somepath\filename.doc")    'where ever it was saved to
    With doc
        .Sections(1).Headers(1).Range.Text = "somestring"
        .Sections(1).Footers(1).Range.Text = format(now, "dd/mm/yyy")
        .PrintOut
        .Save   'if required
        .Close
    End With
    ' if processing multiple emails at one time you can loop from here, to open the next, you may require to save the attachments within a loop of mail items to be processed
    you may well desire to format the headers and footers appropriately, the date can be entered into the footer as a header footer field, or a string ( as above), the header can string can be concantenated from the mail item properties

    the above code has not been tested at all
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2012
    Posts
    6

    Re: [Outlook] Add Header and Footer to Attachment, then Print

    @westconn1, thank you very much. The code you provided worked perfectly. I was finally able to create the script i wanted to do ^^

    That's the code for the Print function:

    TEMP_PATH is a global variable containing the Path to the temp folder, where the File will be saved

    Reference to "Microsoft Word __._ Object Library" is required (In my case it was 14.0)

    PrintAttBox is a form I created, so the User can modify the Inputs, like the Header and the Footer

    Code:
    Sub PrintWithHeader(fFullPath As String)
    
        Dim wd As Word.Application
        Dim doc As Word.Document
        
        'MsgBox (PrintAttBox.DocName.Caption)
        
        Set wd = CreateObject("Word.Application")
        Set doc = wd.documents.Open(TEMP_PATH & PrintAttBox.DocEmail.Caption & "_" & PrintAttBox.DocPos & "_" & PrintAttBox.DocName)  'where is was saved
        With doc
            .Sections(1).Headers(1).Range.Text = PrintAttBox.DocHeader.Text
            .Sections(1).Footers(1).Range.Text = PrintAttBox.DocFooter.Text
            .PrintOut
            .Save 'if required
            .Close
        End With
        
        MsgBox ("The Document was saved successfully: " & TEMP_PATH & PrintAttBox.DocEmail.Caption & "_" & PrintAttBox.DocPos & "_" & PrintAttBox.DocName)
    
    End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width