Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Oct 2002
    Location
    Edmonton, Alberta, Canada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    do while loop (English/Word/2003)

    Hello again.

    The systems administrator wants to change all the printer drivers to Postscript from PCL. This causes issues with the letterhead macro in Word as the new driver doesn't recognize the tray settings that the PCL driver had. Everything is okay for new letters being created, but I need to have a macro that will convert any old letters that were previously created using the old printer tray settings. Here is what I have so far.
    '********** UpdatePrinterSettings() **********
    Sub UpdatePrinterSettings()
    '
    'This macro is created because the printer drivers are being changed
    'from PCL to Postscript
    'This macro is designed to be used on existing letters to update the changes
    'required to the tray settings

    'Select entire document and change the tray to Tray 3 - Plain Paper
    ActiveDocument.Select
    With ActiveDocument.PageSetup
    .FirstPageTray = 261
    .OtherPagesTray = 261
    End With

    'Goto the top of the document and change the first section to appropriate trays
    Selection.HomeKey unit:=wdStory
    With Selection.PageSetup
    .FirstPageTray = 260
    .OtherPagesTray = 259
    End With

    'Search for the envelope style and change the paper trays for the envelope

    With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Envelope")
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    End With
    Selection.Find.Execute
    Do While Selection.Find.Found
    With Selection.PageSetup
    .FirstPageTray = 274
    .OtherPagesTray = 274
    End With
    Selection.GoTo what:=wdGoToSection, which:=wdGoToNext, Count:=1, Name:=""
    Selection.Find.Execute
    Loop

    'Search for the label style and change the paper trays for the label
    With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles("Label")
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    End With
    Selection.Find.Execute
    Do While Selection.Find.Found
    With Selection.PageSetup
    .FirstPageTray = 258
    .OtherPagesTray = 258
    End With
    Selection.GoTo what:=wdGoToSection, which:=wdGoToNext, Count:=1, Name:=""
    Selection.Find.Execute
    Loop

    End Sub

    I have the macro changing the whole document to Tray 3 (white paper) which will change all fax cover sheets, information sheets, etc. Then it goes to the top of the document (where the letter always is) and changes it to First Page Tray 2 (First page letterhead), and the rest of the pages to Tray 1 (Second page letterhead). That all works fine, except when I go to find any envelopes or labels, I search for the style that these are formatted in. It works okay until it gets to the end of the document and there are no more sections, then it creates an infinite loop and continues to find the text formatted in the label or envelope style over and over again. As I have no way of knowing how many labels, envelopes, fax cover sheets there are attached to the documents, is there any code that I can use to get it to recognize that it is at the end of the document and it can stop now?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: do while loop (English/Word/2003)

    To stop looping when your selection is in the last section, how about something like this before your goto-next-section:

    <code>If Selection.Information(wdActiveEndSectionNumber) = ActiveDocument.Sections.Count Then Exit Do</code>

  3. #3
    New Lounger
    Join Date
    Oct 2002
    Location
    Edmonton, Alberta, Canada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: do while loop (English/Word/2003)

    That has resolved all my issues. Thank you so much.

Posting Permissions

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