Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 18th, 2013, 11:53 AM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old March 18th, 2013, 12:42 PM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
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)
Reply With Quote
  #3 (permalink)  
Old March 18th, 2013, 01:12 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #4 (permalink)  
Old March 19th, 2013, 12:51 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
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)
Reply With Quote
  #5 (permalink)  
Old March 19th, 2013, 01:11 PM
Registered User
Points: 17, Level: 1
Points: 17, Level: 1 Points: 17, Level: 1 Points: 17, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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)
Reply With Quote
  #6 (permalink)  
Old March 20th, 2013, 05:09 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:19 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.