VBA Macro to delete specific form

D

DeputySmith770

Ok, so I have a word form that is password protected. When a user
checks a checkbox "Custody", on exit the script AddCustody runs. The
script is working (and I think I am adding comments stating what is
actually taking place.) I am self-taught VBA, so I may have some extra
stuff, but don't know how to clean it up.

--------------===============------------------------
Sub AddCustody()

' This Macro will append the Active Document with a Custody Report
page.

Set vCustody = ActiveDocument.FormFields("xCustody").CheckBox

'Check what type of protection - if any - has been applied
Select Case ActiveDocument.ProtectionType

'This tells the program the protection type is set to allow changes to
formfields only.
Case wdAllowOnlyFormFields

'If we've got this far, it's protected for forms
'Now unprotect the document
ActiveDocument.Unprotect Password:="pwd"
ActiveDocument.SpellingChecked = False

'This section determines whether a check is in the Custody box.
'If returned true, then a section break is added at the last point in
the active document.
'Then, the Custody Report is pulled from the server, and inserted at
the section break.
If vCustody.Value = True Then
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.InsertFile FileName:= _
"Path to file from domain server"
End If

'This ends the selected section of the protected document.
End Select

'Re-protect the document, and apply the same password protection.
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="pwd"
Application.ScreenUpdating = True
Application.ScreenRefresh

End Sub
---------------===================---------------

A similar script runs on exit from 6 other checkboxes. Problem is,
sometimes a person might add a page to the document, only to realize
later they don't need it. I have created a toolbar button that asks
the user for the page number they would like to delete, and on
CommandButton_Click the following script is executed from a UserForm.

----------==============----------------------------
Private Sub CommandButton1_Click()

'Set vPageNumber = ActiveDocument.FormFields("PageNumber").TextInput

'If we've got this far, it's protected for forms
'Now unprotect the document
ActiveDocument.Unprotect Password:="pwd"
ActiveDocument.SpellingChecked = False


Selection.GoTo What:=wdGoToPage, Count:=PageNumber
Selection.Bookmarks("\Page").Select
Selection.Delete
Selection.MoveUp Unit:=wdLine
Selection.EndKey Unit:=wdLine
Selection.Delete Unit:=wdCharacter


'Re-protect the document
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="pwd"
Application.ScreenUpdating = True
Application.ScreenRefresh

DeletePage.Hide
End Sub
----------------------================-----------------------

My problem is occurring when the user needs to delete more than one
page. Initially, when on the "\page" was being deleted, I was left
with the section break from the insert macro, and the header from the
deleted page. Thus, I added to the script, causing it to go up one
line, and to the end, then delete. Fine, until another page2 needs to
be deleted. Then, it starts deleting the bottom of page 1. Can
someone help clean up my delete page macro?

If you need the actual files to setup the situation, I can send them to
you, that way you won't have to rebuild my mess. Thanks for all your
help.
 

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