none
Change Fist word of every Paragraph RRS feed

  • Question

  • I'm trying to change first word of every paragraph if it include this word "QZ:^t". But this macro doesn't recognize ":" or tab mark. Any suggestions??

    Sub test1()

    Dim wdoc As Document
    Dim para As Paragraph
    Set wdoc = ActiveDocument

    For Each para In wdoc.Paragraphs
    If para.Range.Words(1).Text = "QZ:^t" Then para.Range.Words(1).Delete
    Next para

    End Sub

    Thanks in advance.

    Regards,

    Tuesday, June 21, 2016 5:34 AM

Answers

  • A word cannot include a TAB character. Except for the first paragraph in table cells, you could use a macro like:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      .InsertBefore vbCr
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^pQZ:^t"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
      End With
      .Characters.First.Delete
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by David_JunFeng Wednesday, June 22, 2016 4:56 AM
    • Marked as answer by Supunsam Wednesday, June 22, 2016 5:13 AM
    Tuesday, June 21, 2016 10:30 AM

All replies

  • A word cannot include a TAB character. Except for the first paragraph in table cells, you could use a macro like:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      .InsertBefore vbCr
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^pQZ:^t"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
      End With
      .Characters.First.Delete
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by David_JunFeng Wednesday, June 22, 2016 4:56 AM
    • Marked as answer by Supunsam Wednesday, June 22, 2016 5:13 AM
    Tuesday, June 21, 2016 10:30 AM
  • Hi Supunsam,

    According to your description, I have verify macropod's code, it works perfectly. So I suggest that you could follow macropod's solution.
    Wednesday, June 22, 2016 5:06 AM
  • Hi Paul,

    Thank you very much for your answer and please be kind enough to clarify something for me. Can you explain how this macro actually works and what kind of method its using to get the work done. Specially about that first part:

    With ActiveDocument.Range
      .InsertBefore vbCr
      With .Find

    Regards,

    Supun Samarakoon

    • Edited by Supunsam Wednesday, June 22, 2016 5:41 PM
    Wednesday, June 22, 2016 5:41 PM
  • The macro is basically just doing a Find/Replace to replace every sequence of a paragraph break followed by 'QZ:' then a tab with just a paragraph break.

    The '.InsertBefore vbCr' and '.Characters.First.Delete' are there just in case your first paragraph starts with 'QZ:' then a tab. They insert a temporary empty paragraph, then delete it when the Find/Replace has finished; otherwise the Find/Replace wouldn't update the first paragraph.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, June 22, 2016 9:49 PM
  • Thank you for the explanation paul. As always you're the best. Thank you very much.

    Regards

    Supun Samarakoon

    Thursday, June 23, 2016 9:32 PM