Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-15-2023, 01:34 AM
RobiNew RobiNew is offline Establish a new range after finding a string Windows 10 Establish a new range after finding a string Office 2016
Competent Performer
Establish a new range after finding a string
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Establish a new range after finding a string

I'm trying to establish a new range after finding a string, so that I can use Find again from the Found point to the end of the document. But the tentative code here below doesn't work. Can someone help? Thanks!
Code:
Dim oRng As Range
Set oRng = ActiveDocument.Range
    With oRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "Title"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .Execute
    End With
'Here I need to fix the starting point of a new Find, but it doesn't work
    oRng.Start = oRng.Find.Found
    oRng.End = ActiveDocument.Range.End
With oRng.Start.Find
.Text:="Section"
.Execute
End With

Reply With Quote
  #2  
Old 12-15-2023, 02:48 AM
vivka vivka is offline Establish a new range after finding a string Windows 7 64bit Establish a new range after finding a string Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Salut, RobiNew!
Code:
Sub TT()
Dim oRng As range
Set oRng = ActiveDocument.range
    With oRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .text = "Title"
      .Replacement.text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .Execute
    End With
    oRng.Collapse wdCollapseEnd 'this line can be commented
    oRng.End = ActiveDocument.range.End
    oRng.Select 'this line is for testing, can be commented
    With oRng.Find
      .text = "Section"
      .Execute
    End With
     oRng.Select 'this line is for testing, can be commented
lbl_exit:
Set oRng = Nothing
Exit Sub
End Sub
Reply With Quote
  #3  
Old 12-15-2023, 09:43 AM
RobiNew RobiNew is offline Establish a new range after finding a string Windows 10 Establish a new range after finding a string Office 2016
Competent Performer
Establish a new range after finding a string
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks, Vivka! A new problem: can you explain why the code here below doesn't work? I've tried two different (but equvalent) codes for Find, but nothing changes.
Code:
Sub InsertDiv()
  With Options
  .AutoFormatAsYouTypeReplaceQuotes = False
  End With
  Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
  .ClearFormatting
  .Text = "(</body>)" & Chr(13)
  '.Text = "(" & Chr(60) & "/body" & Chr(62) & ")" & Chr(13)
  .Replacement.Text = "\1" & "<div align=""center"">" & Chr(13) & "=========================================================================="
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchWildcards = True
  Execute Replace:=wdReplaceAll
  '.Execute 
  End With
  With Options
  .AutoFormatAsYouTypeReplaceQuotes = True
  End With
  End Sub
Reply With Quote
  #4  
Old 12-15-2023, 12:24 PM
vivka vivka is offline Establish a new range after finding a string Windows 7 64bit Establish a new range after finding a string Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, RobiNew! "<" and ">" are special chars that denote something different from their literal meanings under WildCard search (that is, they have special meanings, which are the start and end of a word), so, if you want to find them with their literal meaning, not special, use a backslash before each of them:
.text = "(\</body\>)" & Chr(13)
Some other special chars are *([)])!?#@
Reply With Quote
  #5  
Old 12-16-2023, 01:54 AM
RobiNew RobiNew is offline Establish a new range after finding a string Windows 10 Establish a new range after finding a string Office 2016
Competent Performer
Establish a new range after finding a string
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks, Vivka! À la prochaine!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Subscript out of Range Error when searched for string and its line number in Text File SamDsouza Word VBA 4 01-12-2021 07:08 PM
Establish a new range after finding a string Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
Establish a new range after finding a string How to Import range between two string from other document PRA007 Word VBA 1 12-18-2015 09:23 PM
Establish a new range after finding a string finding a string containing a combination of formats and adding text before Marco Word VBA 1 11-02-2015 05:08 PM
Establish a new range after finding a string Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:19 AM.


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