• Macro to format doc (xp)

    Author
    Topic
    #445200

    Hi, I am wanting to do the following using a macro, but my knowledge of the WORD object model is a bit rusty…

    I want to go through a document, and find every instance of a set of characters, a speechmark followed by a square bracket “[ .

    Where the macro finds this, I need a carriage return at the begging of the line that these characters are found on, and also a carriage return in front of the characters themselves. Then I need the line above them to be made bold.

    I have this so far which just does the first part.

    Sub temp()
    Dim strFind As String
    strFind = Chr(34) & “[”

    With ActiveDocument.Content.Find
    Do While .Execute(FindText:=strFind, Forward:=True) = True
    With .Parent
    .StartOf Unit:=wdParagraph, Extend:=wdMove
    .InsertAfter Chr(13)
    .Move Unit:=wdParagraph, Count:=2
    End With
    Loop
    End With

    End Sub

    Any thoughts of the quickest way of doing this? I remember coding in Word a few years ago, using the Find and Range objects, but I am struggling now!

    Thanks

    Viewing 0 reply threads
    Author
    Replies
    • #1078894

      Try this:

      Sub temp()
      Dim strFind As String
      strFind = Chr(34) & “[”
      Application.ScreenUpdating = False
      Selection.HomeKey Unit:=wdStory
      With Selection.Find
      .Text = strFind
      .ClearFormatting
      .Forward = True
      Do While .Execute
      Selection.Collapse
      Selection.InsertParagraph
      Selection.HomeKey
      Selection.InsertParagraph
      Selection.MoveRight
      Selection.MoveDown Extend:=True
      Selection.Font.Bold = True
      Selection.MoveRight Count:=3
      Loop
      End With
      Application.ScreenUpdating = True
      End Sub

      It’s less elegant because it uses Selection, but I find that easier to use in combination with Find.
      The code may not do what you want if you have more than one “[ on a line.

      • #1079084

        Hi Hans, nice to hear from you – hows things?

        Thanks for that – I had done the code below but yours has some nice ways of doing things. Finding myself getting re-aquainted with Word object model!

        Sub findSelections()
        ‘remove formatting from .csv file
        With Selection
        .WholeStory
        .ClearFormatting
        .GoTo wdGoToLine, 1
        End With
        ‘look for approved
        With Selection.Find
        Do
        .Execute (“.0,Approved” & Chr(34))
        formatSelection
        Loop Until .Found = False
        End With
        ‘go back to beginning and look for effective
        Selection.GoTo wdGoToLine, 1
        With Selection.Find
        Do
        .Execute (“.0,Effective” & Chr(34))
        formatSelection
        Loop Until .Found = False
        End With
        MsgBox “Formatting Complete”
        End Sub

        Private Sub formatSelection()
        With Selection
        ‘go to line above and add paragraph marker
        .Move Unit:=wdLine, Count:=-1
        .TypeParagraph
        ‘go to the square bracket and add another paragraph marker
        .MoveUntil Cset:=”[“, Count:=100
        .TypeParagraph
        ‘select line above and format as bold
        .Move Unit:=wdParagraph, Count:=-1
        .MoveEnd Unit:=wdLine, Count:=1
        .Font.Bold = True
        ‘move back down
        .Move Unit:=wdParagraph, Count:=1
        End With
        End Sub

        Cheers

        • #1079114

          Hi matz,

          I’m fine. Glorious autumn day here – lots of sun, not much wind.

      • #1079255

        Hello Hans
        Can you help me understand what characteristics of your code made it unnecessary to use “Selection.Find.Found” to determine that there are no more instances of “strFind”.

        T.I.A.

        • #1079272

          The Execute method of the Find object is in fact a function that returns True if the ‘find what’ text was found, and False if it wasn’t. So you can use the construction

          With Selection.Find

          ‘ Perform the search and continue as long as it succeeds
          Do While .Execute

          Loop
          End With

    Viewing 0 reply threads
    Reply To: Macro to format doc (xp)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: