Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-30-2023, 09:52 AM
RobiNew RobiNew is offline Code works only in isolation not within a larger set Windows 10 Code works only in isolation not within a larger set Office 2016
Competent Performer
Code works only in isolation not within a larger set
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question Code works only in isolation not within a larger set

I have two code versions to obtain the same result. They both work perfectly in isolation. When inserted in a larger set of commands they do not work at all on the same document. No error occurs. Can someone help? Thanks!
Code:
Sub Replace1()
Dim aRng As Range
Set aRng = ActiveDocument.StoryRanges(1)
    With aRng.Find
        .Text = "(^0146)" & "(^0171)" '« (146=’)
        .Replacement.Text = "\1" & "^0187" '»
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Code:
Sub Replace2()
Dim aRng As Range
Set aRng = ActiveDocument.StoryRanges(1)
    With aRng.Find
      .MatchWildcards = False
      .Text = Chr(146) & Chr(171) '« (146=’)
      .Replacement.Text = Chr(146) & Chr(187) '»
      .Execute Replace:=wdReplaceAll
    End With
End Sub

Reply With Quote
  #2  
Old 11-30-2023, 02:38 PM
Guessed's Avatar
Guessed Guessed is offline Code works only in isolation not within a larger set Windows 10 Code works only in isolation not within a larger set Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

There are other .Find parameters and they are sticky settings. For example, if an earlier find left some formatting parameters turned on then that would stop your simple find from getting hits.

Without seeing your earlier code it is hard to diagnose but you could add in all the usual other .Find settings you get when you record a find/replace to ensure that no other sticky settings are causing a problem.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 12-01-2023, 12:59 AM
RobiNew RobiNew is offline Code works only in isolation not within a larger set Windows 10 Code works only in isolation not within a larger set Office 2016
Competent Performer
Code works only in isolation not within a larger set
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks for your hint, Guessed! It saved me a lot of time.
Reply With Quote
  #4  
Old 12-01-2023, 03:59 AM
gmaxey gmaxey is offline Code works only in isolation not within a larger set Windows 10 Code works only in isolation not within a larger set Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

RobiNew,


I have a bit of fixed code in my normal module. In most of my projects, whenever I want to do a find and replace, I call it



Code:
Sub Replace1()
Dim aRng As Range
   ResetFRParameters
   Set aRng = ActiveDocument.StoryRanges(1)
     With aRng.Find 
        .Text = "(^0146)" & "(^0171)" '« (146=’) 
        .Replacement.Text = "\1" & "^0187" '» 
        .MatchWildcards = True   
        .Execute Replace:=wdReplaceAl
     End With
End Sub

Code:
Sub ResetFRParameters()
 With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute
End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 12-01-2023, 04:37 AM
RobiNew RobiNew is offline Code works only in isolation not within a larger set Windows 10 Code works only in isolation not within a larger set Office 2016
Competent Performer
Code works only in isolation not within a larger set
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Thank you, Gmaxey! Your bit of fixed code is very useful.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perfect macro not working when its code is inserted in larger macro RobiNew Word VBA 3 10-18-2023 03:19 AM
VBA code looks ugly, but works ok, needs a going over to neaten it up. trevorc Outlook 0 04-21-2022 07:51 PM
Biz Case for Larger Monitors Karen615 Misc 0 12-14-2021 01:41 PM
larger ribbon Herm61 Outlook 0 05-27-2013 12:46 PM
Code works only in isolation not within a larger set .OnAction only works in document with the code donbexcel Word VBA 1 11-02-2011 05:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:27 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