Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-14-2022, 06:39 AM
laith93 laith93 is offline Applying a VBA code to current paragraph without being selected Windows 10 Applying a VBA code to current paragraph without being selected Office 2019
Competent Performer
Applying a VBA code to current paragraph without being selected
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default Applying a VBA code to current paragraph without being selected

Hi,
Please, how to apply a VBA code to the current paragraph without selecting it at all (in general), in my case, I want to apply a find and replace VBA code to the current paragraph without selecting it (just the pointer is in)



Code:
Sub TestV2()
'
'
  Dim Rng As range
 Set Rng = Selection.range


    With Rng.Find
        .Text = "[ñõðóòö~úø]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I used
Selection.Paragraphs(1).range
for Rng
but does not work
Thanks
Reply With Quote
  #2  
Old 10-14-2022, 07:58 AM
rollis13's Avatar
rollis13 rollis13 is offline Applying a VBA code to current paragraph without being selected Windows 10 Applying a VBA code to current paragraph without being selected Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

Here you will find how to apply your macro to the current paragraph LINK .
The linked macro will automatically select the current paragraph if you substitute:
Code:
Debug.Print GetParagraphIndex(r)
with:
Code:
ActiveDocument.Paragraphs(GetParagraphIndex(r)).Range.Select
Once the paragraph is selected you can apply your macro to Find/Replace (even if your macro doesn't seem to work for me??!!).

If it does what you need then you can easily merge the codes into a single Macro + Function and assign it to a shortcut key.
Reply With Quote
  #3  
Old 10-14-2022, 09:46 AM
laith93 laith93 is offline Applying a VBA code to current paragraph without being selected Windows 10 Applying a VBA code to current paragraph without being selected Office 2019
Competent Performer
Applying a VBA code to current paragraph without being selected
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by rollis13 View Post
Here you will find how to apply your macro to the current paragraph LINK .
Thanks for your reply,
I followed the link and made the code
It works fine, but the button for this code requires clicking on it twice
first for selecting the current paragraph, and the second for applying the VBA code.
However, it's ok
but what if I want to run the code at all in just single click
Thanks
Reply With Quote
  #4  
Old 10-14-2022, 11:43 AM
rollis13's Avatar
rollis13 rollis13 is offline Applying a VBA code to current paragraph without being selected Windows 10 Applying a VBA code to current paragraph without being selected Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

I already said what you have to do: merge the code, or, Call the second macro just before the end of the first.
Reply With Quote
  #5  
Old 10-14-2022, 08:46 PM
gmayor's Avatar
gmayor gmayor is offline Applying a VBA code to current paragraph without being selected Windows 10 Applying a VBA code to current paragraph without being selected Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Selection.Paragraphs(1) will work. There is no need to select the paragraph. You do however have to stop the search when the range has been processed.



Code:
Sub TestV3()
'
'
Dim Rng As Range
    Set Rng = Selection.Paragraphs(1).Range
    With Rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[no?ooo~uo]"
        .Replacement.Text = ""
        .Wrap = wdFindStop 'stop the search
        .Format = True
        .Execute Replace:=wdReplaceAll
    End With
    Set Rng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 10-14-2022, 11:04 PM
laith93 laith93 is offline Applying a VBA code to current paragraph without being selected Windows 10 Applying a VBA code to current paragraph without being selected Office 2019
Competent Performer
Applying a VBA code to current paragraph without being selected
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by rollis13 View Post
I already said what you have to do: merge the code, or, Call the second macro just before the end of the first.
Thanks so much
Thank you for your help
Reply With Quote
  #7  
Old 10-14-2022, 11:10 PM
laith93 laith93 is offline Applying a VBA code to current paragraph without being selected Windows 10 Applying a VBA code to current paragraph without being selected Office 2019
Competent Performer
Applying a VBA code to current paragraph without being selected
 
Join Date: Jul 2021
Posts: 117
laith93 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Selection.Paragraphs(1) will work. There is no need to select the paragraph. You do however have to stop the search when the range has been processed.
Wow Wow Wow

It works perfectly, although I changed the Rng to Selection.Paragraphs(1).Range, but doesn't work previously.
However, I think it requires restarting Word, but when I used your code, it works directly.

Thanks, Thanks Thanks my bro
You are the magic man
Reply With Quote
Reply

Tags
find & replace, word 19, word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Applying a VBA code only on a selected table RobertDany Word VBA 5 10-25-2021 09:28 PM
Applying a VBA code only on a selected text or range RobertDany Word VBA 2 10-09-2021 08:31 AM
Applying a VBA code to current paragraph without being selected applying heading style within paragraph using macro cheech1981 Word 8 08-23-2017 03:07 PM
Applying a VBA code to current paragraph without being selected Applying style to first part of paragraph foxtrot Word 3 07-24-2012 07:30 AM
Does Word do this when applying Styles to a paragraph a second time? Bobosmite Word 2 04-17-2012 02:13 PM

Other Forums: Access Forums

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