0

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 ...

arvee
  • 21
  • 1
  • 6
  • possible duplicate of [How can a Word document be created in C#?](http://stackoverflow.com/questions/10412/how-can-a-word-document-be-created-in-c) – Michel de Nijs Feb 27 '15 at 09:50
  • I think this article will help you: https://msdn.microsoft.com/en-us/library/6b9478cs.aspx – netwer Feb 27 '15 at 10:14

2 Answers2

0

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.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • The COM API is a little outdated, it is normally recommended you use the [Open XML SDK](https://msdn.microsoft.com/en-us/library/office/bb448854.aspx) provided by Microsoft first, then if you need a feature it does not support fall back to the COM API. – Scott Chamberlain Jul 13 '16 at 16:38
-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?

arvee
  • 21
  • 1
  • 6
  • How is this a solution? [`File.WriteAllText`](https://msdn.microsoft.com/en-us/library/system.io.file.writealltext(v=vs.110).aspx): "Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten" -- You are not "inserting" text into a document and saving it, you are creating a brand new *text* file that happens to have a `.docx` extension. 1) It is *not* a Word document, and 2) you did not "insert" or append text to the document, you blasted whatever document was there and made a whole new one. – Quantic Jul 13 '16 at 16:32
  • @Quantic even though this answer is months old, insert text and blasting the old file and recreating it can be synonymous (at least in terms of word documents). If he loads the file first into txtResult, then adds the lines he want, this will be the same as 'appending' lines at the end, as word does not have an event based system. – Dispersia Jul 13 '16 at 16:39
  • 1
    @Dispersia no, because .doc and .docx files are not plaintext files. .doc is a propriatary format and .docx is a renamed .zip file with .xml files inside of it. Neither of those will work with `File.WriteAllText`. – Scott Chamberlain Jul 13 '16 at 16:41
  • Um, heh, **no. This is _very_, _very_ incorrect like** @Quantic **said.** – Momoro May 02 '20 at 02:10