Solved

Setting Header of the Word Document with vba code

Posted on 2011-02-15
3
342 Views
Last Modified: 2012-05-11
Dear all,

I wish to design a macro for setting the header of the document with the code below but the result ended up with all 3 lines on the left. Can any help to resolve the problem?

Line 1 on the Right
Line 2 on the Left
Line 3 on the Left

Thanks very much

   

With ActiveDocument.sections(1).Headers(wdHeaderFooterPrimary).Range
    
        .Text = "Line 1 on the Right" & Chr(10)
        .Paragraphs.Alignment = wdAlignParagraphRight
    
        .InsertAfter "Line 2 on the Left" & Chr(10)
        .Paragraphs.Alignment = wdAlignParagraphLeft
    
        .InsertAfter "Line 2 on the Left"
        .Paragraphs.Alignment = wdAlignParagraphLeft
    
    
    End With

Open in new window

0
Comment
Question by:Lawlords
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
Comment Utility
Try as below ... setting the cursor after the text inserted at the rignt.

Chris
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    
    .Paragraphs.Alignment = wdAlignParagraphRight
    .Text = "Line 1 on the Right" & Chr(10)
    .Collapse wdCollapseEnd

    .Paragraphs.Alignment = wdAlignParagraphLeft
    .InsertAfter "Line 2 on the Left" & Chr(10)
    .InsertAfter "Line 2 on the Left"
    
    
End With

Open in new window

0
 

Author Closing Comment

by:Lawlords
Comment Utility
Thanks very much Chris, you gave me a great help.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
Word can be very finicky with the methods used for populating text so you are not alone, (I struggle too).

Thank you for the prompt closure and grade.

Chris
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now