Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LAST TIME: HOW DO I MAKE THIS MACRO LOOP

Status
Not open for further replies.

tombaraider

Technical User
Feb 20, 2004
20
US
Sorry for not making things clearer in my previous posts and my explanation as to why the problem has not been solved would be too wordy. So I apologize for my previous posts on this and truly appreciate previous input. I finally have this macro perfected like I want. The macro was created in Word and once a document is opened it arrows down twice, finds the word "REPORT", and once it's found, close search window, cursor up one line and press End key to get to the end of that line then Ctrl-Enter to force a page break. Please tell me exactly what and where to put it into this macro to make it keep running until the end of the file and stop. Thank you so much. Here is the code:

Sub Macro1()
'
' Macro1 Macro
'
'
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "REPORT"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.EndKey Unit:=wdLine
Selection.InsertBreak Type:=wdPageBreak
Selection.MoveDown Unit:=wdLine, Count:=2
End Sub

 
How about uploading a sample Word document that has all the stuff in it that this macro needs.

Also state what the finished product should look like page break wise.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
You still show no evidence that you've actually tried what you've already been given. IMHO you're just wasting everyone's time...

Cheers
Paul Edstein
[MS MVP - Word]
 
strongm's suggestion in the previous threads does exactly this albeit with less screen flickering and much faster.
Have you tried it?
If so: what exactly does it NOT do the way you want it to?
once a document is opened
have you tried putting it in a module in your document like this
Code:
Sub AutoOpen()
...
End Sub
?

P.S: Please respond here, do not open yet another thread, especially not all-caps or with an added "!Arghhh!" [yinyang]

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Here's your macro (it's just a find/replace that you really don't need to code as a macro):
Code:
Sub Annual_Report()

'
' Annual_Report Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = _
            "^p                                             ANNUAL REPORT "
        .Replacement.Text = "^m^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
You have repeatedly stated that REPORT was the key word, but it is NOT! Your requirement is incorrect!
Actually, tombaraider specified it as 'Report', but the document has 'REPORT', which makes all the difference with a wildcard Find/Replace. Whether it's 'REPORT' or 'ANNUAL REPORT' makes no difference for the F/R unless one needs to differentiate between the two. It can all be done with a wildcard Find/Replace, where:
Find = (^13)([!^13]@REPORT@[^13])
Replace = \1^012\2
or:
Find = (^13)([!^13]@ANNUAL REPORT@[^13])
Replace = \1^012\2

No macros or loops required! Of course, if tombaraider really, really, really wants a macro, there's always the macro recorder...



Cheers
Paul Edstein
[MS MVP - Word]
 
Right, so a very, very minor modification to the code I presented to you earlier works with your new criteria. As with macropod's solution it still does not need a loop.

Code:
[blue]Selection.Find.Replacement.ParagraphFormat.PageBreakBefore = True
With Selection.Find
    .Text = "report"
    .Replacement.Text = ""
    .Forward = True
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll[/blue]

 
Well, the OP code at the very beginning of this thread does something different.
I actually opened a word document, typed =rand() to get some sample text, 'sprinkled' some 'REPORT' words in it and tried the macro.

Yes, it does Find on the word "REPORT", but then
[ul]
[li]moves the cursor one line up[/li]
[li]moves the cursor to the end of the line[/li]
[li]inserts a page break[/li]
[li]moves the cursor 2 lines down[/li]
[/ul]
That's a different action that simply "replace word REPORT with a PageBreak"...

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
I apologize. [blush]

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Like many things, there's more than one way to skin a cat. Unfortunately, the OP was not clear on what they needed, nor that helpful on the format of the document. If they had provided the examples of the before and after like they finally did, this would have been solved on day 1 with minimal issues and only 1 forum topic instead of 3.
 
zeglar your code worked perfectly for the file that I posted. But please understand that this file has been altered to protect confidential information. I only put the word "ANNUAL" before "REPORT" in the document. So please disregard "ANNUAL". In the real document there is another word that preceeds "REPORT" but let's forget about that word. The macro needs to find occurrence of "REPORT" then proceed from there. Please see my original post here for clearest instructions that will work in any document structured like this where only REPORT needs to be found. Thank you so much.
 
BTW, I am NOT A CODER. The macro is being recorded in Word. Of course, I can tweak it in VBA.
 
And my code doesn't care what the word is. Do you actually try any of the suggestions? Or just the first one that catches your eye?

>Of course, I can tweak it in VBA.

You've been given several VBA solutions that only require minor tweaking between your changes of requirements. So tweak ...
 
Actually strongm I tried your solution next, and it worked like a charm. In fact, so well I sent it to my client and am waiting for his response. After I got the OK from him I was going to post my appreciation to you. And I thank you so much. When I was younger I used to relate with unnecessary attitude. And it was so damn easy. I am more peaceful and respectful now, knowing that many times there is more to something than just our perception. But I must say that there are those who think they provided me with the solution but actually did not through misunderstanding. As you know reading and presenting textual info is far different and ambiguous that talking. My steps in the first post here are flawless in explaining what I needed. And thanks again to you. If my client (and that's another variable in this equation) likes your solution, I must say you have made my day.
 
zeglar, At first I blew off posting my files because of the info in them and so as not to get myself in trouble. When I was finally left with no choice I did the busywork of doctoring the file. If this was not the case, I would have posted only one thread and got my solution faster. Nevertheless, I know now how to keep things to one thread. Thanks for your time and attention.
 
I must say that there are those who think they provided me with the solution but actually did not through misunderstanding.
No, the problem was you specified something other than what you actually required. Don't try to shift the blame to anyone else.

Cheers
Paul Edstein
[MS MVP - Word]
 
strongm my client confirmed today that your solution worked perfectly. I am sure other solutions were valid, too. Many thanks to you and to all who offered assistance and endured my confusion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top