how to insert text from c# application into MS word document and save it as new file

末鹿安然 提交于 2019-12-25 08:42:50

问题


I have created a desktop application which in that I need to save texts of textbox into into a word document, the document should be created by user and the position of the document would be chosed by user and then the value of textbox should be saved into that.

for example:

"this is textBox value"

then there is a save button, by clicking that the save window should be opened then user will give a name to that and set its position then click OK finally the text of textbox will be saved into that word document.

any idea how to do it? I have searched allot in google but I couldn't find anything to find the way ...


回答1:


Word exposes a COM API, search for Word Interop or Office Interop.

With this API you may start word, load a document, add to this document, for example at the current cursor position and save the document to a new file.




回答2:


I have found a solution for my own question, I thought to share it if anyone else has faced this kind of problems, here is the code:

    private void btnSave_Click(object sender, EventArgs e)
    {


        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.Filter = "Word document|*.doc";
        saveFileDialog1.Title = "Save the Word Document";
        saveFileDialog1.FileName = "MS word document.docx";

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            File.WriteAllText(saveFileDialog1.FileName,txtResult.Text.ToString());
        }
    }

this works fine, but I want to set font-family and font-size, if anyone knows please share your solution?



来源:https://stackoverflow.com/questions/28761877/how-to-insert-text-from-c-sharp-application-into-ms-word-document-and-save-it-as

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!