none
How to save a word document from a windows form in an add in? RRS feed

  • Question

  • I have a Word Addin which opens a Windows Form. On submitting the form it will create a XML. I want to save the word with the same name as the XML document. I want to save the XML first then the word.
    The Add in and the windows form are two different projects. 
    Save Format ->
    <aaa>_<doctype>_<guid>.xml
    <aaa>_<doctype>_<guid>.docx
    aaa=companycode (data is available in the windows form), 
    doctype=document type of the doc(data is available in the windows form), 

    Please advise..

    Amit Sahoo


    • Edited by AmitSahoo Wednesday, September 12, 2018 3:25 PM
    Wednesday, September 12, 2018 3:18 PM

All replies

  • Hi Amit,

    Please try the following code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WordAddIn1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string path = "C:\\Test\\companycode_xml_xxx.xml";
                FileStream fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                StringBuilder builder = new StringBuilder();
                builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                builder.AppendLine("<Note>");
                builder.AppendLine("<Day year=\"{0}\" month =\"{1}\" day =\"{2}\">");
                builder.AppendLine("<To>{3}</To>");
                builder.AppendLine("<From>{4}</From>");
                builder.AppendLine("<Heading>{5}</Heading>");
                builder.AppendLine("<Message>{6}</Message>");
                builder.AppendLine("</Day>");
                builder.AppendLine("</Note>");
                string show = string.Format(builder.ToString(), "A", "B", "C", "D", "E", "F", "G");
                sw.Write(show);
                sw.Close();
                fs.Close();
                DialogResult result = MessageBox.Show("save successfully", "prompt", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    this.Close();
                }
    
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                app.Visible = true;
    
                app.Documents.Add("C:\\Test\\companycode_xml_xxx.docx");
    
                app.Documents.Open("C:\\Test\\companycode_xml_xxx.docx");
    
                app.Documents.Save();
    
                app.Quit();
    
            }
        }
    }

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Thursday, September 13, 2018 10:22 AM
    Moderator
  • Hi Amit,

    Have you solved your problem?

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Wednesday, September 26, 2018 6:57 AM
    Moderator
  • Hi Amit,

    Did you solevd your problem or any updates for this?  If you have any question, or update, please feel free

    to let me know.

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Friday, September 28, 2018 2:51 AM
    Moderator