none
How do I change paragraph Format to left justify for certain line in the header? RRS feed

  • Question

  • Hi,

    I am trying to make a header (primary header) in word document to make first line in the header as center and second line in header as left justified. I was using range to do that but when I change the second line to left it moves first line too. I could not figure it why. My primary header has "This is first line in the header" + vbcr + " This is the second line in the header"  I pass " This is the second line in the header" as parameter.  Here is my code

    with wordDoc

    .ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView

    .ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader

    MyRange = .ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range

    intLoc = Instr(1, UCase(MyRange.text), UCase(targetString))-1

    If intLoc >= 0 then

    With MyRange

        .Start = intLoc

        .End = intLoc + Len(TargetString)

        .Select()

        .ParagraphFormat.Alignment = wdAlignParagraphLeft

    End with

    End if

    Thanks in advance!

    

    Friday, September 21, 2012 1:02 PM

Answers

  • How about if you do it more like this:

    with wordDoc
    MyRange = .Sections(1).Headers(wdHeaderFooterPrimary).Range
    MyRange.Paragraphs(2).Alignment = wdAlignParagraphLeft
    End with


    Cindy Meister, VSTO/Word MVP, my blog


    Friday, September 21, 2012 3:10 PM
    Moderator

All replies

  • How about if you do it more like this:

    with wordDoc
    MyRange = .Sections(1).Headers(wdHeaderFooterPrimary).Range
    MyRange.Paragraphs(2).Alignment = wdAlignParagraphLeft
    End with


    Cindy Meister, VSTO/Word MVP, my blog


    Friday, September 21, 2012 3:10 PM
    Moderator
  • Cindy,

    This is going to move every line in the header/Primary header to the left.

    I only need to move the second line to the left and leave the first line as it is. I alreay tried this approach and it moved every line in ther header to the left. I even tried to set the start and end position of the range but still no luck.

    Thanks for your help.

    Monday, September 24, 2012 5:36 PM
  • Hi BLT

    Perhaps you should specify exactly how the "lines" are generated.

    If you did not press ENTER to generate the lines, then you cannot align them differently using Alignment. This property affects entire paragraphs.

    If Shift+Enter was pressed, then both lines belong to the same paragraph. In this case, the only thing you could possibly do would be to define a Tab stop with Center alignment then insert a TAB character at the beginning of the line to be centered, leaving the entire paragraph left-aligned.

    vbCr is the equivalent of ENTER, so these should be two separate paragraphs.

    The only thing I can think of that could be causing the behavior you're seeing is if the Header style has been used to format this text AND it is set to "Automatically update" (change the style definition according to the formatting applied to the text). As I recall, this was the default for an older version of Word...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 25, 2012 4:54 AM
    Moderator
  • Hi Cindy,

    I created those strings from my program and I attached vbCr at the end of each line but when I change the line justification it moved all together. I am still trying to figure it out but no luck so far.

    thanks

    Tuesday, September 25, 2012 4:58 PM
  • Hi BLT123,

    Would you please share a sample document for further research?

    T.X.


    志异征诛,三让两家王朝
    功同开辟,一桮万古江南

    Wednesday, September 26, 2012 7:09 AM
  • T.X,

    I would like to share but I can not do it for our policy.

    Thanks for offering.

    Wednesday, September 26, 2012 6:55 PM
  • Hi Cindy,

    I found your suggestion about paragraph is pretty helpful, even I can not make it for my case. I have 3 paragraphs in my range but once I execute Myrange.Find.execute() to replace string in my range it changes to 1 paragraphs. I think that's why it is not working on my case. Do you know how to add new paragraph in the paragraphs collection. For example, I have Paragraphs(1) = "aaa" and Paragraphs(2) = "CCC" I would like to add new paragraph between the first and the second paragraph. Final result will be

    Paragraphs(1) = "aaa"

    Paragraphs(2) = "bbb"

    Paragraphs(3) = "CCC"

    Thanks in advance.

    Wednesday, September 26, 2012 7:01 PM
  • Hi BLT

    <<I have 3 paragraphs in my range but once I execute Myrange.Find.execute() to replace string in my range it changes to 1 paragraphs>>

    Don't you think we should fix this, instead?

    In order to add a paragraph you have to identify the Range that identifies the end of / start of a paragraph then use InsertAfter or InsertBefore to insert a paragraph. Question is: Can you identify these points?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 27, 2012 5:39 AM
    Moderator
  • Cindy,
    Thanks for your response. I was adding it by add new paragraph in the range using Range.Paragraphs.Add(range) and after that add text into it. After that  I did chanage the justification of paragraph. It works almost but sometime it moves more than one paragraph but I could not figure it out. So I took different approach to remove paragraphs and insert new paragraphs and it seems working fine now.

    Thank for your time and help!

    Wednesday, October 3, 2012 1:11 PM