Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-25-2023, 11:56 AM
ctviggen ctviggen is offline Pasting in text, selecting that text, replacing words within that text but adding numbers too Windows 10 Pasting in text, selecting that text, replacing words within that text but adding numbers too Office 2016
Advanced Beginner
Pasting in text, selecting that text, replacing words within that text but adding numbers too
 
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
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pasting in text, selecting that text, replacing words within that text but adding numbers too Adding space between numbers in list and the text cricket1001 Word 3 05-13-2020 03:56 PM
Pasting in text, selecting that text, replacing words within that text but adding numbers too Adding numbers outside a text box chubtor Word 13 03-28-2020 06:07 AM
Replacing a comment with underline or other formatting (or adding that formatting to commented text) paulkaye Word 4 05-16-2016 05:38 AM
Selecting and moving text boxes identified by specific text. Chayes Word VBA 8 02-22-2016 07:01 AM
Pasting in text, selecting that text, replacing words within that text but adding numbers too How to keep the footnote(endnote) reference numbers when pasting text between two documents? gn4619 Word 4 10-22-2015 08:01 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:49 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft