View Single Post
 
Old 03-25-2023, 11:56 AM
ctviggen ctviggen is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default Pasting in text, selecting that text, replacing words within that text but adding numbers too

I am trying to work within text that has been pasted in. Here is the sequence, and whether or not it works (in parentheses):


1) Add some text (works)
2) Add more text indicating the first "Example" ["Example 1."] follows. (This works, but gets overwritten: the oRng.Move needs to set to the end of the line, but I can't find a wdXXXX that does that, so I tried two wdWord, which get close but the end gets overwritten.)
3) Paste and select the text (works)
4) Search for two paragraph returns and replace with with one paragraph return and "Example [i++]. " (works, but needs an increase in number)


In other words, the output should be:
"The following are additional examples.
Example 1. [A bunch of text]
Example 2. [More text]
Example 3. [More text]..."


a) How do I move the range to the end of "Example " & i & ". "?


b) How do I add in and increase the integer i?



To add in the number, basically, I want to so this, along with increasing i each time:


Code:
.Replacement.Text = "^pExample " & i & ". "
But With Selection.Find...End with isn't really a loop.



c) Also, once I'm done manipulating the selected text, is there a way to deselect the text?


Thanks for any help.



Here is the code:


Code:
Sub InsertTextWithExamples()

    Dim oRng As Word.Range
    Dim lngS As Long
    Dim i As Integer
    i = 1
          
    ' oRng is used to select the range of what is being pasted.
    Set oRng = Selection.Range
    ' Sets lngS to the starting point of the range
    lngS = oRng.Start
    ' Inserts beginning text and moves down
    Selection.InsertAfter Text:="The following are additional examples." & vbCr
    oRng.Move wdParagraph
    Selection.InsertAfter Text:="Example " & i & ". "
    oRng.Move wdWord
    oRng.Move wdWord
    ' Pastes the claims and selects what was just pasted
    Options.PasteOptionKeepBulletsAndNumbers = True
    oRng.PasteSpecial DataType:=wdPasteText
    oRng.Start = lngS
    oRng.Select
    ' Replaces two paragraph returns with one paragraph return then "Example "
    With Selection.Find
        .Text = "^p^p"
        .Replacement.Text = "^pExample "
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Reply With Quote