Revisiting: How do you disable word counting?

E

eowen

Picking up this thread from a few years ago. (See below.

I'm in the exact same predicament as David, below. I've tried Jonathan West's suggested workaround of defining an object variable for the BuiltInDocumentProperties collection, but I think I'm finding that you cannot assign to BuiltInDocumentProperties, only read from it

That is, I do the following

dim objBuiltInProps as DocumentPropertie

set objBuiltInProps = ActiveDocument.BuiltInDocumentPropertie

' do some stuff to modify the values of the properties in objBuiltInProp
...

set ActiveDocument.BuiltInDocumentProperties = objBuiltInProp

I get a run-time error on the last line, telling me that the object doesn't support this property or method

Am I doing this wrong, or is there some other way to go about it

For the time being, I'm assigning each property in the objBuiltInProps collection to the corresponding property in ActiveDocument.BuiltInDocumentProperites collection, which causes the whole word count problem to appear again -- Word recounts the words in the document once for each property involved

thank
eowe

-----------------------------------------

Hi David

There are a number of workarounds

1. Define an object variable for the CustomDocumentProperties collection
make your changes to that, and then assign the variable back to th
CustomDocumentProperties collection. This will reduce the number of time
the word count runs to just twice. You can do similar things with th
BuiltInDocumentProperties collectio

2. Drop back to use of the WordBasic object, and use the SetDocumentPropert
method. As MS has not seen fit to properly document the WordBasic object i
VBA Help, the best source of information is the WordBasic Help file fro
Word 95, which I keep handy for just such an occasion

Try thi

WordBasic.SetDocumentProperty "My Property", 0, 2, "My property value

WordBasic command always apply to the ActiveDocument, so beware of this. Th
full entry of SetDocumentProperty is as follow

SetDocumentProperty Name$, Type, Value[$], CustomOrBuiltI

Defines a document property for the active document. If Name$ is a built-i
property that doesn't match the type specified by Type, or if it is
read-only property, an error occurs. Use the DocumentPropertyExists(
function to determine whether a property called Name$ already exists
For a list of the built-in properties available in Word, se
DocumentPropertyName$(). In Word version 6.0, SetDocumentProperty i
unavailable and generates an error

Argument Explanatio
Name$ The name of the property. Name$ can be a built-in property or a custo
property
Type The type of the property to be defined:0 (zero) String1 Number
Date3 Yes or NoIf Name$ is a built-in property and doesn't match the typ
specified by Type, an error occurs
Value[$] The property value. Value[$] must match the type specified b
Type.If Value[$] doesn't match the type specified by Type, an error occurs
CustomOrBuiltIn Specifies whether Name$ is a custom property or a built-i
property:0 (zero) or omitted Name$ is a custom property unless it appear
in the list of built-in properties. If Name$ is a built-in property
SetDocumentProperty will update its value.1 Name$ is a built-in property. I
it is not one of the built-in properties, an error occurs. 2 Name$ is
custom property, regardless of whether a built-in property with the sam
name already exists

3. If you don't mind closing the document first, then you can read and writ
document properties using an ActiveX DLL freely downloadable from th
Microsoft web site. Take a look here for detail
http://www.mvps.org/word/FAQs/MacrosVBA/DSOFile.ht

-
Regard
Jonathan West - Word MV
MultiLinker - Automated generation of hyperlinks in Wor
Conversion to PDF & HTM
http://www.multilinker.co
Word FAQs at http://www.multilinker.com/wordfa
Please post any follow-up in the newsgroup. I do not reply to Word question
by emai
 
D

Doug Robbins - Word MVP

I don't really know what you are trying to do, but your syntax is incorrect.

It should be more like

Dim objBuiltInProp As DocumentProperty

For Each objBuiltInProp In ActiveDocument.BuiltInDocumentProperties
If objBuiltInProp.Name = "Title" Then
objBuiltInProp.Value = "New Title"
End If
Next


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
eowen said:
Picking up this thread from a few years ago. (See below.)

I'm in the exact same predicament as David, below. I've tried Jonathan
West's suggested workaround of defining an object variable for the
BuiltInDocumentProperties collection, but I think I'm finding that you
cannot assign to BuiltInDocumentProperties, only read from it.
That is, I do the following:

dim objBuiltInProps as DocumentProperties

set objBuiltInProps = ActiveDocument.BuiltInDocumentProperties

' do some stuff to modify the values of the properties in objBuiltInProps
...

set ActiveDocument.BuiltInDocumentProperties = objBuiltInProps


I get a run-time error on the last line, telling me that the object
doesn't support this property or method.
Am I doing this wrong, or is there some other way to go about it?

For the time being, I'm assigning each property in the objBuiltInProps
collection to the corresponding property in
ActiveDocument.BuiltInDocumentProperites collection, which causes the whole
word count problem to appear again -- Word recounts the words in the
document once for each property involved.
thanks
eowen

------------------------------------------

Hi David,

There are a number of workarounds.

1. Define an object variable for the CustomDocumentProperties collection,
make your changes to that, and then assign the variable back to the
CustomDocumentProperties collection. This will reduce the number of times
the word count runs to just twice. You can do similar things with the
BuiltInDocumentProperties collection

2. Drop back to use of the WordBasic object, and use the SetDocumentProperty
method. As MS has not seen fit to properly document the WordBasic object in
VBA Help, the best source of information is the WordBasic Help file from
Word 95, which I keep handy for just such an occasion.

Try this

WordBasic.SetDocumentProperty "My Property", 0, 2, "My property value"

WordBasic command always apply to the ActiveDocument, so beware of this. The
full entry of SetDocumentProperty is as follows

SetDocumentProperty Name$, Type, Value[$], CustomOrBuiltIn

Defines a document property for the active document. If Name$ is a built-in
property that doesn't match the type specified by Type, or if it is a
read-only property, an error occurs. Use the DocumentPropertyExists()
function to determine whether a property called Name$ already exists.
For a list of the built-in properties available in Word, see
DocumentPropertyName$(). In Word version 6.0, SetDocumentProperty is
unavailable and generates an error.

Argument Explanation
Name$ The name of the property. Name$ can be a built-in property or a custom
property.
Type The type of the property to be defined:0 (zero) String1 Number2
Date3 Yes or NoIf Name$ is a built-in property and doesn't match the type
specified by Type, an error occurs.
Value[$] The property value. Value[$] must match the type specified by
Type.If Value[$] doesn't match the type specified by Type, an error occurs.
CustomOrBuiltIn Specifies whether Name$ is a custom property or a built-in
property:0 (zero) or omitted Name$ is a custom property unless it appears
in the list of built-in properties. If Name$ is a built-in property,
SetDocumentProperty will update its value.1 Name$ is a built-in property. If
it is not one of the built-in properties, an error occurs. 2 Name$ is a
custom property, regardless of whether a built-in property with the same
name already exists.


3. If you don't mind closing the document first, then you can read and write
document properties using an ActiveX DLL freely downloadable from the
Microsoft web site. Take a look here for details
http://www.mvps.org/word/FAQs/MacrosVBA/DSOFile.htm

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email

David J. Young said:
I have some VBA code that adds a number of custom document properties
(File->Properties, Custom tab), and modifies a number of the Builtin
Document properties.
"Application.ScreenUpdating = False" and "Application.DisplayStatusBar =
False" does not help. changes
have been made?
 
E

eowen

Dou

Thanks for the reply

Here's a recap of the issue at hand: Whenever you access any of Word's built in document properties via VBA, it causes Word to do a recount of the number of words in the document -- presumably so that the built-in document property of "word count" is accurate. For example, the following VBA code causes Word to do a word count of the document

dim p as DocumentPropert

set p = ActiveDocument.BuiltInDocumentProperties("Title"

When you are accessing the built in document properties many times, as my code does, and when your document is large (or even only 90+ pages), this causes a severe performance hit

A suggestion made by Jonathan West (some years ago now) was this
1. Define an object variable for the CustomDocumentProperties collection
make your changes to that, and then assign the variable back to th
CustomDocumentProperties collection. This will reduce the number of time
the word count runs to just twice. You can do similar things with th
BuiltInDocumentProperties collectio

This does indeed provide a method to access the built in document properties and only incur the word count performance hit once upon reading. However, if you need to change the built in document properties and get your changes back into the document, it would appear that the BuiltInDocumentProperties collection cannot be assigned to, only assigned FROM. Once again, here's the code that fails

dim objBuiltInProps as DocumentProperties ' I need the whole COLLECTION, not a single propert

set objBuiltInProps = ActiveDocument.BuiltInDocumentPropertie

' do some stuff to modify the values of the properties in objBuiltInProp
' e.g.
objBuiltInProps("Title").Value = "New title
...

set ActiveDocument.BuiltInDocumentProperties = objBuiltInProps ' this line produces a run time erro

The run time error is "object does not support this property or method" (or whatever the exact wording is)

So my question is whether there is some way to accomplish what I want to do, short of assigning each individual property in the objBuiltInProps collection to each individual property in ActiveDocument.BuiltInDocumentProperties. I don't want to do that because that fires off a word count for each property that is set, which is unacceptable performance-wise

Does that clear things up
eowen
 
P

Peter Hewett

Hi eowen

I've not explored this issue deeply. The following is based on the assumption that
accessing ANY Built In Document Property (BIDP) causes Word counting to occur.

There's no benefit in creating an object reference to either an individual BIDP or the
BIDP collection. All you are doing is creating a variable with a new name that is a
pointer to BIDP or BIDP collection. So forget this as an option.

What would work is to create your own Collection object or just store the individual
values in string variables or an array. You can't always copy the entire collection of
DocumentProperties as the reference can be invalid. Take for example the "Last save time"
property. This is an invalid reference unless you have saved the document. So I'd be
fussy and just add the BIDP's you want to your collection:

Private mcolBIDP As Collection

Public Sub BIDPDuplicate()
Dim varBIDPValue As Variant

Set mcolBIDP = New Collection

' Copy BuiltInDocumentProperties to a private collection using
' the Property Name as the key
With ActiveDocument.BuiltInDocumentProperties
Debug.Print .Item(wdPropertyAuthor)
AddToCollection1 .Item(wdPropertyAuthor)
AddToCollection1 .Item(wdPropertyComments)
AddToCollection1 .Item(wdPropertyCompany)
AddToCollection1 .Item(wdPropertySubject)
AddToCollection1 .Item(wdPropertyTitle)
End With

' Dump private collection of document property values
For Each varBIDPValue In mcolBIDP
Debug.Print varBIDPValue
Next
End Sub

Private Sub AddToCollection1(ByVal bdpItem As DocumentProperty)
mcolBIDP.Add bdpItem.Value, bdpItem.Name
End Sub


The code above demonstrates how you can build you own collection of BIDP's. You have to
refer to this collection using the property name:
Debug.Print mcolBIDP("Title")

or you could use strings!

You have another option and that's to use Custom Document Properties (CDP) instead and
bypass this problem.

HTH + Cheers - Peter
 
E

eowen

Pete

Thanks for the reply

----- Peter Hewett wrote: ----

There's no benefit in creating an object reference to either an individual BIDP or th
BIDP collection. All you are doing is creating a variable with a new name that is
pointer to BIDP or BIDP collection. So forget this as an option

I understand this and wondered about this myself before I tried it, but I could swear I did NOT see the word count occur when I accessed the BIDP via my object refence. I will confirm

----- Peter Hewett wrote: ----

You have another option and that's to use Custom Document Properties (CDP) instead an
bypass this problem

Unfortunately the requirements for my project are that it store certain things in the BIDP. I store plenty of other things in the CDP, but where there is an existing BIDP I have to use it--another process external to this VBA tool expects to find things in the built in properties

I'll report back on whether the object reference method invokes the word count or not

Is there no way (setting, property, etc.) to convince Word it doesn't need to do a word count

eowe
 
E

eowen

Hi Pete

Reporting back, per my last post, re

----- Peter Hewett wrote: ----

There's no benefit in creating an object reference to either an individual BIDP or th
BIDP collection. All you are doing is creating a variable with a new name that is
pointer to BIDP or BIDP collection. So forget this as an option

There DOES appear to be the benefit of not invoking Word's automatic word count. By referencing the BIDP indirectly one must avoid triggering this somehow

Here's when I do and don't see the word count invoked by my code

dim objBuiltInProps as DocumentPropertie
dim p as DocumentPropert

set objBuiltInProps = ActiveDocument.BuiltInDocumentProperties ' this causes a word coun

set p = ActiveDocument.BuiltInDocumentProperties("Title") ' this causes a word coun

set p = objBuiltInProps("Title") ' this does NOT cause a word coun

thank
eowen
 
E

eowen

Figured things out

My "understanding" of the pointer to the BIDP collection was only partial. Peter is correct that

set objBuiltInProps = ActiveDocument.BuiltInDocumentPropertie

merely creates another way to reference the BIDP collection. But the plus side is that "reading" the BIDP collection this way does not trigger the automatic word count

What I failed to realize or even check for was that doing things like

objBuiltInProps("Title").Value = "New title

actually changes the BIDP collection. Of course it does, because objBuiltInProps is just a pointer to the BIDP. And, happily, making this kind of assignment does not trigger the automatic word count either

Thanks for the help
owen
 
P

Peter Hewett

Hi eowen

If what you say is correct (I only say this because I have not had the time to test this
myself) suspect that it not the Property reference that's causing the Word count, but the
Collection object reference. Based on your info I came up with the following simple Sub's
and hooked them to a toolbar:

Private objBuiltInProps As DocumentProperties
Private p As DocumentProperty

' This causes 4 Word counts to occur
Public Sub TriggerCount1()
Set p = ActiveDocument.BuiltInDocumentProperties("Title")
Set p = ActiveDocument.BuiltInDocumentProperties("Subject")
Set p = ActiveDocument.BuiltInDocumentProperties("Author")
Set p = ActiveDocument.BuiltInDocumentProperties("Company")
End Sub

' TriggerCount2 causes 1 Word count to occur, TriggerCount3 causes none
Public Sub TriggerCount2()
Set objBuiltInProps = ActiveDocument.BuiltInDocumentProperties
End Sub
Public Sub TriggerCount3()
Set p = objBuiltInProps("Title")
End Sub

' This causes 1 Word count to occur
Public Sub TriggerCount4()
With ActiveDocument.BuiltInDocumentProperties
Set p = .Item("Title")
Set p = .Item("Subject")
Set p = .Item("Author")
Set p = .Item("Company")
End With
End Sub

So it's definitely the collection object reference that does it. I guess it sort of makes
sense when you think about it (not nice, but sensible). Word does not do word counting as
a background task so when you ask Word about the number of words it has in a document via
a BIDP then it has to calculate that before it can return it!

HTH + Cheers - Peter
 
P

Peter Hewett

Hi eowen

For anyone interested. The reason for this behavior is explained at the end of the
original thread. But in summary the Word count is caused by accessing the
BuiltInDocumentProperties (BDP) collection NOT it's members. So code like this is to be
avoided at all costs:

Private p As DocumentProperty

Public Sub TriggerCount1()
Set p = ActiveDocument.BuiltInDocumentProperties("Title") ' this causes a word count
Set p = ActiveDocument.BuiltInDocumentProperties("Subject") ' this causes a word count
Set p = ActiveDocument.BuiltInDocumentProperties("Author") ' this causes a word count
Set p = ActiveDocument.BuiltInDocumentProperties("Company") ' this causes a word count
End Sub

as each statement triggers a word count, so 4 word counts are performed. Use code like
this, this triggers only 1 word count:

Public Sub TriggerCount4()
With ActiveDocument.BuiltInDocumentProperties
Set p = .Item("Title")
Set p = .Item("Subject")
Set p = .Item("Author")
Set p = .Item("Company")
End With
End Sub

HTH + Cheers - Peter
 

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