Printing Forms from Macros

J

JayM

I have created some protected forms for users but I also have a customized
printing menu. This enables the users to print to different paper trays (we
have several) easily without having to go CTRl P change the paper type and
remember which tray is which each time.

My printing menu lists the paper trays by type.

How can I get the Printing macros to print the protected forms. If I try at
the moment I get Run-time Error 4605.

Any help would be greatly appreciated.

JayM
 
D

DebsP via OfficeKB.com

Hi JayM

You can get your code to unprotect the form at print time & then re-protect
it again if necessary.

This is the code to re-protect. Note that the NoReset option is set to true
so that the data in the form fields is retained.

If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True
End If

DebsP
 
J

JayM

Thanks DebsP

One more question then could I add this code and still use the macro for
printing normal docs or wills this throw an error.

would test this myself but am desperately trying to get th answers whilst
trying to look after sick kids. Any help would be appreciated.

Many thanks

JayM
 
D

debbieprobert via OfficeKB.com

Hi JayM

Don't worry about asking another question :)

You can check the protection status of all documents. If it is protected
then unprotect it, print & re-protect. If your forms are password protected,
then I recommend you use a standard password as you can code that. However,
I suspect they're just protected for forms.

If you prefer not to check all documents, are all your forms templates in the
same folder? If so, then you can check the template path e.g. if the
attached template is in c:\MyTemplates\Forms\ then you know straight away it
is a form & will need to be dealt with accordingly. If not, you could build
a list of forms, in say an Access table, and get the code to check the list
to know whether the item is to be unprotected.

Does that help? Please ask again if not.
Thanks DebsP

One more question then could I add this code and still use the macro for
printing normal docs or wills this throw an error.

would test this myself but am desperately trying to get th answers whilst
trying to look after sick kids. Any help would be appreciated.

Many thanks

JayM
[quoted text clipped - 24 lines]
 
B

Brent Morris

I have created some macros that will unprotect our forms then will run
either spell check, envelopes, or dymo label,, and then reprotect them,
since you can't do either of those in a protected form which I think is dumb
that you can't. I would be curious to see your custom printing menu because
now I'm trying to create a macro that will unprotect our forms, then use the
users default printer and change the pagesetup to print the first page to
tray 2 for letterhead and the rest to tray 1 for second page preprinted
paper.
I started with using the macro for spell checking a form that I found at
this site.
http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm
I then used this macro for envelopes and the dymo label.
Let me know if you want any of the code for these and I'm not sure if I can
help you with your problem except for looking at my code. I'm to new at
these macros.

Thanks
Brent Morris
 
J

JayM

DebsP

Thanks for your help with this.

I was able to unprotect my document but then I couldn't get the document to
reprotect.

I attach the code I was using.

I then thought that this would just protect any document if it worked and I
would only want to protect documents that were previously protected. Any
help you can give with regard to this would be appreciated. I am still on a
big learning curve and could do with finding a course (online, book etc) to
help with vba. If you have any ideas that would be great.

Thanks again
 
J

JayM

Forgot the code, so here it is

Public Sub UNPROTECTDOCUMENT()
'Unprotect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
End Sub


Public Sub REPROTECTDOCUMENT()
'Protect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub

Sub PRINT_THIN_P()
' PRINT_THIN_P Macro
UNPROTECTDOCUMENT
With ActiveDocument.PageSetup
.FirstPageTray = wdPrinterPaperCassette
.OtherPagesTray = wdPrinterPaperCassette
End With
Application.PrintOut filename:="", Range:=wdPrintAllDocument,
Item:=wdPrintDocumentContent, Copies:=1, Pages:="",
PageType:=wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False
REPROTECTDOCUMENT
End Sub


debbieprobert via OfficeKB.com said:
Hi JayM

Don't worry about asking another question :)

You can check the protection status of all documents. If it is protected
then unprotect it, print & re-protect. If your forms are password protected,
then I recommend you use a standard password as you can code that. However,
I suspect they're just protected for forms.

