Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Business Applications Business Applications
Search Search
Search for:
Tech Support Guy > > >

Word Macro: loop through end of file?


(!)

kejo's Avatar
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
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 at 02:08 PM..
Rollin_Again's Avatar
Member with 4,694 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Advanced
30-Apr-2009, 07:18 PM #2
Explain in detail what the macro is going to be doing.

Regards,
Rollin
kejo's Avatar
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
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
Member with 4,694 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Advanced
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
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
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
Member with 4,694 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Advanced
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 at 12:58 PM..
kejo's Avatar
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
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
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
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
Member with 4,694 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Advanced
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
Member with 4,694 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Advanced
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
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
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
Member with 4,694 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Advanced
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
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
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 at 08:49 AM..
kejo's Avatar
kejo kejo is offline
Member with 70 posts.
THREAD STARTER
 
Join Date: Dec 2006
03-May-2009, 05:07 AM #14
please, anybody help, ?
Rollin_Again's Avatar
Member with 4,694 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Advanced
03-May-2009, 08:49 AM #15
Can you post a sample document?

Regards,
Rollin
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
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.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