none
Macro to apply style for paragraphs in word document RRS feed

  • Question

  • Hi,

    I need a macro to replace the paragraphs with the style tags based on conditions

    All the style tags which need to be replaced are existed with the attached document.

    • Replace all the paragraphs in the active document whish has first line indent  > 0”  but <0.3”  to a style named”P1”, and
    • Al l the bold paragraphs which has first line indent  >0”  but <0.3”  to a style named   “P2”, and
    • All paragraphs which first line indent  >0.3” and   <0.5” will be  “P1A
    • All paragraphs which has alignment fully left (no indent) should be style named   “P
    • All bold paragraphs which has alignment fully left (no indent) should be style named  “H2
    • All the centered paragraphs should be style named  “H7
    • All the bold and centered paragraphs should be style named  “H1
    • All paragraphs which has left alignment  >0” but  <0.4” (no first line or hanging indent) should be style named  “I2A
    • All paragraphs which has left alignment  >0” but  <0.4” and first line alignment  >0.2”   but <0.4” should be style named  “I2
    • All paragraphs which has hanging alignment <0.40” and left indent 0” should be style named  “IH2
    • All paragraphs which has hanging alignment >0.41” but <0.70” and left indent >0” and <0.4” should be style named  “IH2A
    • All the bold paragraphs which has hanging alignment <0.40” and left indent 0” should be style named  “IH2QB

    And also I want to attach all these style tags in any word file.

    Document attached in the below link

    https://www.dropbox.com/s/kt3mezxuyeztzs5/Style%20tags.docx

    Could any one look and help on this please?

    Thanks

    John

    Sunday, July 20, 2014 7:12 AM

Answers

  • Hi John

    I didn't say to use ReplaceAll, did I :-)?

    Beyond that, I recommend you first use the Find feature (dialog box) in Word until you get the set of criteria that WORK to FIND what you need to manipulate. Once that works you can record making and running the settings in a macro to get the basic syntax that works.

    You can also try using Replace with the style to see if that works. If it does, then record that and you should be all set.

    If it does not, perhaps because other steps would be required, then you have to work with the code loop, such as you currently have.

    But in either case you have to begin by getting the Find settings and execution code necessary to locate the text you want to manipulate.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, July 21, 2014 3:55 PM
    Moderator

All replies

  • This forum is for Word Developers - to help them develop and resolve issues with their own code, not to write and develop it for them. So far, all you've done is to treat the forum as a free coding service.

    Also, your specifications are incomplete. For example, you have no specification for what happens when the first line indent is exactly 0.3".


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, July 20, 2014 10:51 AM
  • Hi Paul,

    Thanks for your immediate response.

    It works fine when I replace the style tags with find and replace option, however it not works when I recorded the find and replace through macro.

    So I submit it in this forum.

    You can avoid if any request is incomplete.

    I really understand it gives pain to write a long code for with many conditions.

    Writing any minimum of 3 code (each one from (Hanging, Left Indent, First line Indent) ) will be much appreciated, otherwise  you can ignore this request.

    Thanks for all your help.

    John


    Sunday, July 20, 2014 12:39 PM
  • Hi,

    I tried with the below code.

    It change the bold paragraph also to style "P"

    But all I need the macro to change the paragraphs which is not bold and Italic formatted.


    Dim oPara As Paragraph
    With ActiveDocument
      For Each oPara In .Paragraphs

     Selection.Find.ClearFormatting
        With Selection.Find.ParagraphFormat

            .Alignment = wdAlignParagraphJustify
        End With

       With Selection.Find.Font
            .Bold = False
            .Italic = False

        End With
      
      
        With oPara
          If .LeftIndent <= 0 Then
            
            .Style = "P"
          End If
        End With
      Next
    End With


    Any help will be much appreciated.

    John

    Sunday, July 20, 2014 2:44 PM
  • Hi John

    Since you don't use a Find.Execute Word isn't doing anything with the Selection.Find settings you have in your code. Stick a Selection.Find.Execute in there and see if that makes any difference.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, July 21, 2014 1:52 PM
    Moderator
  • Hi Cindy Meister,

    I tried with the below code                    

    Selection.Find.Execute Replace:=wdReplaceAll

    But its didn't make any difference.

    Its apply the style tag for bold paragraph too.

    All I need the macro to find only the hanging paragraph(not bold or italics format and paragraph alignment either left or justify) which is greater than 2”  and less than 4”  and replace the paragraphs with any style. “Say style 1”

    And find only the first line indent paragraphs (not bold or italics format and paragraph alignment either left or justify) which is greater than 2”  and less than 4”  and replace the paragraphs with any style. “Say style 2”

    I tried in websites for many hours, but I didn't found any code or solutions to replace style for the Indent paragraphs based on conditions.

    Regards

    John

    Monday, July 21, 2014 3:46 PM
  • Hi John

    I didn't say to use ReplaceAll, did I :-)?

    Beyond that, I recommend you first use the Find feature (dialog box) in Word until you get the set of criteria that WORK to FIND what you need to manipulate. Once that works you can record making and running the settings in a macro to get the basic syntax that works.

    You can also try using Replace with the style to see if that works. If it does, then record that and you should be all set.

    If it does not, perhaps because other steps would be required, then you have to work with the code loop, such as you currently have.

    But in either case you have to begin by getting the Find settings and execution code necessary to locate the text you want to manipulate.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, July 21, 2014 3:55 PM
    Moderator
  • Hi Cindy Meister,

    Thanks for your immediate reply.

    Will let you know if I able to get the code with your suggesstion.

    John

    Monday, July 21, 2014 4:33 PM