Unprotect AND (RE)protect fields

P

papou

Hi all
I am using this OLE code within Excel:
With wrdDoc
.Unprotect
.FormFields("Texte1").Result = Me.ComboBox1.Value
.FormFields("Texte2").Result = Me.TextBox1.Value
.Protect wdAllowOnlyFormFields, True
.Close (True)
End With
This works as regards unprotecting and feeding the fields but fails with the
line:
..Protect wdAllowOnlyFormFields, True
As a matter of fact this line does not raise an error but it does not lock
my Word document as it was initially from the "lock" button available from
the Word Forms Tool bar.
Does anyone know which syntax I should use to achieve this?

TIA
Cordially
Pascal
 
D

Dave Lett

Hi Pascal,

I'm just guessing, but you might not have a reference to the Word Object
library set in your project. If so, then wdAllowOnlyFormFields will mean
nothing to Excel. In the VBE, on the Tools menu, click References, and see
if Microsoft Word nn.n Object Library is selected (where nn.n is the version
number of Word that you're running). If it isn't then you have one of two
options: 1) make the reference available in this Excel project (that's okay
if you're not sharing this with someone else) or 2) change
wdAllowOnlyFormFields to 2, as in the following:

..Protect 2, True '''much less readable, though

HTH,
Dave
 
P

papou

Hello Dave
Thank you for your answer.
I am not using a reference to Word but use the Object method (early/late
binding).
I thought your suggestion related to the 1st argument of the protect method
could be the issue, but unfortunatlely it does not work either.
I do think I am just missing the exact method which will lock my document as
per the lock button from the Forms toolbar.

Cordially
Pascal
 
P

papou

The code works with the Word library added in the VB References.
So I (may be wrongly) come to the conclusion that the syntax I use can not
work using the late binding method?

Cordially
Pascal
 
D

Dave Lett

Hi Pascal,

From the article "Early vs. Late Binding" at
http://word.mvps.org/faqs/interdev/EarlyvsLateBinding.htm, I gather that you
LOSE access to Word's constants when you use late binding. So, if you're
using late binding, then it might work if you changed
"wdAllowOnlyFormFields" to "2". The method to protect the document, by the
way, is correct, and I have used it many, many times just recently.

HTH,
Dave
 

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