Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-17-2023, 11:06 AM
savvy savvy is offline if next key pressed is "del" Windows 10 if next key pressed is "del" Office 2016
Novice
if next key pressed is "del"
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default if next key pressed is "del"

I want to add code to a macro that will:

if next key pressed is "del", will press del key and restart macro
Reply With Quote
  #2  
Old 04-17-2023, 06:35 PM
Guessed's Avatar
Guessed Guessed is offline if next key pressed is "del" Windows 10 if next key pressed is "del" Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,994
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

Perhaps you could explain what the macro is doing. How do you propose the macro should sit in a state where it is waiting for a keypress event?

It sounds like you should be using a MsgBox to ask the user if they want to delete something.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-18-2023, 06:33 AM
savvy savvy is offline if next key pressed is "del" Windows 10 if next key pressed is "del" Office 2019
Novice
if next key pressed is "del"
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default if next key pressed is "del"

The macro is below. It highlights the next set of square brackets when I press + (the closest key to the mouse which was free to reassign). Essentially, it replaces Dragon Dictate verbal commands which require me to turn on the mic when I prefer to keep it off and talk while editing).

If I want to change the text that is highlighted I type in the change, press + and go to the next set of [].

If I want to keep the default text I press + and go to the next set of [....default text].

If I want to delete highlight and move on to the next set of [] want to add code that will: If the next key pressed is "del" press "del" and run the macro again (to continue to the next set of [].

this is my code:
Sub FindBetweenSqBrac()
'
' FindBetweenSqBrac Macro
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(\[)(*)(\])"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

End Sub

Thank you.
Reply With Quote
  #4  
Old 04-18-2023, 05:24 PM
Guessed's Avatar
Guessed Guessed is offline if next key pressed is "del" Windows 10 if next key pressed is "del" Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,994
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

The simplest way is to use a MsgBox to ask if the user wants to continue on to the next location. You can click Yes (or type Y) to find next or No (or type N) to stop the macro. If you want to change the text, you type N to end the macro, make the desired change and then start the macro again.
Code:
Sub FindNextSquareBrackets()
  Dim iResp As Integer, aRng As Range
  Set aRng = ActiveDocument.Range(Selection.Range.Start, ActiveDocument.Range.End)
  With aRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(\[)(*)(\])"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    Do While .Execute
      aRng.Select
      iResp = MsgBox("Goto Next?", vbYesNo, "Found one")
      If iResp = vbNo Then Exit Sub
      aRng.Collapse Direction:=wdCollapseEnd
      aRng.End = ActiveDocument.Range.End
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 04-20-2023, 01:26 AM
savvy savvy is offline if next key pressed is "del" Windows 10 if next key pressed is "del" Office 2019
Novice
if next key pressed is "del"
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default if next key pressed is delete

Thanks. I had already considered your approach. The problem is that it slows down the work when there are many [default text] in the document

would this work?

Sub FindBetweenSqBrac()
'
' FindBetweenSqBrac Macro
'


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(\[)(*)(\])"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute

If the next key pressed is {DEL}
then
SendKeys {DEL}
play FindBetweenSqBrac Macro
OR if the next key pressed is anything else than {DEL}
then
End Sub

Or would reassigning keypresses work? for example:

run FindBetweenSqBrac Macro 'same code as macro above
then
assign {DEL} to "{DEL} and run FindBetweenSqBrac macro"
then
assign {DEL} back to {DEL}
End Sub

Thanks again
Savvy
Reply With Quote
  #6  
Old 04-25-2023, 06:40 PM
savvy savvy is offline if next key pressed is "del" Windows 10 if next key pressed is "del" Office 2019
Novice
if next key pressed is "del"
 
Join Date: Aug 2020
Location: Toronto
Posts: 17
savvy is on a distinguished road
Default next key pressed is del

Thanks so much Guessed.

I tried your suggestion and it works perfectly. I am trying to tweak it to work in the context of the task I am doing. I have assigned the macro to an unused key sequence.

1) can I change the behavior of hitting "N" to: if after I type a word or sentence, when I next press enter, the macro would play again instead of starting a new line, and revert to its normal behavior?

2) how do I change the code to respond to keypress " ' " instead of "N", then
revert " ' " back to " ' "?

Thanks Savvy
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mailto Hyperlink Formula contains too large of "Body" receiving "#Value" need to find workaround MCamera Excel 1 03-02-2022 07:52 PM
Excel 2003: VBA "Function" causes "#VALUE!" errors after running "insert/delete row" custom macro Matt C Excel Programming 2 01-08-2022 06:03 AM
if next key pressed is "del" How to configure Word 10 so that when "enter" is pressed, the entire next line of text drops togethe JKW Word 3 11-25-2015 04:55 PM
remove repeated words with " macro " or " wild cards " in texts with parentheses and commas jocke321 Word VBA 2 12-10-2014 11:27 AM
if next key pressed is "del" How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM

Other Forums: Access Forums

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