Greg
Fri Sep 14 16:55:46 CDT 2007
Elaine,
No I really can't make much sense of it.
What exactly are your trying to loop? Do you want to add 3 new sections to
the document with each loop? How many sections does the document have to
begin with?
Just a stab,
If "Refer to:" is the key identifier of many notices then putting most of
your code in a
While .Execute
Wend
Statement, may be the place to start.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
"Elaine J." <ElaineJ@discussions.microsoft.com> wrote in message
news:E7A28B94-0045-4C75-8984-7CC946B9FE0A@microsoft.com...
> Sorry, Greg, It's a little long, but hopefully you can tell what is going
> on.
> I do document mine with plenty of notes. Basically this is what is
> happening: We use a document generation program in Word that create a
> single
> document that contains notices that we sent to customers. Each notice is
> 3
> to 5 sections long. First I have to determine whether the notice is 3, 4,
> or
> 5 sections based on information that is unique to each section. After I
> determine how many sections a notice is, it is printed. Then the macro
> calls
> another macro that prints some forms. Then the notice is deleted. It
> then
> goes through the same steps with the second notice (that is now the first
> notice).
>
> The problem that I am having, is the macro works -- once (i.e. I can run
> it
> for the first notice, then run it again for the second notice and keep
> clicking run until I am done. It also works if I step through it. But if
> I
> try to add any kind of loop to it, it kind of scrambles the notices. I
> have
> wondered if it is going too fast (is that possible?). I wondered if maybe
> I
> need to somehow pause it between each notice?
>
> If you need the macro that is called, I can send that too.
>
> Thanks for any help you can give me.
>
> Sub altNewPrintNOH()
>
> 'Print the Notice of Hearing.
> 'Requires the forms "hrformsrep.doc and hrformsnorep to be in the user's
> K:\
> 'Updated 9/10/07 to accomodate EF procedures.
>
>
>
>
> Application.ScreenUpdating = true
>
> Dim notice As Range
> Dim printnotice As Range
>
> Dim notice3 As Range
> Dim printnotice3 As Range
>
> Dim notice4 As Range
> Dim printnotice4 As Range
>
> Dim notice5 As Range
> Dim printnotice5 As Range
>
> Dim acknotice As Range
> Dim blankpage As Range
>
> Dim electronic As Boolean
> Dim rep As Boolean
> Dim expert As Boolean
> Dim concurrent As Boolean
>
>
> Selection.EndKey Unit:=wdStory
>
> Selection.InsertBreak Type:=wdSectionBreakNextPage
> Selection.InsertBreak Type:=wdSectionBreakNextPage
> Selection.InsertBreak Type:=wdSectionBreakNextPage
>
>
>
> Selection.HomeKey Unit:=wdStory
>
> With Selection.Find
> .ClearFormatting
> .Text = "Refer to:"
> .Forward = True
> .Wrap = wdFindStop
> End With
>
> If Selection.Find.Execute = False Then
> End
> End If
>
>
>
> 'this checks for occasional blank pages left behind.
>
> Set blankpage =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(1).Range.End)
>
> With blankpage.Find
> .ClearFormatting
> .Text = "Refer To:"
> .Forward = True
> .Wrap = wdFindStop
> End With
>
> If blankpage.Find.Execute = False Then
> Set blankpage =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(1).Range.End)
> blankpage.Delete
>
> End If
>
> Selection.HomeKey Unit:=wdStory
>
>
> '*************VARIABLES***************
>
>
> 'Check for Electronic
>
> Set notice5 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(5).Range.End)
>
> With notice5.Find
> .ClearFormatting
> .Text = "Electronic"
> .Forward = True
> .Wrap = wdFindStop
> End With
>
>
> 'Electronic If
>
> If notice5.Find.Execute("Electronic") = True Then
> electronic = True
> End If
>
> 'Check for Rep
>
> Set notice =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(1).Range.End)
>
> With notice.Find
> .ClearFormatting
> .Text = "cc:"
> .Replacement.Text = ""
> .Forward = True
> .Wrap = wdFindStop
> End With
>
> ' Rep IF
>
> If notice.Find.Execute("cc:") = True Then
> rep = True
> End If
>
> 'Check for Expert
>
> Set notice4 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(4).Range.start,
> End:=ActiveDocument.Sections(4).Range.End)
>
> With notice4.Find
> .ClearFormatting
> .Text = "in accordance with your contract"
> .Replacement.Text = ""
> .Forward = True
> .Wrap = wdFindStop
> End With
>
> 'Expert if
>
> If notice4.Find.Execute = True Then
> expert = True
> End If
>
> 'Check for concurrent
>
> Set notice =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(1).Range.End)
>
> With notice.Find
> .ClearFormatting
> .Text = "The hearing also concerns"
> .Replacement.Text = ""
> .Forward = True
> .Wrap = wdFindStop
> End With
>
> 'Concurrent If
>
> If notice.Find.Execute("The hearing also concerns") = True Then
> concurrent = True
> End If
>
>
>
>
> '************THIS STARTS THE PRINT PORTION OF THE MACRO********
>
> Set notice =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(2).Range.End)
> Set printnotice =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(2).Range.End - 1)
> Set notice3 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(3).Range.End)
> Set printnotice3 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(3).Range.End - 1)
> Set notice4 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(4).Range.End)
> Set printnotice4 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(4).Range.End - 1)
> Set notice5 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(5).Range.End)
> Set printnotice5 =
> ActiveDocument.Range(start:=ActiveDocument.Sections(1).Range.start,
> End:=ActiveDocument.Sections(5).Range.End - 1)
> Set acknotice =
> ActiveDocument.Range(start:=ActiveDocument.Sections(3).Range.start,
> End:=ActiveDocument.Sections(3).Range.End)
>
>
>
> '******ELECTRONIC CASES**********
>
> 'Electronic Cases with rep and expert (5 sections)
>
> If electronic = True Then
> If rep = True Then
> If expert = True Then
>
> 'print the notice
>
> printnotice5.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> 'print the forms
>
> Call PrintHRFormsRep
>
> 'delete the notice
>
> notice5.Delete
>
> End If
> End If
> End If
>
>
>
> 'Electronic with rep but no Expert (4 sections)
>
> If electronic = True Then
> If rep = True Then
> If expert = False Then
>
> 'Print the notice
>
> printnotice4.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> ' print the forms
>
> Call PrintHRFormsRep
>
> 'delete the notice
>
> notice4.Delete
>
> End If
> End If
> End If
>
>
> 'Electronic with no rep with an expert (5 sections)
>
> If electronic = True Then
> If rep = False Then
> If expert = True Then
>
> ' Just print the forms
>
> Call PrintHRFormsNoRep
>
> 'delete the notice
>
> notice5.Delete
>
> End If
> End If
> End If
>
> 'Electronic with no rep AND NO expert (4 sections)
>
> If electronic = True Then
> If rep = False Then
> If expert = False Then
>
> 'Just print the forms
>
> Call PrintHRFormsNoRep
>
> 'Delete the notice
>
> notice4.Delete
>
> End If
> End If
> End If
>
>
> '**********PAPER CASES***************
>
> 'Paper case with rep and expert (4 sections)
>
> If electronic = False Then
> If rep = True Then
> If expert = True Then
>
> 'Print 1 copy for rep
>
> printnotice4.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> 'Concurrent = print 2 copies for the file
>
> If concurrent = True Then
>
> 'delete the acknowledgement notice
>
> With acknotice
> .Select
> .Delete
> End With
>
> 'print the notice
>
> printnotice4.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=2,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If
>
>
>
> 'Not concurrent = print 1 copy for the file
>
> If concurrent = False Then
>
> 'delete the acknowledgement notice
>
> With acknotice
> .Select
> .Delete
> End With
>
> 'print the notice
>
> printnotice4.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If ' CONCURRENT If
>
> 'print the forms
>
> Call PrintHRFormsRep
>
> 'Delete the notice
>
> notice4.Delete
>
> End If
> End If
> End If
>
>
>
> 'Paper case with rep and no expert (3 sections)
>
> If electronic = False Then
> If rep = True Then
> If expert = False Then
>
> 'Print 1 copy for rep
>
> printnotice3.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> 'Concurrent = print 2 copies (first delete the acknowledgement notice)
> '
> If concurrent = True Then
>
> With acknotice
> .Select
> .Delete
> End With
>
> printnotice3.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=2,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If
>
> 'Not concurrent = print 1 copy (first delete the acknowldgement notice)
>
> If concurrent = False Then
>
> With acknotice
> .Select
> .Delete
> End With
>
> printnotice3.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If ' concurrent
>
> 'print the forms
>
> Call PrintHRFormsRep
>
> 'Delete the notice
>
> notice3.Delete
>
> End If
> End If
> End If
>
>
> 'Paper case with no rep and an expert (4 sections)
>
> If electronic = False Then
> If rep = False Then
> If expert = True Then
> If concurrent = True Then
>
> 'Print 2 copies for the file (first delete the acknowledgment notice)
>
> With acknotice
> .Select
> .Delete
> End With
>
> printnotice4.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=2,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If
>
> 'Print 1 copy for the file
>
> If concurrent = False Then
>
> With acknotice
> .Select
> .Delete
> End With
>
> printnotice4.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If
>
> 'print the forms
>
> Call PrintHRFormsNoRep
>
> 'Delete the notice
>
> notice4.Delete
>
> End If
> End If
> End If
>
>
> 'Paper case with no rep and no expert (3 sections)
>
> If electronic = False Then
> If rep = False Then
> If expert = False Then
> If concurrent = True Then
>
> 'Print 2 copies for the file (first delete the acknowledgement notice)
>
> With acknotice
> .Select
> .Delete
> End With
>
> printnotice3.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=2,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If
>
> 'Print 1 copy for the file
>
> If concurrent = False Then
>
> With acknotice
> .Select
> .Delete
> End With
>
> printnotice3.Select
> ActiveDocument.PrintOut Range:=wdPrintSelection, Copies:=1,
> PageType:=wdPrintAllPages, Collate:=True, Background:=False,
> PrintToFile:=False
>
> End If
>
> 'print the forms
>
> Call PrintHRFormsNoRep
>
> 'Delete the notice
>
> notice3.Delete
>
> End If
> End If
> End If
>
> Selection.HomeKey Unit:=wdStory
>
>
>
>
>
>
> 'Closes the notices document
> '
> ' With ActiveDocument
> ' .Saved = True
> ' .Close
> ' End With
>
> End Sub
>
>
> "Greg Maxey" wrote:
>
>> Without seeing your complete code it is impossible for me to even guess.
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>>
http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
>>
>> "Elaine J." <ElaineJ@discussions.microsoft.com> wrote in message
>> news:48CBFEEA-C87E-4914-B31A-58B5F439E493@microsoft.com...
>> > Sorry, I was going to do some editing and somehow clicked something to
>> > make
>> > this post before I was done.
>> >
>> > My question was going to be: what would make a macro work properly
>> > once,
>> > but not when it is looped?
>> >
>> > Thanks,
>> >
>> > "Elaine J." wrote:
>> >
>> >> I have a macro that I have been working on. It works exactly as I
>> >> want
>> >> to as
>> >> long as I run it once.
>> >>
>> >> Basically I have a document that contains many notices. Each notice
>> >> is 3
>> >> to
>> >> 5 sections long based on criteria that I can identify. The macro
>> >> correctly
>> >> identifies the number of sections for each notice, prints it and
>> >> deletes
>> >> it
>> >> so that the second notice is now the first.
>> >>
>> >> I can start the macro and print the first notice. Then start the
>> >> macro
>> >> again and print the second notice and so on until it reaches the end.
>> >> So
>> >> I
>> >> know the macro is working. The problem is, I want to be able to run
>> >> the
>> >> macro with no user interaction. So a simple loop should work. But no
>> >> matter
>> >> how I try to make it loop, it does not run properly. (It prints part
>> >> of
>> >> them
>> >> correctly, and part of them not correctly)
>> >>
>> >> I have tried:
>> >>
>> >> Start: (at the beginning of the macro)
>> >> goto Start: (at the end of the macro)
>> >>
>> >> Doesn't work
>> >>
>> >> I have tried
>> >>
>> >> do while selection.find.execute ("refer to:") = true
>> >> macro
>> >> Loop
>> >>
>> >> Doesn't work
>> >>
>> >> I have tried
>> >>
>> >> Do
>> >> Macro
>> >> Loop
>> >>
>> >> Doesn't work
>> >>
>> >> I have tried
>> >>
>> >> do until selection.find.execute ("refer to:") = false
>> >> macro
>> >> loop
>> >>
>> >> That doesn't work.
>> >>
>> >> I have stepped through the macro repeatedly. Everytime I step through
>> >> it,
>> >> it works perfectly. Then I can add a loop to it and it does not work
>> >> correctly.
>>
>>
>>