Results 1 to 6 of 6
  1. #1
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to format doc (xp)

    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
    Thanks,

    pmatz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to format doc (xp)

    Try this:

    Sub temp()
    Dim strFind As String
    strFind = Chr(34) & "<!t>["
    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.

  3. #3
    4 Star Lounger
    Join Date
    Aug 2003
    Location
    Stroud, United Kingdom
    Posts
    548
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to format doc (xp)

    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
    Thanks,

    pmatz

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to format doc (xp)

    Hi matz,

    I'm fine. Glorious autumn day here - lots of sun, not much wind.

  5. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Macro to format doc (xp)

    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.
    Regards
    Don

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to format doc (xp)

    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

Posting Permissions

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