Darren's profileBeardie's LairPhotosBlogListsMore Tools Help

Blog


    September 01

    How to cache data into a Word Document

    Tech Blog: Geek factor 9/10

     

    I was working on a Visual Studio Tools for Office VSTO solution with Beta 2 of Visual Studio 2005 and I needed a way to hold some configuration data for the document I was working with.  I heard that you can save the data within the document itself into something called a data island and I thought this would be ideal to save my configuration data.

    I won’t go into the technicalities of the solution I was working on only to say that I needed a way to store a DataTable within a Word document that could be accessed at Runtime by the VSTO libraries to then act as the configuration options of my solution.

    So there is two elements to this.

    1. Store a datatable to a word document.
    2. Access the data at runtime from my VSTO document class.

    I’m afraid it’s not an elegant solution and there may be issues with the way that I’m doing it but it appears to work for me and hopefully if you are looking to do this it gives you a head start.  Thanks to some of the guys on the VSTO team at Microsoft for providing me with some information. 

     

    To access a document and insert a DataTable you will need a reference to Microsoft.VisualStudio.Tools.Applications.Runtime.

     

    All code was written is C#.  I’m a glutton for punishment.

     

    //get a reference to the document in the form of a ServerDocument class

    //pass the parameter false to create document without starting Word.

    ServerDocument MyDoc = new ServerDocument(@"C:\test\MyWordDoc.dot", false);

     

    //the top level of the cached data hierarchy is the Host item

    // this is indexed by using the class name of the VSTO solution

    CachedDataHostItem hostItem =

        MyDoc.CachedData.HostItems["MyWordDoc.ThisDocument"];

     

    //if no reference then add the item

    if (hostItem == null)

        hostItem = MyDoc.CachedData.HostItems.Add("MyWordDoc.ThisDocument");

     

    //Now get the cached data from the Host item

    CachedDataItem cache = hostItem.CachedData["_cachedData"];

     

    //again if this does not exist create it as a dataset with an indexer of _cachedData

    if (cache == null)

        cache = hostItem.CachedData.Add("_cachedData", "DataSet");

     

    // create a new dataset to insert into the doc containing our new data table.  I’ve kept this

    // sample simple but in reality you would probably build a proper data table

    DataSet ds = new DataSet("_cachedData");

    DataTable tbl = new DataTable("NewTable");

    ds.Tables.Add(tbl);

    cache.SerializeDataInstance(ds);

     

    //save and close the ServerDocument

    MyDoc.Save();

    MyDoc.Close();

     

    That’s it our VSTO document now has cached data within it that can be accessed by using the same object collections of CachedDataHostItem containing CachedDataItems.

     

    Now what I needed to do was to access the cached data within a document from within theVSTO projects ThisDocument class itself.

    By declaring a public DataSet within the ThisDocument class and giving it the cached attribute when you run the VSTO solutution the VSTO runtime will look for this dataset and populate it if the document contains cached data.  Here is the declaration to enable this.

     

    public partial class ThisDocument

    {

            //data island within the document 
            [Microsoft.VisualStudio.Tools.Applications.Runtime.Cached()]
            public DataSet _cachedData;

     

    If the document contains cached data the _cachedData variable will be populated.

     

    It is also possible to cache data into a Word document that isn’t enabled for VSTO in other words it has no embedded application manifest.  When you create the ServerDocument object pass the second parameter true which will create the document object using Word, known as client side, as opposed to server side.  To do this you will need to have Word installed on the machine.

    ServerDocument MyDoc = new ServerDocument(@"C:\test\MyWordDoc.dot", true);

     

    One last point is that caching is allowed on any serialisable type so that opens up all sorts of options.

     

    I hope this has given you enough information to get to grips with this technology if it’s something you want to do.  It's certainly a useful feature and one I am using myself.

    Comments (4)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    http://www.batteryfast.com/toshiba/pa3395u-1brs.htm toshiba pa3395u-1brs battery ,
    http://www.batteryfast.com/toshiba/pa3421u-1brs.htm toshiba pa3421u-1brs battery ,
    http://www.batteryfast.com/toshiba/pa3431u.htm toshiba pa3431u battery ,
    http://www.batteryfast.com/toshiba/satellite-m60.htm toshiba satellite m60 battery ,
    http://www.batteryfast.com/toshiba/satellite-m65.htm toshiba satellite m65 battery ,
    http://www.batteryfast.com/toshiba/pa3451u.htm toshiba pa3451u battery ,
    http://www.batteryfast.com/toshiba/pa3457u.htm toshiba pa3457u battery ,
    http://www.batteryfast.com/toshiba/satellite-a80.htm toshiba satellite a80 battery ,
    http://www.batteryfast.com/toshiba/satellite-a85.htm toshiba satellite a85 battery ,
    http://www.batteryfast.com/toshiba/pa3465u-1brs.htm toshiba pa3465u-1brs battery ,
    http://www.batteryfast.com/toshiba/pabas069.htm toshiba pabas069 battery ,
    http://www.batteryfast.com/toshiba/pa3536u.htm toshiba pa3536u battery ,
    http://www.batteryfast.com/toshiba/satellite-x200.htm toshiba satellite x200 battery ,
    http://www.batteryfast.com/toshiba/pa3536u-1brs.htm toshiba pa3536u-1brs battery ,
    http://www.batteryfast.com/toshiba/satellite-p200.htm toshiba satellite p200 battery ,
    http://www.batteryfast.com/toshiba/satellite-p205.htm toshiba satellite p205 battery ,
    http://www.batteryfast.com/toshiba/pa3534u-1brs.htm toshiba pa3534u-1brs battery ,
    http://www.batteryfast.com/toshiba/satellite-a205.htm toshiba satellite a205 battery ,
    http://www.batteryfast.com/uniwill/un223.htm uniwill un223 battery ,
    http://www.batteryfast.com/uniwill/223-3s4000-f1p1.htm uniwill 223-3s4000-f1p1 battery ,
    http://www.batteryfast.com/uniwill/223-3s4000-s1p1.htm uniwill 223-3s4000-s1p1 battery ,
    http://www.batteryfast.com/uniwill/bat-243s1.htm uniwill bat-243s1 battery ,
    http://www.batteryfast.com/uniwill/un243.htm uniwill un243 battery ,
    http://www.batteryfast.com/uniwill/un244.htm uniwill un244 battery ,
    http://www.batteryfast.com/uniwill/255-3s4400-g1l1.htm uniwill 255-3s4400-g1l1 battery ,
    http://www.batteryfast.com/uniwill/un255.htm uniwill un255 battery ,
    http://www.batteryfast.com/uniwill/un258.htm uniwill un258 battery ,
    http://www.batteryfast.com/uniwill/258-4s4400-s1p1.htm uniwill 258-4s4400-s1p1 battery ,
    http://www.batteryfast.com/uniwill/258-4s4400-s2m1.htm uniwill 258-4s4400-s2m1 battery ,
    http://www.batteryfast.com/uniwill/755-4s4000-s1p1.htm uniwill 755-4s4000-s1p1 battery ,
    http://www.batteryfast.com/uniwill/un755.htm uniwill un755 battery ,
    http://www.batteryfast.com/acer/1670.php acer LC.BTP05.004 Aspire 1670 series laptop battery ,
    http://www.batteryfast.com/acer/2420.php acer BTP-ARJ1 2420 series laptop battery ,
    http://www.batteryfast.com/acer/as07b72-battery.php Battery Fit Aspire 5520 5920 Series AS07B72 laptop battery ,
    http://www.batteryfast.com/acer/batcl50l.php acer travelmate 290 291 29x 292 batcl50l laptop battery ,
    http://www.batteryfast.com/acer/batecq60.php acer 1800 series batecq60 laptop battery ,
    http://www.batteryfast.com/acer/batefl50l6c40-battery.php Battery For Aspire 5030 5050 5500 5550 5570 5580 laptop battery ,
    http://www.batteryfast.com/acer/btp-39d1.php acer btp-620 btp-39d1 btp-39sn ms2103 laptop battery ,
    http://www.batteryfast.com/acer/btp-43d1.php acer travelmate 22x 23x 26x 28x btp-43d1 laptop battery ,
    http://www.batteryfast.com/acer/btp-550.php acer btp-550p btp-550 bat30n3l batby27l laptop battery ,
    http://www.batteryfast.com/acer/btp-73e1.php acer travelmate 370 380 btp-73e1 btp-50t3 laptop battery ,
    http://www.batteryfast.com/acer/btp-agd1.php acer btp-63d1 btp-agd1 3020 3610 laptop battery ,
    http://www.batteryfast.com/acer/lcbtp03003.php New battery fit Acer LIP-4084QUPC SQU-401 5000 laptop battery ,
    http://www.batteryfast.com/acer/tm3002-battery.php New Battery Acer TravelMate 3002WTCi 3002WTMi laptop battery ,
    Mar. 10
    Aug. 25
    I am _trying_ to do what you're doing in this example. I can confirm that data is being added to my document, because a "read" through a ServerDocument gets me the data like it should.

    However, the code within my document's ThisDocument class, does not get any data. Is simply declaring the dataset with the whole cached-attribute enough, or do you have to do some kind of retrieval also? My app is very simple, but not working.

    Any ideas?

    Jeppe Jespersen
    Denmark
    Feb. 15
    Picture of Anonymous
    Ron wrote:
    I am doing something very similar, but I don't use a dataset. I have a document in which I have set a number of bookmark controls from a VSTO project. For every bookmark I create a string public member in the class with the Cached attribute. From an ASP.NET application, I use the ServerDocument class to update the cached members:
    sd.CachedData.HostItems["MyDoc.ThisDocument"].CachedData["MyCachedString"].SerializeDataInstance("anystring");
    sd.Save();

    In the document:
    this.MyBookmark.Text = this.MyCachedString;

    I know that it is being cached in the document because I can check the XML property of the ServerDocument object's cached data the next time I get the same document, but the bookmark never updates its value. The same approach works perfectly in Excel (NamedRange). I'm running put of ideas.
    Nov. 23

    Trackbacks (4)

    The trackback URL for this entry is:
    http://beardieslair.spaces.live.com/blog/cns!8C2C357940A64396!190.trak
    Weblogs that reference this entry