PDA

View Full Version : Unprotecting and Reprotecting Word Forms on Merge



twotinners
06-15-2006, 01:09 PM
Hello Gurus:

I have the following code (found on this site) and I want to be able to merge protected and unprotected word forms into a single document. I get an error when I run it saying "Already unprotected" and the macro stops.

:think: Ideas. thoughts, suggestions ?

Sub test()
Dim doc As Document
Dim r As Range
Dim num_files As Integer
Dim file_name As String
Dim dir_path As String
Dim file_ext As String
Dim lngPage As Long

Application.ScreenUpdating = False
Set doc = Application.Documents.Add

dir_path = "C:\__virtual\_forms\"
file_ext = "doc"

file_name = Dir(dir_path & "*." & file_ext)
Do While Len(file_name) > 0
Documents.Open FileName:=dir_path & file_name
lngPage = ActiveDocument.PageSetup.Orientation
ActiveDocument.Unprotect Password:="sparky"
Set r = Selection.Range
r.WholeStory
r.Copy
ActiveDocument.Close wdDoNotSaveChanges
With Selection
With .Sections(1).PageSetup
.Orientation = lngPage
.SectionStart = wdSectionNewPage
End With
.PasteAndFormat wdFormatOriginalFormatting
file_name = Dir()
If Len(file_name) > 0 Then
.InsertBreak Type:=wdSectionBreakNextPage
End If
.EndKey unit:=wdStory
End With
Loop
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="sparky" '
End Sub

Am I puting the protect / unprotect lines in the wrong area ?

Remeber: ME=Newbie=Be Gentle :*)

mdmackillop
06-15-2006, 03:24 PM
You can check for protection first. From the Help file

Set Doc = ActiveDocument
If Doc.ProtectionType <> wdNoProtection Then Doc.Unprotect

twotinners
06-15-2006, 04:10 PM
I didn't understand that but let's try it from a different perspective.

If all the forms (each with formfields) were in a specific directory and I complete the merge, can I protect the newly merged form so the user can enter data into the formfields ?

They don't have to be password protected (that doesn't do much anyway).

:reading: I'm still reading up on this....

Dave -> the VB Newbie

fumei
06-15-2006, 04:57 PM
When posting that you are getting an error, it is handy to also post precisely where yo are getting the error. Documents.Open FileName:=dir_path & file_name
lngPage = ActiveDocument.PageSetup.Orientation
ActiveDocument.Unprotect Password:="sparky" I am assuming that the code stops at the:

ActiveDocument.Unprotect Password:="sparky"

line. Yes? That means the file that was just opened is not, in fact, protected. Therefore - "Already unprotected".

What Malcolm is suggesting is that it is a good idea - and it of course is - to check first. So Documents.Open FileName:=dir_path & file_name
If ActiveDocument.ProtectionType <> wdNoProtection Then _
ActiveDocument.Unprotect Password:="sparky" Malcolm though is incorrect in usingSet Doc = ActiveDocument
If Doc.ProtectionType <> wdNoProtection Then Doc.Unprotect as the Document variable "doc" has already been set to the activedocument with the previous instruction Set doc = Application.Documents.Add The document being opened (with assumably the protection) is a different document. You could, of course, use another document variable. And it could be that there is a reason for these two document, but as it stands - there are TWO documents.
Set doc = Application.Documents.Add andDocuments.Open FileName:=dir_path & file_name The latter is now the Active Document.

twotinners
06-15-2006, 08:29 PM
Gerry,

Thanks for the reply and advice. The forms unlocked themselves upon merge (with the addition of your code) but then how do I lock them again so the user can enter data on the forms.

I locked the entire new merged form manually but then the forms that had macros wouldn't work (combos with a .txt data file reference and this is the only section of the newly merged form with combos).

GRRR... you were right when you said (in another post) that I was in for a ride learning VB.

So I guess the short questions are: How do I relock the newly created document now and how do I make sure the macros come with any merged form ?

:banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: :banghead: (I need an asprin)

Dave

twotinners
06-16-2006, 01:03 PM
The new document merges beautifully as long as I don't have any documents with headers or footers but the macros don't come with the merge. As I'm on the same subject of losing macros that ARE in the document(s), I lose macro abilities when a form is placed on my company LAN drive and accessed through a Intranet hyperlink. Any thoughts on that.... it is (I think) somewhat related.

VB Newbie Dave
(I've taken a lot of asprin since yesterday)

++++++++++++++++++++++++++++++++++
UPDATE: June 16, 2006 7:50 pm

After much research on the lost macro ability when posting a file to an intranet, I now know it can't be done and Microsoft has even admitted the fact. Hmmmmm, I'll have to rethink the distribution of this little project of mine...... hmmmm

============================================
Update: June 16, 2006 8:30 pm

There is a workaround for posting templates and document w/macros to a LAN/Intranet set up. This MS kb tells it all: http://support.microsoft.com/default.aspx/kb/q278627

I've tried it and it works !!!!!



:)

fumei
06-16-2006, 08:06 PM
The new document merges beautifully as long as I don't have any documents with headers or footers but the macros don't come with the merge.What is the problem with headers and footers?

twotinners
06-17-2006, 07:43 AM
The document headers and footers apply themselves to the documents that follow in the merge. That's really no big deal. I'll just delete them as they're not needed for my little project anyway.

So how do I reprotect the document and get the macros to come with the merge. The macros ARE in the original document.

:doh: Newbie Dave

fumei
06-17-2006, 03:44 PM
You had the syntax for protecting a document correct. ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:="sparky"And that is how you protect a document. As far as Word is concerned, it has NO memory of a document being protected. If it is unprotected - it is unprotected. There is no REprotect. You protect a document...or do you don't.

Now as for the macros. I am trying to follow the eocument creation process. If the original doc has macros, and you are merging the merged doc should have the macros. Unless you are doing something weird. Are you doing something weird?

twotinners
06-17-2006, 07:38 PM
I am trying to follow the eocument creation process. If the original doc has macros, and you are merging the merged doc should have the macros. Unless you are doing something weird. Are you doing something weird?

The code is in the first post on this thread. When I have standard (simple)documents, everything merges great. But if the document has a macro and is used in a multi document merge (merging 15 complete and separete documents), the macros don't work and don't come with the document(s). "Am I doing something weird"? Probably !!! :dunno

Dave "THE VB Newbie"

PS: Thanks for the "reprotect" info.