VBA Help - Remove Page Macro

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

VBA Help - Remove Page Macro

Post by arroway »

We have a locked form with a macro button that inserts a fax cover sheet so we can electronically fax the form. However, we don't want the fax cover sheet saved with the document so our process is to use the macro to insert the cover sheet, then we fax it, then we close the document without saving changes. However, sometimes folks forget to close the file without saving the changes and they save it with the cover sheet attached. Then I have to go through them and remove the coversheets (because the form is password protected and I'm the 'holder of the keys'). Since I'm doing this over and over again I'm looking for a macro which will unlock the document, remove the first page, and then relock the document. With my limited knoledge of VBA, I've got:

Code: Select all

Sub FaxCoverRemover()
   With ActiveDocument
        ' unprotect
        If .ProtectionType <> wdNoProtection Then
           .Unprotect Password:="password"

        ' remove the fax cover/first page
           

         ' reprotect
       .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="password"
   End With
End Sub
I just don't know how to write the codey piece in the middle that instructs, "select the first page and delete." Can someone help?

Thank you,
Dax
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: VBA Help - Remove Page Macro

Post by Rudi »

Hi,

Give this a try...
My wWord VBA knowledge is not that good but the tests I ran with the code below seem to work ...

Code: Select all

Sub FaxCoverRemover()
    With ActiveDocument
        ' unprotect
        If .ProtectionType <> wdNoProtection Then
            .Unprotect Password:="password"
            ' remove the fax cover/first page
            Selection.HomeKey wdStory
            Selection.Bookmarks("\page").Range.Delete
            ' reprotect
            .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="password"
        End If
    End With
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: VBA Help - Remove Page Macro

Post by arroway »

Hmm. I get a Compile error stating the error commonly occurs when code s incompatible with the version, platform, or architecture of the application. Not sure what that means but in case it's important, I'm running Word 2010 on a WIN7 PC.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Help - Remove Page Macro

Post by HansV »

The code posted by Rudi should work in all versions of Word since Word97. Which line is highlighted when you get the error message and click Debug?
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: VBA Help - Remove Page Macro

Post by arroway »

End With is highlighted and the error says there's an End With without a With. ...but isn't there a With?
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: VBA Help - Remove Page Macro

Post by Rudi »

I cannot seem to mimic the error? :sorry:

Edit:
I get the error if I remove the "End If" statement. As Hans says, ensure you copy/paste the code in it's entirety.

Edit 2:
I notice the original code you posted was already missing the End If statement. If you simply copied the two selection lines into your existing macro, this error will be produced. Ensure you have an End If statement above the End With.

Off topic: Two to tengo?
Last edited by Rudi on 23 Dec 2015, 21:02, edited 2 times in total.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Help - Remove Page Macro

Post by HansV »

Are you sure that you copied Rudi's macro correctly? It is syntactically correct, and it should do exactly what you asked.
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: VBA Help - Remove Page Macro

Post by arroway »

Yep, there it is! :stupidme: "Copy all of the code!" Go it. Thank you very much and Merry Christmas!
--Dax
:bananas: :bananas: :bananas: :bananas:
It takes 2 to tango; unless you speak binary; then it takes 10.