assigning to DOCPROPERTY

A

Associates

Hi,

I'm having difficulty in assigning value from a textbox to DOCPROPERTY. In
the document, i have { DOCPROPERTY companyname } set in the header and {
DOCPROPERTY issuedate } in the footer. There are a few pages whose header and
footer linked together (same as previous).

Here is the code i have the problem with
With ActiveDocument
.BuiltInDocumentProperties("bi_compheader") = txtCompanyName.Text
.BuiltInDocumentProperties("bi_issuedate") = txtIssueDate.Text
End With
call updateFooter
call updateHeader

Sub UpdateFooter()
Dim i As Integer

'Get page count
i = ActiveDocument.BuiltInDocumentProperties(14)

If i >= 1 Then 'Update fields in Footer
ActiveDocument.Sections(ActiveDocument.Sections.Count) _
.Footers(1).Range.Fields.Update
End If
End Sub

Sub UpdateHeader()
Dim i As Integer

'Get page count
i = ActiveDocument.BuiltInDocumentProperties(14)

If i >= 1 Then 'Update fields in Header
ActiveDocument.Sections(ActiveDocument.Sections.Count) _
.Headers(1).Range.Fields.Update
End If
End Sub

Your helps are greatly appreciated

Thank you in advance
 
D

Doug Robbins - Word MVP

IMHO it is easier to use document variables and { DOCVARIABLE } fields

With ActiveDocument
.Variables("bi_compheader").Value = txtCompanyName.Text
.Variables("bi_issuedate").Value = txtIssueDate.Text
.PrintPreview 'Update the fields
.ClosePrintPreview
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Gordon Bentley-Mix

Assuming the document properties exist at all, they're definitely custom
properties. I queried the built-in doc props collection and didn't find
anything even close to what's in your code. You should probably do something
similar to see if they exist in the custom properties collect. (Quick n dirty
code to list the built-in and custom properties provided below.)

Sub listdocprops()
Dim mybiprop As DocumentProperty
Dim mycustprop As DocumentProperty
For Each mybiprop In ActiveDocument.BuiltInDocumentProperties
Debug.Print mybiprop.Name
Next mybiprop
For Each mycustprop In ActiveDocument.CustomDocumentProperties
Debug.Print mycustprop.Name
Next mycustprop
End Sub

As for the "Invalid procedure call" message, if a property doesn't exist in
the document then you will get the error you described.

BTW, in re the following:
[snip] I dont think it's a builtin property because i didn't add
any properties. All i did was just add to the document header { DOCPROPERTY
companyname }. So i think it might be a custom property. [snip]

Just inserting a random DOCPROPERTY field with the name of some property
you'd like to use isn't enough; you actually need to _add_ the custom
property to the document before you try to access it. You can do this
manually, but it's probably more reliable and safer to query for the
existence of the property in code and add it programmatically if it doesn't
exist. (And for the record, you can't add properties to the built-in
properties collection - only the custom properties collection - so the fact
that you didn't add any properties isn't a definitive test for determining if
the property is built-in.)

However, I agree with Doug's comment re: document variables. They're
definitely preferrable to properties in many ways - not the least of which is
that they're much harder for Joe Bloggs user to modify or delete. If you're
interested, I can provide you with some sample code for quickly checking for
the existence of a doc var and adding it if it's not found.
--
Cheers!
Gordon
The Kiwi Koder

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

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