Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Business Applications
Tag Cloud
access audio blue screen boot bsod connection crash dell desktop driver drivers dvd email error excel firefox hard drive hardware hdmi hijackthis internet keyboard laptop malware monitor motherboard network networking outlook problem ram recovery router screen slow sound spyware tdlwsp.dll trojan upgrade vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Business Applications >
Word Macro: loop through end of file?

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
30-Apr-2009, 05:42 PM #1
Word Macro: loop until end of file?
hello,

how do i loop an instruction until the end of the document ?
i wasn't able to find the right way using google :\

please help

Last edited by kejo : 01-May-2009 02:08 PM.
Rollin_Again's Avatar
Distinguished Member with 3,728 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
30-Apr-2009, 07:18 PM #2
Explain in detail what the macro is going to be doing.

Regards,
Rollin
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
30-Apr-2009, 07:21 PM #3
it's going to find a string and replace.
my macro will do it once.
i want it to find and replace all the strings in my file (until the end of file)
Rollin_Again's Avatar
Distinguished Member with 3,728 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
30-Apr-2009, 11:02 PM #4
Why not use the wdReplaceAll method??

Code:
Sub ReplaceAll()

With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Execute findtext:="SearchText", replaceWith:="ReplaceText", Replace:=wdReplaceAll
End With

End Sub
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
01-May-2009, 05:40 AM #5
thank you Rollin_Again, but i need to learn how to loop untill End of File.
can you please tell me how to do this?
Rollin_Again's Avatar
Distinguished Member with 3,728 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
01-May-2009, 12:33 PM #6
You still need to explain exactly why and how you want to loop through the document. Do you want to loop through line by line and look for a certain phrase or word. Do you want to loop through word by word? You can try the code below. It looks for the built in bookmark called "EndOfDoc"

Code:
  
Sub LoopDoc()
     
Selection.HomeKey Unit:=wdStory
Selection.HomeKey Unit:=wdLine

i = 1

Do Until ActiveDocument.Bookmarks("\Sel").Range.End = _
ActiveDocument.Bookmarks("\EndOfDoc").Range.End
MsgBox ("You are on line " & i)
Selection.MoveDown
i = i + 1
Loop
     
MsgBox ("You have reached the end of the document")
     
End Sub
Regards,
Rollin

Last edited by Rollin_Again : 01-May-2009 12:58 PM.
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
01-May-2009, 01:23 PM #7
ok thank you very much Rollin_Again, what i want to do is:
find every underlined string in the doc and make the first char of the found string red
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
01-May-2009, 02:12 PM #8
Quote:
Originally Posted by Rollin_Again View Post
You still need to explain exactly why and how you want to loop through the document. Do you want to loop through line by line and look for a certain phrase or word. Do you want to loop through word by word? You can try the code below. It looks for the built in bookmark called "EndOfDoc"

Code:
  
Sub LoopDoc()
     
Selection.HomeKey Unit:=wdStory
Selection.HomeKey Unit:=wdLine

i = 1

Do Until ActiveDocument.Bookmarks("\Sel").Range.End = _
ActiveDocument.Bookmarks("\EndOfDoc").Range.End
MsgBox ("You are on line " & i)
Selection.MoveDown
i = i + 1
Loop
     
MsgBox ("You have reached the end of the document")
     
End Sub
Regards,
Rollin
i am sorry to tell you that your code generates an infinite loop
Rollin_Again's Avatar
Distinguished Member with 3,728 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
01-May-2009, 03:11 PM #9
I modified the original code that I posted. Are you using the updated code as it appears in the post above or are you using the original code?

Regards,
Rollin
Rollin_Again's Avatar
Distinguished Member with 3,728 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
01-May-2009, 04:01 PM #10
Try this

Code:
Sub ChangeColor()

For Each vWord In ActiveDocument.Words

If vWord.Underline = 1 Then
vWord.Select
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Color = wdColorRed
End If

Next

End Sub
Regards,
Rollin
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
02-May-2009, 01:36 PM #11
ok your code works fine, but i need to find lines instead of words.
how to do it?
i need something like this (don't know if it exists):

Code:
For Each vLine In ActiveDocument.Lines
Rollin_Again's Avatar
Distinguished Member with 3,728 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
02-May-2009, 02:13 PM #12
I'm still a bit unclear on what you are trying to accomplish. Can you please provide more explanation on why you want to search line by line? Will the underlined strings you are searching for consist of more than one word?

Regards,
Rollin
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
02-May-2009, 04:20 PM #13
I have many numbered lists like this one:

Code:
1. first item
2. second item
3. third item
and want to make red the number of the underlined line, like this:

Code:
1. first item
2. second item
3. third item
how can i do this?
i have tried some solutions but none worked.
hope you guys are able to help me.

thanks in advance.

Last edited by kejo : 03-May-2009 08:49 AM.
kejo's Avatar
Member with 62 posts.
 
Join Date: Dec 2006
03-May-2009, 05:07 AM #14
please, anybody help, ?
Rollin_Again's Avatar
Distinguished Member with 3,728 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
03-May-2009, 08:49 AM #15
Can you post a sample document?

Regards,
Rollin
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 01:09 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.