VBA open document

Posted on 2004-11-14
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

    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
    LVL 3

    Expert Comment

    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

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

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

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now