Print pages from Autotext then delete

M

Mark

I am to print some guidance for a document completion. I have the guidance
notes stored in autotext which are inserted briefly at the end of the
document so that they can be printed. Once printed they are deleted from the
document.

I have the following code:

Sub PrintGuidance()
Dim i As String
Dim s As String
Dim t As String
Dim v As String
Dim PagesToPrint As String

Application.ScreenUpdating = False

ActiveDocument.Unprotect Password:="kruger"

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Application.DisplayAutoCompleteTips = True
ActiveDocument.AttachedTemplate.AutoTextEntries("Notes_for_guidance"). _
Insert Where:=Selection.Range, RichText:=True

i = ActiveDocument.BuiltInDocumentProperties("Number of Pages")
'i = ActiveDocument.Content.ComputeStatistics(wdStatisticPages)


s = CStr(i - 3)
t = CStr(i - 2)
v = CStr(i - 1)
i = CStr(i)

PagesToPrint = s & "-" & i


Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:=PagesToPrint, PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True,
Background:= _
False, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0

Selection.MoveUp Unit:=wdLine, Count:=166, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=2

Call ProtectForm

Application.ScreenUpdating = True

End Sub

When the routine is stepped into it works fine, but when it is run via a
button it either produces the wrong pages or states that the printrange is
invalid.

Can anyone assist with a solution, please?
 
P

Peter Faulhaber

Hi Mark,

Have you tried to not to use ScreenUpdating?

Further more, try to insert the guidance as a section. Deleting the guidance
is more easily by using ActiveDocument.Sections.Last.Range.Delete
 
M

Mark

Evenif I take the screenupdatring off it doesn't make a difference.

Like the delete last section routine, I'll incoporate that.
 
J

Jay Freedman

Hi Mark,

After you insert the AutoText and before you get the number of pages, call
ActiveDocument.Repaginate to force Word to compute the correct value for i.
Otherwise its value is unreliable.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top