Results 1 to 12 of 12
  1. #1
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Need some Style (Word 2000 and >)

    <img src=/S/hello.gif border=0 alt=hello width=25 height=29> Loungers

    I got a word document that has several paragraphs that start with a title made with the "Heading 1" style.

    I need to add two lines before the titles.

    Could someone give me a starting point with some VBA code?

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> A lot...

    Wassim
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Need some Style (Word 2000 and >)

    Can you edit the paragraph format of the Heading 1 style to have Space Before = 24 points, or do you really want empty paragraphs?

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Need some Style (Word 2000 and >)

    This should be enough to get you started.

    StuartR

    <font face="Georgia">
    Sub AddTextToHeadings()

    Const strAddedText As String = "First Line" & vbCrLf & "Second Line" & vbCrLf

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Style = wdStyleHeading1
    Do While .Execute(FindText:="", Forward:=True, Format:=True) = True
    With .Parent
    .StartOf Unit:=wdParagraph, Extend:=wdMove
    .InsertAfter strAddedText
    .Move Unit:=wdParagraph, Count:=3
    End With
    Loop
    End With

    End Sub
    </font face=georgia>

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Need some Style (Word 2000 and >)

    Jefferson,

    I see we made completely different assumptions about the content of the new lines <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    StuartR

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Need some Style (Word 2000 and >)

    No kidding! And with the trend toward text-based electronic communications in shorter and shorter fragments, we are guaranteed a future of misunderstandings. <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>

    I'm intrigued by your definition of strAddedText as a constant. I suppose it's lazy of me to always use variables for everything, including things that don't vary, but I've never really stopped to consider whether it might be better to use constants. Is there some level of repetition at which it gives you a noticeable performance payoff, or is it more good programming hygiene so that in a large project you don't accidentally try to assign a value to something which really is supposed to be a constant? Inquiring minds want to know. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Need some Style (Word 2000 and >)

    The constant is habit born of years of C programming, where variables on the stack can overflow in horrible ways and constants are nicely compiled into read only locations. But I think it is good practice to use constants wherever possible in any language to reduce accidents and to use memory efficiently.

    StuartR

  7. #7
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Need some Style (Word 2000 and >)

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> Stuart

    OK I see how its working, it is actually inserting the string made of two vbCrLfs. I removed the text within the quotes because I wanted two blank lines, so the vbCrLf will do just fine.

    Now I have a different problem, and I think I know how to fix it, except I don't know Word's Object Model that well:

    The two lines being inserted are of the same style as the Header1. I would prefer the lines be of the document text, which is Courier New, 10pt.

    What should I do with the: .StartOf Unit:=wdParagraph, Extend:=wdMove is to change it to .StartOf Unit:=wdLine, Extend:=wdMove, but when I did that, the code broke with an Run-Time error 4120 Bad Parameter. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>

    Also, could I test to see if there are two lines before the header, not to add them?

    Thanks a Million

    Wassim
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  8. #8
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Need some Style (Word 2000 and >)

    <img src=/S/hello.gif border=0 alt=hello width=25 height=29> jscher2000

    I need to have some of the headers treated, and others already have two lines before them, so this would not be an ideal solution. I should have mentioned this before, but I only recognized that this can happen due to how the data is outputed, and the mainframe guys don't know any thing about PCs, so its a moot argument trying to figure things out for us.

    Wassim
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Need some Style (Word 2000 and >)

    It's actually a very small change because when you insert to the beginning of the "found" range, the range expands and you can simply apply a style to it:

    <pre>Option Explicit

    Sub AddTextToHeadings()
    Const strAddedText As String = "First Line" & vbCrLf & "Second Line" & vbCrLf
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Style = wdStyleHeading1
    Do While .Execute(FindText:="", Forward:=True, Format:=True) = True
    With .Parent
    .StartOf Unit:=wdParagraph, Extend:=wdMove
    .InsertAfter strAddedText
    '.Select 'For debug, to confirm the range covers the new text
    'Style the inserted paragraphs
    .Style = wdStyleNormal
    .Move Unit:=wdParagraph, Count:=3
    End With
    Loop
    End With
    End Sub</pre>

    Hope this helps.

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Need some Style (Word 2000 and >)

    <hr>Also, could I test to see if there are two lines before the header, not to add them?<hr>
    It might be easier to search the whole document for 4 consecutive blank paragraphs and replace with 2 after you run the macro.

    StuartR

  11. #11
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Need some Style (Word 2000 and >)

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> Stuart

    Is this what is called "Thinking outside the Box"? [boxed]

    I now have a neat applet that we can use to manage this cleaning process. Thanks for your help <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

    Wassim
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  12. #12
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Need some Style (Word 2000 and >)

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> Jefferson

    That did the trick. I tweeked it a bit further, and it did the job quite good.

    Thanks for your help.

    Wassim
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

Posting Permissions

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