In my documnet, I need to search for a style and insert a “3~” before each entry… and do this for every entry for that style. I’m only able to find the text and replace it, or find, cancel, home, insert… Search, find, etc. But if I do that, I don’t know when I’ve “found them all”. I think it has to do with some type of loop, but I’m lost… Help?
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Find/Insert (W97)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Find/Insert (W97)
- This topic has 12 replies, 2 voices, and was last updated 22 years, 5 months ago.
AuthorTopicWSmusical1
AskWoody LoungerDecember 26, 2002 at 3:07 pm #381092Viewing 0 reply threadsAuthorReplies-
WSjscher2000
AskWoody LoungerDecember 26, 2002 at 5:50 pm #640805This is a good example of where VBA offers extra flexibility you can’t tap by recording a macro. The logic of what you want to do is along these lines (although I haven’t tested it).
Set up the Find parameters, going forward only, stopping at the end.
This can be copied from a recorded macro
Execute the Find and, if Found, then…
You might be able to use:
Do While Selection.Find.Execute
Use the .InsertBefore method (I think that’s the name, but if not, it’s very similar to that) to insert the new text.
Adjust the selection if necessary.
You might want to collapse the selection to its end to avoid “re-finding” the same item. Tests will tell…
Execute again until there’s no more to be found.
If you’re using a Do loop, it’s simply:
LoopHope this helps.
-
WSmusical1
AskWoody LoungerDecember 26, 2002 at 6:16 pm #640813Yes, I wrote the following work flow for the process:
Ctrl Home
Search for style “XYZ”/ Search Down
Find Style? No=End of document, Exit Yes=InsertBefore “3~”
Next Style “XYZ”But I’m not figuring out how to code it (I’ve been playing with it)… in combination of knowing but lil programming, and my VBA crutch books being at home….
-
WSmusical1
AskWoody LoungerDecember 26, 2002 at 6:57 pm #640816In my playing ( I have no idea if I’m truly close), I tried: Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(“Heading 3”)
With Selection
.InsertBefore “3~”
.Collapse direction:=wdCollapseEnd
End With
End Sub
But it doesn’t go to the style before it insert… it just does it where the cursor is….Option 2, InsertBefore is not allowed.
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(“Heading 3”)
With Selection.Find
.InsertBefore “3~”
.Collapse direction:=wdCollapseEnd
End WithBut it doesn’t go to the style before it insert… it just does it where the cursor is….
-
WSjscher2000
AskWoody Lounger
-
-
WSjscher2000
AskWoody LoungerDecember 26, 2002 at 7:00 pm #640817Well, if you get bored working on it, try this:
Option Explicit Sub RecordedFind() ' ' RecordedFind Macro ' Macro recorded 12/26/2002 by Jefferson F. Scher ' Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting Selection.Find.Style = ActiveDocument.Styles("Body Text") Selection.Find.ParagraphFormat.Borders.Shadow = False 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 Sub FindAndPreface() 'This part will depend on your needs Dim strNewText As String strNewText = "3~" 'Set up the Find using the recorded code, ' reorganized slightly With Selection.Find .ClearFormatting .Text = "" .Style = ActiveDocument.Styles("Body Text") .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With 'Go to the top and search forward in a loop With Selection .HomeKey Unit:=wdStory Do While .Find.Execute 'A match was found, insert the new text before it .InsertBefore strNewText 'collapse the selection to the end to avoid refinding it .Collapse Direction:=wdCollapseEnd Loop End With End Sub
The first macro is purely for reference. the .Wrap parameter was changed to wdFindStop to prevent the change from running over and over. In fact, it might not do that, but I didn’t even want to risk it. Same with the selection collapse. Just safer to do that even if the macro would run correctly without it…too many weird experiences with Find loops.
-
WSmusical1
AskWoody Lounger -
WSjscher2000
AskWoody LoungerDecember 26, 2002 at 7:31 pm #640821Well… I don’t know. Could be something that was “fixed” in Word 2000, so I didn’t experience that. You usually can interrupt running VBA code with Ctrl+Break.
In theory, you could add this to your loop as a failsafe:
With Selection .HomeKey Unit:=wdStory Do While .Find.Execute If Left(.Text, Len(strNewText)) = strNewText Then Exit Do 'A match was found, insert the new text before it .InsertBefore strNewText 'collapse the selection to the end to avoid refinding it .Collapse Direction:=wdCollapseEnd Loop End With
But I haven’t actually tested it. Does this help?
-
WSmusical1
AskWoody LoungerDecember 26, 2002 at 7:53 pm #640822Hmmm. ….
When I closed my MSO, I ofcourse hadn’t saved prior to running the script, so I had to recopy the script back into the template… now when I run it, I get ‘Could not open macro storage runtime error 5981’ I didn’t get this before. The code is haning up on line:
.Style = ActiveDocument.Styles(“Heading 1”) ‘and this IS an existing style -
WSmusical1
AskWoody Lounger -
WSmusical1
AskWoody Lounger -
WSmusical1
AskWoody Lounger -
WSjscher2000
AskWoody LoungerDecember 26, 2002 at 11:46 pm #640854Okay, that explains it. Yet another of those things that will trip up the hasty macro author…one can never get “past” the last paragraph by collapsing a selection that includes it. To create an escape valve for that, you’d need to do something like:
If .Start = ActiveDocument.Content.End – 1 Then Exit Do
This will identify a find result that is limited to the final paragraph mark (i.e, Selection.Start is the second-to-last character in the body of the document).
Or, you could just be very careful what document you run it in.
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Firefox 139
by
Charlie
13 minutes ago -
Who knows what?
by
Will Fastie
2 hours, 16 minutes ago -
My top ten underappreciated features in Office
by
Peter Deegan
17 minutes ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
7 hours, 9 minutes ago -
Misbehaving devices
by
Susan Bradley
23 minutes ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
1 day ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
1 hour, 9 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
23 hours, 22 minutes ago -
Discover the Best AI Tools for Everything
by
Alex5723
23 hours, 31 minutes ago -
Edge Seems To Be Gaining Weight
by
bbearren
13 hours, 40 minutes ago -
Rufus is available from the MSFT Store
by
PL1
21 hours, 42 minutes ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
2 days ago -
KB5061768 update for Intel vPro processor
by
drmark
22 minutes ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
2 hours, 41 minutes ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
1 day, 20 hours ago -
Office gets current release
by
Susan Bradley
1 day, 22 hours ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
3 days, 13 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
2 days, 21 hours ago -
Stop the OneDrive defaults
by
CWBillow
3 days, 13 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
3 days, 23 hours ago -
X Suspends Encrypted DMs
by
Alex5723
4 days, 1 hour ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
4 days, 2 hours ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
4 days, 2 hours ago -
OpenAI model sabotages shutdown code
by
Cybertooth
4 days, 3 hours ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
3 days, 15 hours ago -
Enabling Secureboot
by
ITguy
3 days, 22 hours ago -
Windows hosting exposes additional bugs
by
Susan Bradley
4 days, 11 hours ago -
No more rounded corners??
by
CWBillow
4 days, 7 hours ago -
Android 15 and IPV6
by
Win7and10
3 days, 20 hours ago -
KB5058405 might fail to install with recovery error 0xc0000098 in ACPI.sys
by
Susan Bradley
4 days, 23 hours ago
Recent blog posts
Key Links
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.