View Single Post
 
Old 03-03-2024, 03:28 AM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I think there are two problems with your code
1. the PgNum should be an integer, not a string
2. You need to be explicit with the variables you are passing to the GoTo command as Word is not thinking you are asking for a page number. All the variables are optional and Word is expecting them in the following order.
expression.GoTo( What , Which , Count , Name )
From your code, you were passing the page number in as a string which Word thought was for the optional "Which" parameter and it appears it was intended to be the "Count" parameter. If you haven't explicitly named the parameter for each variable Word tries to align the variables in the same order as the intellisense (and help file) displays the parameters
Try it again with these variations...
Code:
Sub DeleteFromPgToEnd()
'Delete pages from PgNum till the doc's end.

  Dim rng As Range
  Dim PgNum As Integer
  PgNum = CInt(InputBox("Enter the number of the page"))
  Set rng = ActiveDocument.Range.GoTo(What:=wdGoToPage, Count:=PgNum)
  rng.End = ActiveDocument.Range.End
  'rng.Select   'enable for testing
  'If there's a page break before the PgNum page, the pages won't get
  'deleted, so move the rng's start:
  If rng.Characters.First.Previous = Chr(12) Then
    rng.Start = rng.Start - 1
  End If
  'rng.Select      'enable for testing
  rng.Delete
End Sub
The shorter way to specify your key line and rely ONLY on the default order of parameters is to include extra commas to skip an optional parameter
Set rng = ActiveDocument.Range.GoTo(wdGoToPage, , PgNum)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia

Last edited by Guessed; 03-03-2024 at 03:29 PM.
Reply With Quote