Access Document Variable stored in an Addin

R

Rich007

Hi,
I'm trying to store a document variable in an add-in which lives in Word's
start up directory. I can create the docvariable while the add-in is open
(at design time), but I can't figure out to access it from the add-in's
autoexec macro which runs when Word is started (and the addin loads).

The Variables collection is only an object of Documents, not Templates, so
for example this code will NOT work:

Dim strMyAddinPath As String
Dim oVars As Variables
Dim vVar As Variant
strMyAddinPath = AddIns("MyAddin.dot").Path
Set oVars = Templates(strMyAddinPath & "\MyAddin.dot").Variables '!!!!!!!
For Each vVar In oVars
Debug.Print vVar.Name
Next vVar

Is it possible to access a document variable that is stored in a loaded addin?

Cheers
Rich
 
D

Doug Robbins

Tell us what it is that you want to do with the variables.

--
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, originally posted via msnews.microsoft.com
 
R

Rich007

Hi Doug, thanks for responding.
It's the classic "how do I get a macro to run once" problem, but this time
inside an add-in.

I want the add-in to test the value of a document variable and depending on
its value, either run or don't run my "RunOnce" macro. The test lives in the
AutoExce macro in the Addin (.dot).

The RunOnce macro arranges the toolbars and changes a few other settings to
a kind of "recommended setting", but I don't want to force users to have this
for eternity, so it shouldn't run every time they start Word, just the first
time Word is started after the Addin has been installed.

Currently I'm using a test to see if a specific custom toolbar exists - if
it doesn't the RunOnce is called. I'm looking for a more elegant solution.

Also, I don't really want to have the AutoExce open the Addin (open itself)
so a document variable can be queried as this would slow things down (every
time Word starts). So you see why I'm trying to find a way to work with a
document variable that is in a loaded Addin.dot.... Any ideas?

Cheers
Rich
 
M

Manfred F

Hi Rich007,

You can access Your Add-In via the Templates Collection. Loop through it to
identify Your desired template.
But this doesn't allow You to edit the add-in's variables. To do so, You
have to use the .OpenAsDocument method (W2003), then alter the variable and
save the document.
A better option might be to use custom document properties, which You can
access directly from the template without having ot open and close a
document:
Templates(myTemplateName).CustomDocumentProperties(myPropName).Value = neValue
Templates(myTemplateName).Save

But I wouldn't really recomment this option fpr Your purpose. What happens,
when You send out a new version of Your Add-In? All the user information will
be lost. I'd recomment to write user properties into a file or registry on
the user's pc using the PrivateProfileString property (see VBa help).

Kind Regards,

Manfred
 
R

Rich007

Hi Manfred, thank you very much for responding.
You have confirmed my fears that it is not possible to read/write to a
document variable stored in an Add-in without opening the add-in first.

Great idea about the custom document properties (can't believe I didn't
think of that), but also thanks for making the very valuable point about what
would happen when a new version is released!

I'm going to stay away from writing to the registry as (for some reason) I
see this as a high risk strategy (I don't want to corrupt anything).

I think I will go with your recommendation to write to a file. I've done
this before and found that a simple text file can be read very quickly.

Thanks again.
Cheers
Rich
 
M

Manfred F

Hi Rich

Rich007 said:
Hi Manfred, thank you very much for responding.
...> I think I will go with your recommendation to write to a file. I've done
this before and found that a simple text file can be read very quickly.

writing a file would be my choice also.
Thanks for Your appreciation :)

Manfred
 

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