Microsoft Office Forums Can I find-and-replace text in all Quickparts at once?

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2019, 11:40 AM
Steve Kunkel Steve Kunkel is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Novice
Can I find-and-replace text in all Quickparts at once?
 
Join Date: May 2019
Location: Seattle area
Posts: 16
Steve Kunkel is on a distinguished road
Default Can I find-and-replace text in all Quickparts at once?

Hi Folks,


I have a nifty VBA-enabled Word docx that I use for writing reports at work. Upon opening it, a dialog prompts me for information about the person I'm writing a report about, then replaces "[n]" with the person's name, and other target strings ([e],[m], etc) get changed to male or female pronouns. Then the report gets saved as the person's name.

I'd like to start using Quickparts in my report boilerplate tool. An example Quickpart I might have is "[n] was a pleasure to work with." So what I want is that when that VBA code is run, I'd like it to change all of the "[n]" and other target strings that are in all of the Quickparts too. All in one fell swoop, so to speak.

Does that make sense?

EDIT: Found this
https://www.thedoctools.com/word-mac...otext-via-vba/
Might work.
Reply With Quote
  #2  
Old 05-09-2019, 03:42 PM
gmaxey gmaxey is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 902
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

When you insert quickpart in a document you are left with just inserted content (it isn't a quickpart anymore). After you create your document, try something like this:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim arrTerms() As String
Dim lngIndex As Long
Dim oRng As Range
Dim strReplace As String
  arrTerms = Split("[n],[e],[m]", ",")
  For lngIndex = 0 To UBound(arrTerms)
    strReplace = InputBox("What do you want to replace " & arrTerms(lngIndex) & " with?")
    Set oRng = ActiveDocument.Range
    With oRng.Find
      .Text = arrTerms(lngIndex)
      .Replacement.Text = strReplace
      .Execute Replace:=wdReplaceAll
    End With
  Next lngIndex
lbl_Exit:
  Exit Sub
  
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 05-09-2019, 03:55 PM
Guessed's Avatar
Guessed Guessed is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,240
Guessed has a spectacular aura aboutGuessed has a spectacular aura aboutGuessed has a spectacular aura about
Default

I would have thought that this would be an opportunity to use linked content controls and include those in the building blocks where appropriate.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 05-09-2019, 04:17 PM
gmaxey gmaxey is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 902
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

Andrew,


Yes that is certainly a possibility.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 05-09-2019, 07:40 PM
Steve Kunkel Steve Kunkel is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Novice
Can I find-and-replace text in all Quickparts at once?
 
Join Date: May 2019
Location: Seattle area
Posts: 16
Steve Kunkel is on a distinguished road
Default

Thanks for the replies folks.

Greg your code is very cool! Clever how it steps through the different replacements. Often though, a report is built piece by piece over time. It would be laborious to to all of the replacements each time. To get a better idea of what I mean, here is a screenshot that shows the dialog which pops up when the boilerplate .docm is opened:


There's one radio button that changes three pronouns (he/him/his) and a couple of other things that minimizes the number of clicks needed.

There's a 22+ minute screencast [URL="https://www.youtube.com/watch?v=ebFeD5AKSpU&feature=youtu.be&fbclid=IwAR2e vyqMqAIbxwmIp4KnHygGqE2CSiM9J2KoVmHPFUhhjnuoj247nf mvqDs"]of it here. Though you guys probably get the gist of it just from the screenshot.

The file is here, if you want to look at the insides.

So by the time the dialog/form is closed, all of the replacements have been made. My current setup is that I just have tons of boilerplate statements in there, then I cull out the ones I don't want to use for a particular report. Instead, I'd like all of those statements stored as Quickparts. It would just be easier if all of the Quickparts had changes already made.

It's worth mentioning the "AutoText" might also work. I don't think AutoText allows tables though, which is unfortunate. I did try this code. But had problems with it.
Reply With Quote
  #6  
Old 05-09-2019, 08:00 PM
Guessed's Avatar
Guessed Guessed is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,240
Guessed has a spectacular aura aboutGuessed has a spectacular aura aboutGuessed has a spectacular aura about
Default

Steve
I don't think editing the Building Blocks in anticipation of using them in the current doc is a good idea. For starters, the template should be 'document agnostic' because I might have multiple docs open at the same time and if I had hard coded the [n], [e] and [m] fields in the template, it might only be useable in one of those documents until the macro is used to reset them to suit another document.

Investigate linked content controls and then put those into the template building blocks. As an introduction to this, go to Insert > Quick Parts > Document Property and pick a couple of those entries. Now embed those into some random bits of text (such as a couple of your building blocks. Notice how if you place two instances of the same linked CC into a doc, you can edit either one and the other one automatically updates. Now go to a second document and paste that same content into that document, and edit the CC there to see that the same CC can show different values in different docs - because the linked metadata can be different in both.

Including linked CCs in your quick part building blocks allows you to set the value of the document property so that any subsequent content pasted in which includes a CC linked to the same property will automatically show the right info.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 05-10-2019, 11:42 AM
Steve Kunkel Steve Kunkel is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Novice
Can I find-and-replace text in all Quickparts at once?
 
Join Date: May 2019
Location: Seattle area
Posts: 16
Steve Kunkel is on a distinguished road
Default

Thanks for the feedback. Actually one of the things I wanted to ask is whether editing the building blocks is even a good way to go about this. I will definitely check out the content controls feature.
Reply With Quote
  #8  
Old 05-10-2019, 01:17 PM
Steve Kunkel Steve Kunkel is offline Can I find-and-replace text in all Quickparts at once? Windows 10 Can I find-and-replace text in all Quickparts at once? Office 2016
Novice
Can I find-and-replace text in all Quickparts at once?
 
Join Date: May 2019
Location: Seattle area
Posts: 16
Steve Kunkel is on a distinguished road
Default

I don't think the linked content controls will be the way to go. They definitely could be used for this feature, but they problem is they are a bit cumbersome to insert. Part of the reason I settled on [n] rather than the more traditional %FirstName% is because it can be typed fluidly, which is a big part of my groove.

A combination of your, and Greg's suggestions gave me an idea though... When my GUI dialog opens and collects its data, I'll have it also assign some custom Document Properties such as
FirstName = Jon
LastName = Doe
and so on.

Then a second macro can be run for an already in-process report. One click of a button can do all the changes (find [n]; replace with FirstName) and so on.

So I guess this forum topic/idea is a dead end. Thank you guys again!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I find-and-replace text in all Quickparts at once? Find and replace a text with a field eduzs Word VBA 6 02-23-2018 05:19 PM
Can I find-and-replace text in all Quickparts at once? How To find Text and replace with equation maamiradina Word 2 12-17-2016 11:08 PM
Can I find-and-replace text in all Quickparts at once? Macro to find text and replace with form field containing that text iiiiifffff Word VBA 16 06-04-2016 01:47 AM
Macro to find coloured text and replace with form-field/formtext containing that text tarktran Word VBA 1 11-26-2014 08:12 AM
Can I find-and-replace text in all Quickparts at once? Find and replace a string of text errtu Word 1 01-31-2013 02:09 PM


All times are GMT -7. The time now is 05:07 AM.


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