1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Word Macro: loop through end of file?

Discussion in 'Business Applications' started by kejo, Apr 30, 2009.

Thread Status:
Not open for further replies.
Advertisement
  1. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    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
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,732
    Explain in detail what the macro is going to be doing.

    Regards,
    Rollin
     
  3. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    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)
     
  4. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,732
    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
     
  5. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    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?
     
  6. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,732
    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
     
  7. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    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
     
  8. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    i am sorry to tell you that your code generates an infinite loop :(
     
  9. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,732
    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
     
  10. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,732
    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
     
  11. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    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
     
  12. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,732
    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
     
  13. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    I have many numbered lists like this one:

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

    Code:
    [COLOR=Red]1[/COLOR]. [U]first item[/U]
    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.
     
  14. kejo

    kejo Thread Starter

    Joined:
    Dec 14, 2006
    Messages:
    70
    please, anybody help, ?
     
  15. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,732
    Can you post a sample document?

    Regards,
    Rollin
     
  16. Sponsor

As Seen On
As Seen On...

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.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/823244