dcsimg
Results 1 to 12 of 12

Thread: I need Help with word Maco Looping until the end of the document

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    7

    Question I need Help with word Maco Looping until the end of the document

    Hi everyone I have I have the below Macro and I need some help. If this is not is the correct thread please let me know.

    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=20
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace

    I would like to have the below loop until the end of the document. The document will never have the same amount of pages from day to day.
    Selection.MoveDown Unit:=wdLine, Count:=20
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    7

    Re: I need Help with word Maco Looping until the end of the document

    This is a macro for word.

  3. #3
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,427

    Re: I need Help with word Maco Looping until the end of the document

    A clear explanation of what exactly task(s) you are wanting the macro to perform for you on the document would be most helpful to us in order to provide assistance.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    7

    Re: I need Help with word Maco Looping until the end of the document

    Quote Originally Posted by jdc2000 View Post
    A clear explanation of what exactly task(s) you are wanting the macro to perform for you on the document would be most helpful to us in order to provide assistance.
    The macro is going to run in a document that I have to will help me format it so when I print the document to trifold paper it will line up correctly.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,446

    Re: I need Help with word Maco Looping until the end of the document

    i would assume that you used the macro record to get this code, which is ok, but hard for anyone to know what you actually want to do

    can you post sample documents of before and desired result? zip them first, then attach to post

    you could try like
    Code:
    do
    pos = selection.start
    Selection.MoveDown Unit:=wdLine, Count:=20
    if selection.start = pos or selection.start = thisdocument.content.end -1 then exit do                      '    in these cases you are at the end of the document so exit
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    loop
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    7

    Re: I need Help with word Maco Looping until the end of the document

    Quote Originally Posted by westconn1 View Post
    i would assume that you used the macro record to get this code, which is ok, but hard for anyone to know what you actually want to do

    can you post sample documents of before and desired result? zip them first, then attach to post

    you could try like
    Code:
    do
    pos = selection.start
    Selection.MoveDown Unit:=wdLine, Count:=20
    if selection.start = pos or selection.start = thisdocument.content.end -1 then exit do                      '    in these cases you are at the end of the document so exit
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    loop
    Here is the document. I first run a Remove page break macro that i found to get rid of the page breaks and then i run the below macro to format the document.

    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=20
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace

    I only need the below part to loop until the end of the document. The number of pages will never be the same from a day to day basis
    Selection.MoveDown Unit:=wdLine, Count:=20
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Last edited by starsarethebest9; Sep 12th, 2019 at 01:59 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    7

    Re: I need Help with word Maco Looping until the end of the document

    Quote Originally Posted by westconn1 View Post
    i would assume that you used the macro record to get this code, which is ok, but hard for anyone to know what you actually want to do

    can you post sample documents of before and desired result? zip them first, then attach to post

    you could try like
    Code:
    do
    pos = selection.start
    Selection.MoveDown Unit:=wdLine, Count:=20
    if selection.start = pos or selection.start = thisdocument.content.end -1 then exit do                      '    in these cases you are at the end of the document so exit
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=15
    Selection.TypeParagraph
    Selection.MoveDown Unit:=wdLine, Count:=5
    Selection.TypeBackspace
    Selection.TypeBackspace
    loop
    I have some new code that I am trying to do the same thing with. Could you help me do looping with this code, please.

    Selection.MoveDown Unit:=wdLine, Count:=28 (page 1)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=59 (page 2)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=59 (page 3)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=56(page 4)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,446

    Re: I need Help with word Maco Looping until the end of the document

    you can try this to see if it does what you want, is is completely untested

    Code:
    With ThisDocument.Content
        Do
            Set pb = .Find("^m")
            If pb Is Nothing Then Exit Do
            pb.Delete
        Loop
    End With
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    7

    Re: I need Help with word Maco Looping until the end of the document

    Quote Originally Posted by westconn1 View Post
    you can try this to see if it does what you want, is is completely untested

    Code:
    With ThisDocument.Content
        Do
            Set pb = .Find("^m")
            If pb Is Nothing Then Exit Do
            pb.Delete
        Loop
    End With
    How would that look with my macro?

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,446

    Re: I need Help with word Maco Looping until the end of the document

    How would that look with my macro?
    test if it works first, it should just find and delete all manual page breaks

    i got chance to test this code tonight, looks like it needs some fixing anyway

    edit 2: try this
    Code:
        ThisDocument.Content.Find.Execute "^m", , , , , , , , , "", wdReplaceAll
    Last edited by westconn1; Sep 13th, 2019 at 06:01 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2019
    Posts
    7

    Re: I need Help with word Maco Looping until the end of the document

    Quote Originally Posted by westconn1 View Post
    test if it works first, it should just find and delete all manual page breaks

    i got chance to test this code tonight, looks like it needs some fixing anyway

    edit 2: try this
    Code:
        ThisDocument.Content.Find.Execute "^m", , , , , , , , , "", wdReplaceAll

    I need a looping command for the below code. I already have a macro for the page break

    Selection.MoveDown Unit:=wdLine, Count:=28 (page 1)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=59 (page 2)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=59 (page 3)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=56(page 4)
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,446

    Re: I need Help with word Maco Looping until the end of the document

    i posted a loop for similar code in post #4
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width