Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-28-2023, 10:36 PM
Mullin4414 Mullin4414 is offline Help making a Macro Windows 11 Help making a Macro Office 2019
Novice
Help making a Macro
 
Join Date: Feb 2023
Posts: 3
Mullin4414 is on a distinguished road
Default Help making a Macro


someone who is good a programing may be able to help me here, i am trying to set up a macro to mimic a medical software i use at work. basically anytime i press the F2 key in whatever word document i am in i want the macro to highlight the next set of "***" in my document so i can start typing and it will replace the 3 asterisk's
Reply With Quote
  #2  
Old 03-01-2023, 02:09 AM
gmayor's Avatar
gmayor gmayor is offline Help making a Macro Windows 10 Help making a Macro Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

You can record a macro that will do that. e.g.
Code:
Sub Macro1()
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "***"
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub
It might make more sense to replace your asterisks with content controls e.g.
Code:
Sub Macro2()
Dim oRng As Range
Dim oCC As ContentControl
Dim i As Integer: i = 0
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .Wrap = wdFindContinue
        Do While .Execute("***")
            i = i + 1
            oRng.Text = ""
            Set oCC = oRng.ContentControls.Add(wdContentControlRichText)
            oCC.Title = "Text" & i
            oCC.Tag = oCC.Title
            oCC.LockContentControl = True
            oRng.End = oRng.End + 1
            oRng.Collapse 0
        Loop
    End With
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
  #3  
Old 03-01-2023, 11:30 AM
Mullin4414 Mullin4414 is offline Help making a Macro Windows 11 Help making a Macro Office 2019
Novice
Help making a Macro
 
Join Date: Feb 2023
Posts: 3
Mullin4414 is on a distinguished road
Default Thank you!!

thank you so much this is going to help me a lot!, since you seem to be very quick with the code (i havent tried the second one yet im still working out how this all works), do you know if there is a way to instead of selecting the *** i could select the next group of lets say red text or bold text or whatever is easiest, so if i wrote "put patient name here" in red text i could hit F2 and have it select that group of text for me to replace?
Reply With Quote
  #4  
Old 03-01-2023, 10:07 PM
gmayor's Avatar
gmayor gmayor is offline Help making a Macro Windows 10 Help making a Macro Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

You can assign the following to a key -

Code:
Sub FindRed()
    Selection.Find.ClearFormatting
    Selection.Find.Font.Color = wdColorRed
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub
However it seems that you are trying to create proforma templates for documenting patients. That being the case I would recommend using Rich Text content controls and/or List Box content controls for selecting from lists. To that end you will probably find Insert Content Control Add-In useful. Although not essential for it to work, it is advisable to give the fields unique titles

You can also use that add-in to add 'editors' to the fields and protect the form as read only. You can then only enter text in the fields.
__________________
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
  #5  
Old 03-06-2023, 09:17 AM
Mullin4414 Mullin4414 is offline Help making a Macro Windows 11 Help making a Macro Office 2019
Novice
Help making a Macro
 
Join Date: Feb 2023
Posts: 3
Mullin4414 is on a distinguished road
Default Thanks so much for your help

I greatly appreciate you're help with all this!
Reply With Quote
Reply

Tags
highlighting, macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a Outlook macro that opens a Excel Workbook bjornltd Outlook 0 07-03-2021 06:39 AM
Help making a Macro Macro Question: Need help making a macro to highlight the first word in every sentence LadyAna Word 1 12-06-2014 10:39 PM
Help making a Macro Making a macro run for each page in a mailmerge bakhesh Mail Merge 1 09-29-2013 07:58 PM
Help making a Macro Need help making a macro to space images brewsta Word VBA 3 12-11-2012 03:29 PM
Help making a Macro Making a Macro "autoopen" Joshocom Word 1 03-16-2010 05:03 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft