#1
|
|||
|
|||
Replace text with Quick Part using VBA
I'm pretty new to VBA so please bear with me! I'm attempting to streamline a Word document that I use over and over and over. I've written a macro to fix the grammatical errors that are always present but I need to replace a couple separate instances of text with a person's name throughout the document. For example, I need to replace both "your name" and "student" with "Ann Robertson". The actual name like "Ann Robertson" is already in the document inside a quick part named "Person". I'm using quick parts because occasionally the name will change, e.g. from Ann Robertson" to "Ann M. Robertson". Is it possible to do a sort of find and replace w/ VBA? I've found the "insert quickpart" code but I'm too new to know what to do with it. Thank you all in advance for your time. The knowledge I've gained on this forum is great! |
#2
|
|||
|
|||
There are a couple of ways to do this. My VBA Find and Replace Add-In has this feature: http://gregmaxey.com/word_tip_pages/...d_replace.html
The first macro below uses the process where the building block is inserted in a temporary document then cut to the clipboard. The clipboard content is then used as the "Replace with" and you can do a global replacement. The second macro simply inserts the builiding block at the found ranges. It assumes that your building block is in the attached template. If that isn't the case, you will have to change the code. PHP Code:
|
#3
|
|||
|
|||
Thank you Greg, that worked beautifully. I misspoke in my initial question by referencing "quick parts". After reading through some of your site I think what I want to insert is a content control (document property??) throughout the document at the specific places where the text "student" or "your name" appears as you did here:
strFind() = Split("student|your name", "|") (I would like to be able to change the text using the content control (or Document property) at a later date. When I created this document I "stole" one of the document property fields and renamed it. It's nice to be able to type the name in one spot and have it populate thoughout the document.) Thank you again for your help! |
#4
|
|||
|
|||
I don't know if it would be easier/better to write the code so that it places the content controls wherever the macro finds "student" or "your name" and then have a pop-up that requests the actual name like "amy robertson"? I'm already in over my head so any help/suggestions are appreciated. Once the content controls are placed it's just as easy to type in the name and populate the remaining fields that way.
|
#5
|
|||
|
|||
dmarie,
I'm over my head all the time, but isn't it more fun in the deep end? Document Properties (e.g., title, subject, comments, etc.) are stored in one of three built-in customXMLParts in a document. So, if you want you can find text and replace it with a content control and then bind that CC to the customXMLpart data node: PHP Code:
|
#6
|
|||
|
|||
I do like the deep end...hahaha! Thank you Greg for your help with this!!
I have a few questions (big shocker, I know ) 1. The macro works for but it's only changing the first instance of "This student". When I first ran it it worked for every instance... I get a Run-time error 4198? When I debug the following line is highlighted: Set oCC = ActiveDocument.ContentControls.Add(wdContentContro lText, oRng) I have other document properties in the word doc, I'm not sure if that's problematic, I wouldn't think so but see previous post about "in over my head"? 2. Can I add more parameters? Like this: strFind() = Split("This student|student|STUDENT|this Student|This Student", "|") and then change the MatchCase to True? 3. I'm wondering if there is a Not statement I can tie in? In SQL you can basically say look for "student" but not "my student" and then when you find the correct one replace it with "Ann Smith". Is selective replacing possible w/ VBA? The word "student" appears in areas of general text and should not be changed to the proper name (that is now tied to the content control). Also, the word "students" appears randomly and I don't want that to change. I tried changing it from False to True for .MatchWholeWord but then it didn't recognize both terms...It's not specific areas or I would be asking for help with ranges haha. Thank you in advance (again) for your help. I'm trying to figure these things out before I post. So far I just have a headache . Last edited by dmarie123; 12-12-2012 at 12:03 PM. |
#7
|
|||
|
|||
It continues to run here. The error sounds like the range spans an existing CC. Start with a fresh version of the document and see if it won't work:
PHP Code:
|
#8
|
|||
|
|||
Thanks Greg, that did the trick. Is there a way to exclude text in the search parameters?
|
#9
|
|||
|
|||
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey Dim strFind() As String Dim i As Long Dim oRng As Word.Range strFind = Split("this student|a student|my student|student", "|") For i = 0 To UBound(strFind) Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Text = strFind(i) .MatchWholeWord = True .MatchCase = False While .Execute oRng.Select If MsgBox("Process this instance", vbYesNo, "Process?") = vbYes Then oRng.Text = "whatever" End If Wend End With Next i End Sub |
#10
|
|||
|
|||
Thank you!!
|
#11
|
|||
|
|||
I know this thread is really old, but this code looks close to what I need, but I'm wanting to add a MS SharePoint server document property and this code seems to be adding something different.
Would appreciate some assistance. Thanks in advance :-) Tim |
Tags |
content control, vba in microsoft word, word 2007 |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to find text and replace with form field containing that text | iiiiifffff | Word VBA | 16 | 06-04-2016 01:47 AM |
How to Rename a Quick Part | sleake | Word | 14 | 10-14-2013 09:21 AM |
quick replace, sort, change columns | userman | Excel | 1 | 05-01-2012 06:24 AM |
Quick change for all text boxes in a presentation | tmlander | PowerPoint | 1 | 03-09-2012 01:20 AM |
Date display in Mod quick part header | Jrul John | Word | 1 | 08-02-2010 12:17 PM |