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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can create and store an indefinite number of variables in a Word document
Doc.Variables.Add "MyVariable", "MyValue"
Storedvalue = Doc.Variables("MyVariable" )
Doc.Variables.Add "MyVariable", "MyValue"
Storedvalue = Doc.Variables("MyVariable"
ASKER
Excellent job. I can't thank you enough for your very quick and extremely helpful response! Keep up the good work!
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