VBA open document

Posted on 2004-11-14
Medium Priority
Last Modified: 2012-05-05
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.
Question by:thedude112286
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 12580181
You could use the CustomDocumentProperties object to store custom info with the document:

Dim cdp As DocumentProperties

'Add a document property
Set cdp = ActiveDocument.CustomDocumentProperties
cdp.Add "ID", False, msoPropertyTypeNumber, 1234

'Read the Document property
n = ActiveDocument.CustomDocumentProperties("ID").Value

Expert Comment

ID: 12580305
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

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,

LVL 76

Expert Comment

ID: 12580612
You can create and store an indefinite number of variables in a Word document

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

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question