If you prefer not to check all documents, are all your forms templates in the
same folder? If so, then you can check the template path e.g. if the
attached template is in c:\MyTemplates\Forms\ then you know straight away it
is a form & will need to be dealt with accordingly. If not, you could build
a list of forms, in say an Access table, and get the code to check the list
to know whether the item is to be unprotected.

Does that help? Please ask again if not.
Thanks DebsP

One more question then could I add this code and still use the macro for
printing normal docs or wills this throw an error.

would test this myself but am desperately trying to get th answers whilst
trying to look after sick kids. Any help would be appreciated.

Many thanks

JayM
[quoted text clipped - 24 lines]
 
D

debbieprobert via OfficeKB.com

Hi JayM

In your ReprotectDocument routine, you're saying that if the document
protection type is no protection at all then protect it - 'ActiveDocument.
ProtectionType <> wdNoProtection'. The document will not be

Your ReprotectDocument routine contains the line:

'If ActiveDocument.ProtectionType <> wdNoProtection' then

This is saying

'If the document protection type is *not* no protection then' (forgive the
English!)

The document protection type at this stage *is* no protection as you removed
it in your UnprotectDocument routine.

What you want to be saying is:

'If ActiveDocument.ProtectionType = wdNoProtection then'

However, you would then need to check the document to see if contained form
fields and only reprotect those documents that do, otherwise you would end up
protecting all documents.

I believe your requirement is to deal with forms rather than all password
protected documents. If so, this is the code I use to unprotect the form
before any page setup commands are used. All our forms have the same
password so I can pass this in the code (as strFormsPassword). You do not
seem to have a password in which case I believe you can just use
ActiveDocument.Unprotect without the password element.

If the form was protected I set a Boolean variable to 'true' so that I know
whether that form has to be re-protected later in the code. This variable is
declared at Module level rather than within the Procedure. This means that
all the Procedures within the Module can use it. A jargon free explanation
(I hope) is that you simply put the Dim statement at the top of the page
outside of the Sub area.

Dim booWasProtected as Boolean 'Module level declaration of Boolean variable

Sub UnProtect()

'if the document is protected for form fields
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect strFormPassword
'this must be true otherwise the code would not have got this far
booWasProtectedForm = True
End If

End Sub

Sub Protect()

If booWasProtectedForm Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,

Password:=strFormPassword
End If

End Sub

I think there are a number of free online courses which you can find by
hunting the web. Lots of places run hands-on courses depending upon where
you are in the world. As to books, most can be pretty confusing for the non-
programmer & it depends on how much you want to achieve. If you are largely
customising the Word system & want to be able to do some fairly basic/medium
level development to start with, you can do worse than something like "Word
2000 VBA Programmer's Reference". I expect there is a 2003 version now. I'm
no programmer & I found this comprehensible. And don't be afraid of
recording macros wherever you can - eventually you'll find shorter ways to do
things but recorded macros can give you a head start.

Hope that helps - & that the children are better :)

DebsP



Forgot the code, so here it is

Public Sub UNPROTECTDOCUMENT()
'Unprotect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
End Sub

Public Sub REPROTECTDOCUMENT()
'Protect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub

Sub PRINT_THIN_P()
' PRINT_THIN_P Macro
UNPROTECTDOCUMENT
With ActiveDocument.PageSetup
.FirstPageTray = wdPrinterPaperCassette
.OtherPagesTray = wdPrinterPaperCassette
End With
Application.PrintOut filename:="", Range:=wdPrintAllDocument,
Item:=wdPrintDocumentContent, Copies:=1, Pages:="",
PageType:=wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False
REPROTECTDOCUMENT
End Sub
[quoted text clipped - 31 lines]
 
D

debbieprobert via OfficeKB.com

Hi JayM

Please can you ignore the first paragraph of my reply to this. Should have
previewed it before I sent it. Sorry for any confusion.

DebsP
Forgot the code, so here it is

