none
Need to modify my Word Template but its already been published RRS feed

  • Question

  • I am just starting to use VS 2012, but I have been developing Office solutions for 10+ years now. I feel like an infant having to learn to crawl all over again.

    With much reading and research, I was able to modify an existing Word 2010 template by adding an Action Pane, and even wiring a bunch of controls on the pane to interact with the content controls and bookmarks in the Word document. THen I figured out how to 'Publish' the solution and install it on my local machine. And it works! Woo Hoo!

    However, I need to add about 50 CustomDocumentProperties into the template. I really didn't want to type them by hand; I have them in another Word file. So I wrote a little code to copy them from one document to the other. It works swell.

    I also want to modify some features on the Action Pane.

    I can open the solution in VS and modify the Action Pane. But I can't run any VB from within VS.

    So if I want to edit the Word Template file (to add the custom doc properties), and then put it back into the Solution, how can I do this? I seem to be getting all kinds of exceptions. And I can see that there are a number of copies of the Word template in the Project folder for this solution.

    Anyone know how to direct me?

    Thanks,

    Rich P

    Tuesday, October 15, 2013 4:23 AM

Answers

  • Hi Rich

    Those three custom document properties need to be in a VSTO solution - they're what link the document to the DLL containing the code.

    What surprises me, however, is that the technology would remove other Custom Document Properties.

    When you opened and processed the template, did it contain those Custom Document Properties? If not, perhaps you didn't get hold of the right one...?

    What do you use the Custom Document Properties for? Perhaps we can find an alternative if it really does turn out that VSTO isn't going to allow you your own... (I think it must; I can't imagine it wouldn't; but it's not something I've ever investigated before now.)

    <<Also, if I want to swap out the template in the solution, can I drop a different template in the folder?>>

    Mmm, you can't really do that... You'd need to either start a new project and copy in the code OR you can attach the same solution to a different (additional) document(s) using the Server Document functionality: http://msdn.microsoft.com/en-us/library/bb772073.aspx


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Quad Guy Wednesday, October 16, 2013 4:55 PM
    Tuesday, October 15, 2013 5:15 PM
    Moderator

