#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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 |
#3
|
||||
|
||||
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 |
#4
|
|||
|
|||
Andrew,
Yes that is certainly a possibility. |
#5
|
|||
|
|||
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. |
#6
|
||||
|
||||
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 |
#7
|
|||
|
|||
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.
|
#8
|
|||
|
|||
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! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find and replace a text with a field | eduzs | Word VBA | 6 | 02-23-2018 05:19 PM |
How To find Text and replace with equation | maamiradina | Word | 2 | 12-17-2016 11:08 PM |
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 |
Find and replace a string of text | errtu | Word | 1 | 01-31-2013 02:09 PM |