Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-11-2012, 01:01 PM
gmaxey gmaxey is offline Replace text with Quick Part using VBA Windows 7 32bit Replace text with Quick Part using VBA Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

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:
Sub ReplaceWithAUTOTEXT()
Dim strFind As String
Dim strReplace 
As String
Dim strWildcards 
As String
Dim bWild 
As Boolean
Dim strQuery 
As String
Dim sType 
As String
Start
:
strFind InputBox("Enter the text string you want to find""Find")
If 
strFind "" Then
    strQuery 
MsgBox("You have not entered any text to find" vbCr _
    
"Or you have selected 'Cancel" vbCr _
    
"Select OK to re-try or Cancel to quit"vbOKCancel"Find")
    If 
strQuery vbOK Then
        
GoTo Start
    
Else
        Exit 
Sub
    End 
If
End If
strWildcards MsgBox("Use Wildcards"vbYesNo"Find")
If 
strWildcards 6 Then bWild True Else bWild False
GetInput
:
On Error GoTo Oops 'Handle incorrect AutoText request
'
Create a scratch pad
Documents
.Add
If Application.Version 12 Then
'Word 2007 - Use the Building Blocks Organizer
    Dialogs(GetDialog).Show
    sType = "Building Blocks" '
msgbox title
Else
'Not Word 2007 - Use the Autotext dialog.
    Dialogs(wdDialogEditAutoText).Show
    sType = "Autotext" '
msgbox title
End 
If
'Cut the inserted entry to the clipboard
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
'
crumple up scratch pad :-)
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Replace found text with the clipboard contents.
With Selection.Find
    .ClearFormatting


    .Replacement.ClearFormatting
    .Text = strFind
    .Replacement.Text = "^c"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = bWild
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
End With
End
Oops: '
Error handler
ActiveDocument
.Close SaveChanges:=wdDoNotSaveChanges
strQuery 
MsgBox("Select 'OK' to reselect the " sType _
" entry then click 'Insert'" vbCr vbCr _
"Click 'Cancel' to exit"vbOKCancelsType)
If 
strQuery vbOK Then
    Resume GetInput
End 
If
End Sub
Sub ReplaceWithAUTOTEXTII
()
Dim oRng As Word.Range
Dim strFind
() As String
Dim i 
As Long
strFind
() = Split("student|your name""|")
For 
0 To UBound(strFind)
  
Set oRng ActiveDocument.Range
  With oRng
.Find
    
.ClearFormatting
    
.Replacement.ClearFormatting
    
.Text strFind(i)
    .
Forward True
    
.Wrap wdFindContinue
    
.Format False
    
.MatchCase False
    
.MatchWholeWord False
    
.MatchWildcards False
    
.MatchSoundsLike False
    
.MatchAllWordForms False
    
While .Execute
      ActiveDocument
.AttachedTemplate.BuildingBlockEntries("Confidential").Insert Where:=oRngRichText:=True
    Wend
  End With
Next
End Sub 
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Tags
content control, vba in microsoft word, word 2007



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace text with Quick Part using VBA 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:37 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