none
How to save a Word Document from a template that has been created dynamcially using WinForm/C# RRS feed

  • Question

  • I have created a dynamic Word document template that displays data from SQL Server at run-time. After running the application and the Word document shows up, I want to save the document. However, I can NOT do that. There is an option that I can save it as pdf file, but my users want to save the Word document so they can make changes on the document.

    Please help!

    Thank you in advance 

    Sunday, October 25, 2015 12:51 PM

All replies

  • Hi Tim

    You need to provide more information. Since you're posting in the Open XML SDK forum I assume you're using that to create the Word document template? Could you be more specific about the file type, please: what's the extension? Is it docx, dotx, something else?

    Where is the Word document "showing up"? Please show us that part of the code so that we can understand what it's doing, exactly.

    Please describe the menu commands you use when you try to save the document.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Sunday, October 25, 2015 6:01 PM
    Moderator
  • Hi  Cindy,

    Thanks for response. Sorry, but I don't use Open XML SDK (and I don't know how to use it). I created a Window Forms for word template 2010 .dotx. I have created a blank template and programmatically pull out data from SQL Server and "pour" on the blank template.  After running it, the word document .docx shows all data. I'd like to save it but there is no choice on File -> Save to save this document. Thanks in advance for your help.   

    Sunday, October 25, 2015 6:28 PM
  • Hi Tim

    OK, I've moved your question out of the Open XML SDK forum to "Word for Developers".

    Could you please show us the code you use to instantiate the Word application and create the template. I don't want/need the SQL part, just the "administrative" part.

    Also, you mention both dotx and docx in your reply. I'm not quite clear how you're going about things, but maybe that will become clear when we see the code...


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Sunday, October 25, 2015 7:03 PM
    Moderator
  • Hi Cindy,

    I have created the Word template dynamically. The word template is create in the Visual Studio 2010 C# -> Word 2010 Template. The codes manipulate data from SQL server and pull them out and put them on this word template.

    Ds = DataGet("SELECT * From thisTable");

          if (Ds.Tables[0].Rows.Count > 0)

           {

                 DataTable word1;

                 DataView Dv = new DataView(Ds.Tables[0]);

                 }

    Then I format the document by using TableLayoutPanel as following.
    TableLayoutPanel Tbl = new TableLayoutPanel();
    Tbl.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;Tbl.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);

    Then I loop through the dataset and put data into the format

    Tbl.Controls.Add(GetRichTextBox(Dt.Rows[Row]["ID"].ToString() + "  " + Dt.Rows[Row]["Students"].ToString(), true), Col, rowid + 1);

    It works and shows data on the Word document. But I can't save it as a .docx.

    Do you have any idea why it can't be saved? Thanks

     

     
    Sunday, October 25, 2015 10:08 PM
  • Hi Tim

    Mmmm. Word has no such thing as a "TableLayoutPanel" in its object model. When I do a Google search I see there is such a thing for Windows Forms, but this has nothing to do with Word.

    It is possible to place Windows Forms controls on Word documents, using VSTO and from your statement "Visual Studio 2010 C# -> Word 2010 Template" I'm guessing this is a VSTO template. So I will be moving this discussion to the more specialized VSTO forum.

    Before the discussion goes further, have you read the information about using Windows Forms controls in Word. Compare, especially, design time vs. run-time:
    https://msdn.microsoft.com/en-us/library/b86d6w07.aspx?f=255&MSPPError=-2147217396

    You should also consider creating a Word table, rather than trying to use a Windows Forms control which will naturally retain the state of the data when the document is saved and closed.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, October 26, 2015 3:10 PM
    Moderator
  • Hi Tim

    Something else I notice is that the TableLayoutPanel is not available for a VSTO project at design time. It's possible that the control is not supported directly on a document surface.

    I am able, however, to create a User Control and place the DocumentTablePanel on that. Have you tried that?


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, October 26, 2015 4:31 PM
    Moderator
  • Hi Cindy,

    Thank you for your idea. Yes, this should be in the VSTO forum because when I published it, it created a VSTO file. I haven't tried to use User Control and DocumentTablePanel yet. I don't know how. They are new to me. A sample of codes would be great. Do you think by using this way, the word template will be saved as word document?

    Thanks

     

    Tuesday, October 27, 2015 6:34 AM
  • Hi Tim

    I have some more questions for you, because I'm not convinced you're on the right track with your approach.

    1. Are you sure a TableLayoutPanel is the correct way to display your SQL data? If you read the documentation on the TableLayoutPanel control you'll see that it's NOT for creating tables of data. It's for organizing controls on a Windows Form.

    2. Why do you not create a native WORD table? Why do you feel you need to use a WinForm control that will require lots of extra maintainance to maintain state when the document is closed and opened?

    3. Assuming this is really what you want: Why do you feel you need to insert it at run-time? Why not at Design time?


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Tuesday, October 27, 2015 3:16 PM
    Moderator
  • Hi Cindy,

    1. You are right. I use TableLayoutPanel control (Tbl) to organizing controls such as RichTextBox control as following.

    Tbl.Controls.Add(GetRichTextBox(Dt.Rows[Row]["ID"].ToString()))

    2. and 3. I tried to do it dynamically. The Word Template is blank initially. After running the program, data will be filled out on the blank document.

    I don't know how to use a "native WORD table". I have to make some research about this. BTW, after running it, I can go to the [bin] folder, copy everything in there (including dll and vsto files) and paste them in a different folder in my computer. Then I can open this document with data. I don't know if it works when I copy/paste them into other computers.

    Thank you.

    -For saving the dynamic template, I just checked it again and this time even I copied all files, it didn't work. That's strange because sometimes it works sometimes it doesn't.  I have no idea why.
    • Edited by ttim Tuesday, October 27, 2015 9:01 PM
    Tuesday, October 27, 2015 6:18 PM
  • Hi Tim

    The problem for me is that your logic isn't making sense. Why do it dynamically? Why not insert the control at run-time so that all you need to do is fill it?

    I'm going to be off-line for a couple of weeks, so can't follow up on this with you.

    To find out how to create a UserControl at run-time and put the TableLayoutPanel on it, ask in a WinForms forum. But essentially it won't be different from what you're already doing, just a different control type.

    This article might help you with "native" Word tables:
    https://msdn.microsoft.com/en-us/library/aa537149(v=office.11).aspx

    Note that you should be creating a NEW DOCUMENT from the template in order to save as a Word document. Templates are designed to be used in this manner. If you're actually opening the template, so that you're seeing the template name in Word's title bar, then saving is not going to work correctly. You should be seeing something like Document 1 in the title bar.


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Wednesday, October 28, 2015 6:09 PM
    Moderator