View Single Post
 
Old 09-01-2017, 08:14 AM
strodden strodden is offline Windows 10 Office 2010 32bit
Novice
 
Join Date: Aug 2017
Posts: 22
strodden is on a distinguished road
Default Deleting Pages with macro on protected sheet

My boss wants it this way, though, so that's what I'm exploring!

I have a protected word doc with form fields. Some of the pages will only be necessary under certain conditions, and I would like to make a button which removes unneeded pages depending on the need.

I am BRAND new to VBA and have found this code online:

Sub Clearpages()
Dim rgePages As Range
Dim PageCount As Integer
PageCount = ActiveDocument.ComputeStatistics(wdStatisticPages)
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=PageCount
rgePages.End = Selection.Bookmarks("\Page").Range.End
rgePages.Delete
End Sub

This works until I protect the doc, then stops working. I read on another page that I need to insert the code "Sheetname.Unprotect Password:="password"/"Sheetname.Protect Password:="password"" at the beginning and end of this code, but I can't make that work either.

What am I doing WRONG????


Best,
Seth
Reply With Quote