Link to home
Start Free TrialLog in
Avatar of thedude112286
thedude112286

asked on

VBA open document

I'm not sure if this is the right place for this question so if there is a better place to post it, pleast tell me.  I have a COM addin for MS Word.  Whenever the user presses a button on a toolbar I created in Word, I want to call a method, UpdateDocument, on the COM addin and pass it the full text of the current document.  This is what my application does:

1) When a new Word document is created, I get a DocumentChange event and in it, I call the Init method of my addin.  This returns an id number.

2) When the user clicks the toolbar button, I call the UpdateDocument method and pass it the id number of the ActiveDocument as well as the full text of the Active Document (I've been using ActiveDocument.Range.Text.  Is this right?).

My question:  Is there any way for me to associate an open document with an id number?  I could do it with a global hashtable where ids were indexed by filenames of documents, but VBA doesn't have a hashtable structure.  Maybe it's possible to set variables or store data in documents themselves and access these variables/data via ActiveDocuement?  Is there any way to do this?

P.S. I am running VBA code from a template if that's important.  Thank you very much for your help.  If you need my VBA code, just ask.
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could use the Dictinary object from the Microsoft scripting library to provide hashtable functionality (add a reference to 'Microsoft Scripting Runtime' - scrrun.dll). It provides basic hashtable functionality and is very easy to use. This should be possible with VBA too.


Dim dic as new Dictionary

' Store
set dic.Item(id) = WordDocument

'Retrieve
Dim doc as WhateverAWordDocumentIs

' Check if Id is valid
If dic.Exists(id) then
    set doc = dic.Item(id)
End If

' Do something with doc

Best regards,
Matthias

Avatar of GrahamSkan
You can create and store an indefinite number of variables in a Word document

Doc.Variables.Add "MyVariable", "MyValue"
Storedvalue = Doc.Variables("MyVariable")
Avatar of thedude112286
thedude112286

ASKER

Excellent job.  I can't thank you enough for your very quick and extremely helpful response!  Keep up the good work!