Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2013, 11:53 AM
Registered User
 
Join Date: Mar 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Word 2007: Add & Remove Some Part of Letters/Word

Hi,

This is driving me crazy. I keep trying to create a vba to remove and add a word but it's not working. I'm a newbie on VBA and keep surifng to find something like what I have but I couldn't find anything. What I'm trying to do is:

1. The word "Done" is incomplete on my text table (header section). What I have is "Do" instead of "Done". so I need to have a VBA to automatically add "ne" with it.

2. Remove the word "Pass" from the header.

Your help is highly appreciated.


Thanks so much
n
 
Old March 18th, 2013, 12:42 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

You need to use Selection.Find / Document.Find method to find and replace a word. You need to do this cautiously (keep wholewords for Do to Done etc..)

Here are some good links:

http://word.mvps.org/faqs/customizat...ceAnywhere.htm

http://social.msdn.microsoft.com/For...-b84c4958f9db/


Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old March 18th, 2013, 01:12 PM
Registered User
 
Join Date: Mar 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Shasur,

I found the first one as my solution. Thanks so much for the links :)

I have a quick question though. What if I need to find two different words to replace? Is there a way to put them together in one macro? If yes, how? Let's say I need to find:

1. find text - replace by "Im found"
2. find number - repalce by "Number found"



Sub FindAndReplaceFirstStoryOfEachType()

Dim rngStory As Range

For Each rngStory In ActiveDocument.StoryRanges

With rngStory.Find

.Text = "find text"

.Replacement.Text = "I'm found"

.Wrap = wdFindContinue

.Execute Replace:=wdReplaceAll

End With

Next rngStory

End Sub


Thanks,
netchie
 
Old March 19th, 2013, 12:51 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Netchie

you can have an array with the find and replacement text. Then loop through the array to iteration.

If you are using selection.find then use selection.homekey before each iteration


Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old March 19th, 2013, 01:11 PM
Registered User
 
Join Date: Mar 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Shasur! :)

How about let's say the word is not always the same to change? I tried the VBA for a wrong word to change from "Desc" to "Description" but it changed the following:

1. Desc to Description (which is correct)
2. Descr to Descriptioncr (which the extra cr still intact)
3. Descrip (which is rip still intact)
 
Old March 20th, 2013, 05:09 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Netchie

You need to use the WholeWord and other options that come with the Find

ActiveDocument.Content.Find.MatchWholeWord = True

of

Selection.Find.MatchWholeWord = True

will ensure that Desc will search for Desc alone, Description, Describe will not be searched

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting Image to each page in word 2007 massoudshakeri Word VBA 2 March 18th, 2013 02:04 PM
XSLT Problem with Word 2007 Half-Saint XSLT 1 January 6th, 2010 07:49 AM
Word 2007 VBA validation of formfield beth.harless Word VBA 0 August 18th, 2009 02:24 PM
Word 2007, undo, VBA FredrikH Word VBA 0 April 16th, 2009 04:20 AM
Copying some part of the doc using macro in word Lakshmi KS Access VBA 2 December 5th, 2003 05:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.