Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using C# with MS Word - Any help?

Options
  • 19-09-2007 10:07am
    #1
    Registered Users Posts: 11,038 ✭✭✭✭


    Hey, using C# (Express Edition of it) and one part of what i'm coding requires me to create a word document, i can go about this 2 ways.

    1. Create the entire document via C#
    2. Create a template. Add just what's different into various bookmarks.

    I've decided that option 2 is the quicker, i have an awful lot of text in the document that will be the same, regardless of what inputs are used my user.

    I googled a bit and found this site, it's a microsoft support site. I followed the instructions on how to create a sample word document, it worked grand. Then i went onto the section on bookmarks. I created a quick template with 1 bookmark. I followed the sample code from the page but i keep on getting an error. I've tried googling this but to no avail...

    Can anyone help me please?

    My code:
    object oTemplate = "g:\\sampleTemplate.dot";
    oDoc1 = oWord.Documents.Add(ref oTemplate, ref oMissing,
         ref oMissing, ref oMissing);
    object oBookMark = "MyBookmark";
    oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
    

    The code will compile and run (and create the document based on the template) if i leave out the last line.

    The error i get is:
    Microsoft.Office.Interop.Word.Bookmarks' does not contain a definition for 'Item'

    I presume this means I am missing some reference or something, but I haven't a clue as to what it may be...


Comments

  • Registered Users Posts: 11,038 ✭✭✭✭dulpit


    Doesn't matter, figured it out...

    Stoopid really, the code given on the site had
    oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
    

    whereas it should be
    oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = "Some Text Here";
    


    Note the .get_Item() as opposed to .Item()


  • Closed Accounts Posts: 120 ✭✭samelterrance


    Hi,

    Have a look at this site, apparantely get_item works.
    http://www.ureader.com/message/685423.aspx

    Try get_Item. I think that's fairly standard when automating Office apps
    from C#:

    wd.Range rng = wdDoc.Bookmarks.get_Item(ref objBkmName).Range;


  • Closed Accounts Posts: 120 ✭✭samelterrance


    dulpit wrote:
    Doesn't matter, figured it out...

    Stoopid really, the code given on the site had
    oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
    

    whereas it should be
    oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = "Some Text Here";
    


    Note the .get_Item() as opposed to .Item()


    cool, we must have found the same site :-)


  • Registered Users Posts: 11,038 ✭✭✭✭dulpit


    Cheers, beat you to it :D

    But cheers none the less...


    Kinda odd how a microsoft site would have code that doesn't actually work...


  • Closed Accounts Posts: 120 ✭✭samelterrance


    dulpit wrote:

    Kinda odd how a microsoft site would have code that doesn't actually work...


    :eek: :eek: :eek:
    I have found that problem alot unfortunately!


  • Advertisement
  • Registered Users Posts: 11,038 ✭✭✭✭dulpit


    :eek: :eek: :eek:
    I have found that problem alot unfortunately!


    Really? :rolleyes: Bravo microsoft...


Advertisement