Consulting

Results 1 to 3 of 3

Thread: Getting Word to Move to next instance of a text

  1. #1
    VBAX Newbie
    Joined
    Sep 2022
    Posts
    2
    Location

    Getting Word to Move to next instance of a text

    Hello, I'm really new at this, and I'm trying to build a macro that can do a repetitive activity that I do at work for me.

    It's supposed to take a table that we have and add a row to it with specific minor changes, and I've got it to work mostly.

    I haven't included all the code below, but the relevant bits. So the code checks each paragraph to see if it's a certain kind of paragraph, and if it is then it executes a given action. DiscPara, for example, checks to see if the paragraph contains {{Disc}}, and if it does, it moves the selection until it finds the "!", then moves down from that and copies and pastes the value.

    The problem is that {{Disc}} appears 26 times in my document, but because of the way I have it coded right now, it begins the selection at the first instance of {{Disc}} instead of the paragraph that the loop is currently on. It looks in the string of allWords for the first instance {{Disc}} and then performs the desired function on the first instance instead of on the second, third, etc. The same goes for lines containing ! WMS & i.

    I've been thinking to possibly separate it out of the loop, so it's something that happens after everything else in the document. In either case, though, I'm not sure how to get it to perform the desired function at the paragraph I want it to instead of at the first instance of the paragraph.

    Sub UPdate()
    ActiveDocument.PageSetup.LineNumbering.Active = False
    allWords = ActiveDocument.Content.Text
    i = "11.1"
    j = "11.2"
    myText = myPara.Range.Text
    For Each myPara In ActiveDocument.Paragraphs
    DiscPara:
          If InStr(myText, "{{Disc}}") > 0 Then
            With Selection
              .Start = Selection.Start
              .End = Selection.Start
              .Collapse wdCollapseStart
                Do While InStr("!", Left(Selection.Text, 1)) = 0
                .MoveStart , -1
                Loop
              .Collapse wdCollapseStart
              .MoveStart wdParagraph, 1
              .Expand wdParagraph
              .Copy
              .MoveUp wdParagraph, 1
              .Paste
            End With
            DoEvents
            GoTo StartUpdate
          Else: GoTo HeadPara
          End If
    HeadPara:
          If InStr(myText, "! ABC " & i) > 0 Then
            With Selection
                  .Start = Selection.Start
                  .End = Selection.Start
                  .Collapse wdCollapseStart
                  .Expand wdParagraph
                  .Copy
                  .MoveUp wdParagraph, 1
                  .Paste
                  .Start = InStr(allWords, myText) + 2
                  .End = InStr(allWords, myText) + 3
                  .Expand wdParagraph
                End With
    With Selection.Find
                  .ClearFormatting
                  .Replacement.ClearFormatting
                  .Text = i
                  .Wrap = wdFindStop
                  .Forward = True
                  .Replacement.Text = j
                  .MatchWildcards = False
                  .Execute Replace:=wdReplaceAll
                End With
            DoEvents
            GoTo StartUpdate
          End If
    StartUpdate:
        Next myPara
    End Sub
    Last edited by Aussiebear; 09-16-2022 at 04:55 PM. Reason: Added code tags to suppled code

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Without sample documents showing what you're starting with and what you hope to end up with, it's impossible to know what your code is supposed to be doing, as it relies on the repeated use of Selection - for which we are oblivious as to what is being selected. There are also some fairly fundamental problems with your code, including setting myText = myPara.Range.Text before determining what myPara is, via For Each myPara In ActiveDocument.Paragraphs.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Sep 2022
    Posts
    2
    Location
    Thank you for your thoughts. Since I can't post the docs I'm editing, I understand now that this won't be helpful. Thank you for pointing out the error in my code too.

Posting Permissions

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