All replies

  • Hi Rich

    Mmmm, it's hard to be sure without "looking over your shoulder", but in principle you should be able to

    1. Uninstall the template, same as you would any other software

    2. Open the solution (*.sln) in Visual Studio and use Build/Clean to make sure everything associated with the "debug" version of the template is removed.

    At this point, your system should have no idea that the template exists as a VSTO solution, either as installed software or as "debug" software.

    If I remember correctly, you should be able to open the "real" template in Word by holding SHIFT so that no code tries to execute. You want to open the template in a top-level project folder, not one in any debug or published folder. The original template, the copy in the project folder, is the one that VS opens when you work on the project and the one it copies when you use debug or publish mode.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 15, 2013 2:10 PM
    Moderator
  • Cindy - Thanks so much for responding. I am honored to be 'speaking' with you. Believe me, if I thought you were available to connect to my machine and show me how to fix this, I would take you up on it in a second!

    I tried your suggestion, and was partially successful. The tricks were knowing which document to open in the project folders, and to hold the shift key down while opening.

    Once opened, I was able to run my utility which copied all of the CustomDocumentProperties to the template. I saved the template and closed Word.

    But when I go back into my VS Project and run the template, all of the CustomDocumentProperties are gone. They are replaced with the three properties entered by the Solution; _AssemblyLocation; _AssemblyName; and Solution ID.

    Am I not able to include/persist CustomDocumentProperties in a project such as this?

    Also, if I want to swap out the template in the solution, can I drop a different template in the folder? Or do I somehow add it through the project in VS? In either case, does it have to be the same file name?

    Thanks so much for getting back to me!

    Rich P

    Tuesday, October 15, 2013 3:42 PM
  • Hi Rich

    Those three custom document properties need to be in a VSTO solution - they're what link the document to the DLL containing the code.

    What surprises me, however, is that the technology would remove other Custom Document Properties.

    When you opened and processed the template, did it contain those Custom Document Properties? If not, perhaps you didn't get hold of the right one...?

    What do you use the Custom Document Properties for? Perhaps we can find an alternative if it really does turn out that VSTO isn't going to allow you your own... (I think it must; I can't imagine it wouldn't; but it's not something I've ever investigated before now.)

    <<Also, if I want to swap out the template in the solution, can I drop a different template in the folder?>>

    Mmm, you can't really do that... You'd need to either start a new project and copy in the code OR you can attach the same solution to a different (additional) document(s) using the Server Document functionality: http://msdn.microsoft.com/en-us/library/bb772073.aspx


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Quad Guy Wednesday, October 16, 2013 4:55 PM
    Tuesday, October 15, 2013 5:15 PM
    Moderator
  • Thanks again Cindy. As for the need for the Properties...this is a very large document (Government Form) originally developed in Office 97. Before being filled out, the tempalte is 28 pages. Once it is filled out, it is typically 50-70 pages. The form is essentially an Insurance Claim form, including everything from checkboxes, items to be circled, blanks to be filled in within sentences, repeating blocks of data, and tables that may need to be expanded based on number of records to be entered.

    This form was filled out by hand until I got involved. My client's data was already in an Access db, so I created a Word template that I could populate though Automation. I created a custom Class to handle the whole job from within Access. When filling out tables, I rely on Bookmarks as a landing zone from which to write data. But for the values that needed to be inserted in the middle of a sentence or paragraph, (an possibly repeated elsewhere in the document), I populated the Custom Doc Properties and then displayed them with fieldcodes.

    I suppose the right way to approach this now is to use Content Controls instead of the properties. But to rewrite this class module would take a ton of time that I can't justify to the client. It would be easier to simply forgo the use of the Cust Doc properties and just target bookmarks instead. This would probably improve the performance (which dropped considerably when we converted to Office 2010!).

    What do you think of that idea?

    I have been killing myself for the last week and a half (unbillable), trying to research how to leverage all this cool new stuff in Office 2010 and VS 2012. But in the end, it seems it's too complicated and expensive to convert. And there really is nowhere to turn for timely tutoring and answers. If you ever decide to author an eBook or something like that on this subject, I have some great examples/ideas for content!

    Thanks again. I will look at the link to see about the Server Document functionality...

    Rich P

    Tuesday, October 15, 2013 5:38 PM
  • Hi Rich

    <<I have been killing myself for the last week and a half (unbillable), >>

    I can identify with that <sigh>

    If you don't need Custom Document Properties (and the suggestions I made in my previous reply don't help) the simplest thing would probably be to switch to Document VARIABLES (that's an object, not a variable in your code). Unlike DocProps, these aren't visible to the user. You should be able to use Find/Replace to substitute DocVariable for DocProperty in your document's text. The naming conventions are, as I recall, the same, so no need to change much else. Creating DocVars is actually simpler to code than DocProps as all you need is
         Document.Variables("name") = "STring value"

    Reading is sVar = Document.Variables("Name"). If you set it to an empty string it's deleted from the document.

    Theoretically, yes, content controls mapped to a custom XML Part would be the way to go. But that would involve more learning curve and more changes to your code; adjusting the document wouldn't be possible using Find/Replace.

    Books: I actually cover this in the German Word Programmierung Handbuch, the fourth edition of which will be coming out in a few weeks.

    I am starting work on two e-books - XML for Office VBA Developers, and Open Office File Formats for Office Developers - that I hope to have finished over the next twelve months. I don't expect to be covering this particular scenario in a lot of detail, however. Custom XML Parts will certainly be in the first with some discussion about mapping them to content controls as that is XML. But I'll really be targeting more the XML DOM, XPath, etc. using the MSXML Parser.

    I'd be happy to see your ideas, though, in any case :-)!


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 15, 2013 5:54 PM
    Moderator
  • FYI - I ended up creating a new project based on a clean template. I opened to template (using the Shift Key) from the project folder. I ran my utility to copy the required cust doc properties into the template. All worked fine. But...when I save and close the template and then open the solution in VS and try to run it, I get the following error

    ************** Exception Text **************
    Microsoft.VisualStudio.Tools.Applications.Runtime.CannotCreateStartupObjectException: Could not create an instance of startup object Shell_IL.ThisDocument in assembly Shell_IL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at Shell_IL.ThisDocument..ctor(Factory factory, IServiceProvider serviceProvider) in C:\Users\rich\Documents\Visual Studio 2012\Projects\Shell_IL\Shell_IL\ThisDocument.vb:line 3
       --- End of inner exception stack trace ---

    Maddening...just maddening.

    Rich P

    Tuesday, October 15, 2013 6:04 PM
  • Thanks Cindy - Can I assume that the DocVars are analogous to the TempVars in Access?

    If so, does it really make sense for me to instantiate all those DocVars, populate them, and then process each of them again to populate the document? Or would I be better of simple stuffing the data directly to bookmarks in the template?

    I didn't mention this, but the people who will end up receiving these completed documents don't strip data out of them. (Believe me, I have tried to convince them of the possibilities!!!). But you know how efficient state governments are...they are happy to visually read each page and process them manually.

    So I really don't need to have any manner of embedded data within the final document. Does stuffing into bookmarks make the most sense?

    Thanks.

    Rich P

    p.s. What's the best way to send you some ideas for content?

    Tuesday, October 15, 2013 6:21 PM
  • it's after the end of my workday, so this is just aquickie, while I'm waiting for supper to warm... I'm not familiar with the Access thing, but if you're using docprops there's no reason you shouldn't use docvars. cindymeister AT swissonline DOT ch

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 15, 2013 6:42 PM
    Moderator
  • Thanks Cindy -

    I will look into the DocVars idea. In the meantime, in case anyone else is interested, I am still struggling with the cust Doc Properties in this template.

    Here is what I have done to this point.

    • I prepared my template by adding all of the CustomDocumentProperties and saved the template.
    • I created a new project in VS 2012 based on the template file I just saved
    • I added a User Control (Action Pane) to the project and copied all the WinForm controls from a previous project (to save some time)
    • I copied all the code behind the User Control from my previous version of the project
    • I built the project and had no errors
    • I run the project and the CustomDocumentProperties are gone but the new Assembly and SolutionID properties are present.

    So I figured I would try to outsmart the system...

    • I added a sub to my project that copies the CustomDocumentProperties from another file on my HDD
    • I called that sub from the ThisDocument_Startup() event
    • I build and ran the solution and voila! The properties are there!

    So it seems as though when running the project, any CustomDocumentProperties  are deleted from the template? Can this really be?

    Tuesday, October 15, 2013 7:14 PM
  • Hi Rich

    <<So it seems as though when running the project, any CustomDocumentProperties  are deleted from the template?>>

    Sometimes, it takes a while for the nickle to actually drop...

    When you create a new document from a template I don't think the custom document properties are "inherited"?

    VSTO will write its properties that link the (new) document to the DLL into the document as it's generated. That's why they're there.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 16, 2013 1:58 PM
    Moderator
  • Hi Rich

    Document Variables (docVars) are a space inside the Word document put aside for storage of strings. Traditionally, this is what Word developers have used to store data that should "live" in the document.

    Unlike Document Properties, there is no interface for users to view and mange Document Variables. There is no upper limit on the amount of text they can hold (DocProps can have, as I recall, a max of 255 characters). They cannot be accessed directly from the closed file (we're talking about the old binary file format *.doc), while docProps could be.

    You can display the value of any docVar using a DocVariable field, same as for a document property, using a DocProperty field. That's why this suggestion should be reasonably simple to implement, as compared to going with content controls and custom XML parts.

    I don't know if it makes any difference whether you use field codes or bookmarks - there must have been some reason you're using document properties rather than bookmarks?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 16, 2013 2:03 PM
    Moderator

  • When you create a new document from a template I don't think the custom document properties are "inherited"?

    VSTO will write its properties that link the (new) document to the DLL into the document as it's generated. That's why they're there.


    Back in the old days (last week), when I instantiate from Access, a new Word file based on this template, the Custom Doc properties are certainly there. That's how I was able to populate them.

    This is why it is so puzzling that VSTO strips them before writing its own properties in there.

    Actually, what's even crazier is that by simply adding the template to a VS Project seems to be where the properties are stripped out. And short of entering them by hand, I can't see a way to get them back in once the file is part of the project.

    Rich P

    Wednesday, October 16, 2013 4:45 PM