+ Reply to Thread
Results 1 to 3 of 3

Thread: select text after replace in all storyranges

  1. #1
    Registered User
    Join Date
    08-01-2007
    Location
    Johannesburg
    Posts
    8

    select text after replace in all storyranges

    I have a word document that contains normal text, tables and shapes. I have a macro that runs through an excel list and replaces certain texts in the word doc with other data. So it would replace <name> with Bob Smith for example.

    The problem is some data strings are longer than 255 characters. So I need to replace <name> with the first 255 characters. SELECT the text at that point and insert the other data via numerous strings.

    The problem is when using this code how do I select the range in order to be able to insert the data. Find / selection only moves the cursor when finding via selection. When finding via storey range word doesn't move the cursor to that point.

    Here is an extract of my code:

                        For Each myStoryRange In ActiveDocument.StoryRanges
                            myStoryRange.Select
                            With myStoryRange.Find
                                .Text = input_text
                                .Replacement.Text = Mid(output_text, 1, 255)
                                .Wrap = wdFindContinue
                                .Execute Replace:=wdReplaceAll
                            End With
                            
                            If myStoryRange.Find.Execute = True Then
                                myStoryRange.Find.Execute Replace:=wdReplaceAll
                                Set rng = doc.Range(myStoryRange.End, myStoryRange.End)
                                rng.InsertAfter " Inserted text"
                            End If
    
    
                            
                            Do While Not (myStoryRange.NextStoryRange Is Nothing)
                                Set myStoryRange = myStoryRange.NextStoryRange
                                With myStoryRange.Find
                                    .Text = input_text
                                    .Replacement.Text = Mid(output_text, 1, 255)
                                    .Wrap = wdFindContinue
                                    .Execute Replace:=wdReplaceAll
                                End With
    
                            Loop

  2. #2
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: select text after replace in all storyranges

    in Word there's no limitation to string lengths.

    I would prefer:

    Sub snb()
      For Each sr In ThisDocument.StoryRanges
        sr.Find.Execute "text", , , , , , , , , "replacement text", wdReplaceAll
      Next
    End Sub



  3. #3
    Registered User
    Join Date
    08-01-2007
    Location
    Johannesburg
    Posts
    8

    Re: select text after replace in all storyranges

    Thanks for the assistance.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0