none
VS2010 Word 2010 Template Project; reference to the document RRS feed

  • Question

  • Hi Everyone,

     

    I am creating a Word 2010 Template project using VS2010 and C#.  The project has a Windows form with a set of radio buttons.  When a particular radio button is selected, I want to insert a dotx file into template's document.  The problem is, I am having trouble getting a reference to the template document.  Here is my code for the radio button's checked event handler:

     

     

    using Word = Microsoft.Office.Interop.Word;
    
    ...
    
        private void rbPSRG_CheckedChanged(object sender, EventArgs e)
        {
          Word.Range docRng;
    
          Word.Document oWordDoc = Globals.ThisDocument;
    
          docRng = oWordDoc.Range(0, 0);
         
          docRng.InsertFile(@"C:\Data\Office\shared\psrg_outline.dotx");
    
          chkPSRG.Checked = true;
          chkPSR.Checked = false;
          chkPOSTG.Checked = false;
          chkPOSTG.Checked = false;
          chkAppexB.Checked = false;
          chk2A.Checked = true;
          chk2B.Checked = false;
          chk3A.Checked = true;
        }
    
    


    The problem is where I am using "... = Globals.ThisDocument."  It's returning an error message that reads:

    "Cannot implicitly convert type <namespace>.ThisDocument to 'Microsoft.Office.Interop.Word'"

     

    I'm  fairly new to.NET programming and I have to admit that I am struggling with getting references to something outside of a class.  I queried the forum and as a result have tried many things, but haven't found the answer.

     

    Any guidance would be appreciated!

     

    Susan

    Tuesday, July 12, 2011 3:45 PM

Answers

  • Hi Susan,

     

    Thanks for your post.

     

    I think you need some code like below:

     

            private void radioButton1_CheckedChanged(object sender, EventArgs e)

            {

                Word.Document oDOC = Globals.ThisDocument.Application.ActiveDocument;

                Word.Range rng = oDOC.Range(0, 0);

                string path = @"C:\TestTemplate.dotx";

                rng.InsertFile(path);   

            }

     

    ThisDocument is a class in VSTO document-level project, you can access the class by using Globals.ThisDocument property.

     

    For some information about ThisDocument & Globals, please refer to:

    Global Access to Objects in Office Projects

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 13, 2011 6:28 AM
    Moderator
  • Hi Susan

    I'm not quite clear about why you unmarked your code as the Answer... Do you still have a problem?

    There's another way you can do what you want to do, just FYI. You could pass the Document to the constructor of the Windows Form. Just to round out the discussion :-)


    Cindy Meister, VSTO/Word MVP
    Wednesday, July 13, 2011 8:14 AM
    Moderator

All replies

  • Hi Susan,

    Change from this:

    Word.Document oWordDoc = Globals.ThisDocument;

    to this

    Word.Document oWordDoc = Word.ActiveDocument;

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, July 12, 2011 4:12 PM
  • Hi Rich,

    Thanks for the help!

    When I tried this, I get the message:

    "The type or namespace name'AcitveDocument' does not exist in the namespace 'Microsoft.Office.Interop.Word' (are you missing an assembly reference?)"

    Yet when I go to the Object Browser, and search for ActiveDocument, I can see that ActiveDocument is a member of Microsoft.Office.Interop.Word._Application so I tried:

    Word.Document oWordDoc = Word._Application.ActiveDocument;

    This results in the following error message:

    "An object reference is required for the non-static field, method, or property 'Microsoft.Office.Interop.Word._Application.AcitiveDocument.get"

    sigh.

    So I tried these:

    Word.Document oWordDoc = Word._Application.ActiveDocument();

    and

    Word.Document oWordDoc = Word._Application.ActiveDocument.get();

    Neither worked.

     

    Tuesday, July 12, 2011 4:33 PM
  • Hi Susan,

    Sorry I don't work in C# so obviously I misled you. I think maybe the command should be:

    Word.Document oWordDoc = this.Application.ActiveDocument;


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, July 12, 2011 4:54 PM
  • PS - here's a good reference link for you to bookmark.

    http://msdn.microsoft.com/en-us/library/bb157880.aspx


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, July 12, 2011 4:59 PM
  • Hi Rich,

    No, that's not quite working either.  The block of code is in the Windows Form class, so "this" is referring to the form. 

    I have tried Globals too, and didn't get anywhere.

    Any other ideas?

    Susan

    Tuesday, July 12, 2011 4:59 PM
  • I got it!!  I don't really understand it, but here it is.  I had to use Microsoft.Office.Interop.Word to get the range and Microsoft.Office.Tools.Word to get a reference to the template document.

     

    using Word = Microsoft.Office.Interop.Word;
    using WordTools = Microsoft.Office.Tools.Word;
    
    
    
    
        private void rbPSRG_CheckedChanged(object sender, EventArgs e)
        {
          object startIndex = 0;
          object endIndex = 0;
          Word.Range docRng;
          WordTools.Document oWordDoc = Globals.ThisDocument;
     
          docRng = oWordDoc.Range(ref startIndex, ref endIndex);
         
          docRng.InsertFile(@"C:\Data\Office\shared\psrg_outline.dotx");
    
          chkPSRG.Checked = true;
          chkPSR.Checked = false;
          chkPOSTG.Checked = false;
          chkPOSTG.Checked = false;
          chkAppexB.Checked = false;
          chk2A.Checked = true;
          chk2B.Checked = false;
          chk3A.Checked = true;
        }
    
    


     

    Tuesday, July 12, 2011 5:22 PM
  • Hi Susan,

    I'm very glad you figured it out. I really need to swear off trying to help anyone using C# :-)

    Have a great day!


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, July 12, 2011 5:33 PM
  • Hi Susan,

     

    Thanks for your post.

     

    I think you need some code like below:

     

            private void radioButton1_CheckedChanged(object sender, EventArgs e)

            {

                Word.Document oDOC = Globals.ThisDocument.Application.ActiveDocument;

                Word.Range rng = oDOC.Range(0, 0);

                string path = @"C:\TestTemplate.dotx";

                rng.InsertFile(path);   

            }

     

    ThisDocument is a class in VSTO document-level project, you can access the class by using Globals.ThisDocument property.

     

    For some information about ThisDocument & Globals, please refer to:

    Global Access to Objects in Office Projects

     

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 13, 2011 6:28 AM
    Moderator
  • Hi Susan

    I'm not quite clear about why you unmarked your code as the Answer... Do you still have a problem?

    There's another way you can do what you want to do, just FYI. You could pass the Document to the constructor of the Windows Form. Just to round out the discussion :-)


    Cindy Meister, VSTO/Word MVP
    Wednesday, July 13, 2011 8:14 AM
    Moderator
  • Hi Cindy,

     

    "I'm not quite clear about why you unmarked your code as the Answer...  You could pass the Document to the constructor of the Windows Form." 

     

    You just answered your own question!  I checked "Mark as Answer" because what I did works...  But I think my solution is clumsy, so I unchecked in the hopes that someone would respond with a graceful answer.  You and Calvin both provided me with much more sophisticated solutions.  So, therefore, I am much smarter for having unchecked it!

     

    This forum is amazing.  I am truly grateful for it.

     

    Susan


    Thursday, July 14, 2011 1:25 AM
  • That's Beautiful Calvin!  Thank you!

    Susan

    Thursday, July 14, 2011 1:48 AM
  • Hi Susan

    Glad you have what you need :-)


    Cindy Meister, VSTO/Word MVP
    Thursday, July 14, 2011 10:07 AM
    Moderator