Protect Word Doc

P

PumaMan

I have a word doc that is pulled up from a template in VBA. I have tried to
protect the doc after it opens & sends the data from excel to the appropriate
bookmarks. I just need to protect the doc for filling in forms only at that
point, but can't get it to work. I've followed the instructions of earlier
posts i've found here and checked the Word MVP site, nothing works.
Something must be wrong with my code...

The code to protect is towards the bottom. Thank you in advance!

Sub WriteToWordDoc()

Dim WordObject As Object 'Word Object
Dim SigCardTemplateString As String 'Template name
Dim SigCardPathString As String 'Path & Template

If AccountState = "FLORIDA" Then
SigCardTemplateString = "FL W9 Sig.dot"
SigCardPathString = "C:\Documents and Settings\nbk337h\Desktop\" & _
"Excel Process Docs\" & SigCardTemplateString
End If

Set WordObject = CreateObject("Word.Application")

With WordObject
'Create a new Word document based on the template
.Documents.Add Template:=SigCardPathString
.Visible = True 'Can be False
.ActiveDocument.Bookmarks("AccountNumberBookmark").Range =
AccountNumber
.ActiveDocument.Bookmarks("AccountTypeBookmark").Range = AccountType
.ActiveDocument.Bookmarks("CaseNumber").Range = CaseNumber
.ActiveDocument.Bookmarks("TitleLine1Bookmark").Range = TitleLine1
.ActiveDocument.Bookmarks("TitleLine2Bookmark").Range = TitleLine2
.ActiveDocument.Bookmarks("TitleLine3Bookmark").Range = TitleLine3
.ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password:="123"
End With

'Cleanup
Set WordObject = Nothing
End Sub
 
J

Jay Freedman

Because you're using late binding
(http://www.word.mvps.org/FAQs/InterDev/EarlyvsLateBinding.htm), the
constant wdAllowOnlyFormFields isn't defined. If you used the Option
Explicit directive at the top of your module
(http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm), your code
would get a compile error. Because you don't have that directive, VBA
assumes that wdAllowOnlyFormFields is a variable and automatically
initializes it to zero.

The simplest fix is to add the value as a constant in your declarations:

Const wdAllowOnlyFormFields = 2

The alternative is to change to using early binding, which makes all the
constants and other parts of the object model available.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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