none
Insert a QuickPart in a word document RRS feed

  • Question

  • I want to Insert a textbox in a word document programmatically. This has to be done for all documents in a SharePoint library. I am planning to use OpenXml. This is the structure I came up with but I did not have any success. Can anyone suggest how to add a textbox to a word document.

    using (WordprocessingDocument wordDoc =
                        WordprocessingDocument.Open(mem, true))
                       {
                           //ADD THE CODE TO ADD TEXTBOX HERE
                wordDoc.MainDocumentPart.Document.Body.Append(/*TEXT BOX*/);
                         
                       }
                       string linkFilename = file.Item["LinkFilename"] as string;
                       file.ParentFolder.Files.Add(linkFilename, mem, true);
                   }
    Any input will be very helpful.


    • Edited by shankze Sunday, October 6, 2013 7:58 PM
    Friday, October 4, 2013 3:33 AM

Answers

  • I still don't understand what it is you want to accomplish using the Open XML SDK. The Open XML SDK doesn't really work like this - it does not mimic user actions, the way the programming APIs do.

    If you mean you want to insert the result of the screen shots you show, then your best bet is to analyze a document that already contains them using the Open XML SDK Productivity tool. That can generate code that shows how to create the content in a document.

    Please note, however, that the Open XML SDK will not have direct access to "Quick Parts" stored in a template available to the user. It makes no sense for you to try to use the "Simple Text Box" Quick Part. You need to construct it from scratch.

    Ditto for the document property: you need to analyze the code the Productivity tool shows you.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 8, 2013 4:11 PM
    Moderator

All replies

  • There are three or four different things in Word that are called "textbox". Which one do you mean, and please be very specific. If you're unsure how to describe it, give us the steps you use to insert it.

    Also, I recommend you create a very simple document with such a textbox in it, then view the XML in the document.xml file of the Zip package. That will give you some idea of how Word constructs the control in its XML.

    You can then use the Open XML SDK Productivity Tool to get the basic syntax to generate the document.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 4, 2013 2:11 PM
    Moderator
  • Thanks for suggesting Open XML SDK Productivity Tool. I want the SharePoint custom column to be displayed in the Word Document. So I am planning to include a QuickPart to display the version number. To position the quickpart, I am planning to insert the quick part in the Textbox.

    So I want these actions to be performed in a Word Document -  

    Insert -> TextBox -> SimpleTextBox

    

    Then select the Textbox, Insert -> Quickparts -> Document Property  -> Custom Metadata

    How do I add the QuickPart and the TextBox to the document programmatically?

    Sunday, October 6, 2013 7:58 PM
  • I still don't understand what it is you want to accomplish using the Open XML SDK. The Open XML SDK doesn't really work like this - it does not mimic user actions, the way the programming APIs do.

    If you mean you want to insert the result of the screen shots you show, then your best bet is to analyze a document that already contains them using the Open XML SDK Productivity tool. That can generate code that shows how to create the content in a document.

    Please note, however, that the Open XML SDK will not have direct access to "Quick Parts" stored in a template available to the user. It makes no sense for you to try to use the "Simple Text Box" Quick Part. You need to construct it from scratch.

    Ditto for the document property: you need to analyze the code the Productivity tool shows you.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 8, 2013 4:11 PM
    Moderator
  • If OpenXML SDK does not have direct access to Quick Parts stored in the template, I will have to look for some other way to add the quickpart. Is there any other way I can insert the result of these user actions into the document?

    My requirement is to add a Line of text in the document which shows SharePoint version number. The SharePoint metadata is tied to the document through document properties (Quick Part). I just need a way of displaying this in the document programmatically. I think inserting the quickpart is the best of doint it. I know how to insert it manually (above screenshots). I am struggling to them programmatically.

     
    Tuesday, October 8, 2013 4:21 PM
  • As I mentioned before, create a document that contains these things (and little else) then look at the document in the Open XML SDK productivity tool.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 8, 2013 4:54 PM
    Moderator