Public Sub UNPROTECTDOCUMENT()
'Unprotect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
End Sub

Public Sub REPROTECTDOCUMENT()
'Protect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub

Sub PRINT_THIN_P()
' PRINT_THIN_P Macro
UNPROTECTDOCUMENT
With ActiveDocument.PageSetup
.FirstPageTray = wdPrinterPaperCassette
.OtherPagesTray = wdPrinterPaperCassette
End With
Application.PrintOut filename:="", Range:=wdPrintAllDocument,
Item:=wdPrintDocumentContent, Copies:=1, Pages:="",
PageType:=wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False
REPROTECTDOCUMENT
End Sub
[quoted text clipped - 31 lines]
 
J

JayM

DebsP

What a star you are!!

Many thanks for your help and guidance with this. I will look at for the
book you referred to. I have seen VB and VBA in a Nutshell and wondered
whether this was worth a delve into.

JayM
debbieprobert via OfficeKB.com said:
Hi JayM

In your ReprotectDocument routine, you're saying that if the document
protection type is no protection at all then protect it - 'ActiveDocument.
ProtectionType <> wdNoProtection'. The document will not be

Your ReprotectDocument routine contains the line:

'If ActiveDocument.ProtectionType <> wdNoProtection' then

This is saying

'If the document protection type is *not* no protection then' (forgive the
English!)

The document protection type at this stage *is* no protection as you removed
it in your UnprotectDocument routine.

What you want to be saying is:

'If ActiveDocument.ProtectionType = wdNoProtection then'

However, you would then need to check the document to see if contained form
fields and only reprotect those documents that do, otherwise you would end up
protecting all documents.

I believe your requirement is to deal with forms rather than all password
protected documents. If so, this is the code I use to unprotect the form
before any page setup commands are used. All our forms have the same
password so I can pass this in the code (as strFormsPassword). You do not
seem to have a password in which case I believe you can just use
ActiveDocument.Unprotect without the password element.

If the form was protected I set a Boolean variable to 'true' so that I know
whether that form has to be re-protected later in the code. This variable is
declared at Module level rather than within the Procedure. This means that
all the Procedures within the Module can use it. A jargon free explanation
(I hope) is that you simply put the Dim statement at the top of the page
outside of the Sub area.

Dim booWasProtected as Boolean 'Module level declaration of Boolean variable

Sub UnProtect()

'if the document is protected for form fields
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect strFormPassword
'this must be true otherwise the code would not have got this far
booWasProtectedForm = True
End If

End Sub

Sub Protect()

If booWasProtectedForm Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,

Password:=strFormPassword
End If

End Sub

I think there are a number of free online courses which you can find by
hunting the web. Lots of places run hands-on courses depending upon where
you are in the world. As to books, most can be pretty confusing for the non-
programmer & it depends on how much you want to achieve. If you are largely
customising the Word system & want to be able to do some fairly basic/medium
level development to start with, you can do worse than something like "Word
2000 VBA Programmer's Reference". I expect there is a 2003 version now. I'm
no programmer & I found this comprehensible. And don't be afraid of
recording macros wherever you can - eventually you'll find shorter ways to do
things but recorded macros can give you a head start.

Hope that helps - & that the children are better :)

DebsP



Forgot the code, so here it is

Public Sub UNPROTECTDOCUMENT()
'Unprotect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
End Sub

Public Sub REPROTECTDOCUMENT()
'Protect the Document
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub

Sub PRINT_THIN_P()
' PRINT_THIN_P Macro
UNPROTECTDOCUMENT
With ActiveDocument.PageSetup
.FirstPageTray = wdPrinterPaperCassette
.OtherPagesTray = wdPrinterPaperCassette
End With
Application.PrintOut filename:="", Range:=wdPrintAllDocument,
Item:=wdPrintDocumentContent, Copies:=1, Pages:="",
PageType:=wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False
REPROTECTDOCUMENT
End Sub
[quoted text clipped - 31 lines]
 

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