Hello all,

I am using Word AddIn , i have a button on my Ribbon and i would like to save the current file on click of the button.

How to do it ?????????

Recommended Answers

All 8 Replies

can you post code what you have done so far? where is current file?

Is your problem trying to find a ".Save()" method for your add-in or locating the My Documents path programmatically? If is the former then you need to post your code, if it is the latter then you can use Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

hello Thankx for reply ,

Now I will explain you what i want to do,

As i mentioned i have a ribbon on which i have a button on click of that button i have to save the current word file in my documents.

For this i used SaveDialogBox and File Stream.

But i am not getting how to read the Text enter on the word and write it in the file to save ??????

following is my code (Not working as i want):

private void button6_Click(object sender, RibbonControlEventArgs e)
        {
            saveFileDialog1.InitialDirectory = "My Documents";
            saveFileDialog1.FileName = "AddRecord.docx";
            saveFileDialog1.DefaultExt = ".docx";
            saveFileDialog1.OverwritePrompt = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Word.Application wordapp = new Microsoft.Office.Interop.Word.Application();
                Word.Document doc = new Microsoft.Office.Interop.Word.Document();
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);

               // if (fs.Length > 0)
                //{
                    //StreamReader sr = new StreamReader(fs);
                    StreamWriter sw = new StreamWriter(fs);
                   // string Data = sr.ReadToEnd();
                    doc.ActiveWindow.Selection.WholeStory();
                    IDataObject data = Clipboard.GetDataObject();
                    string text = data.GetData(DataFormats.Text).ToString();
                    sw.Write(text);
                    sw.Close();
                 //   sr.Close();
                //}
                //else
                //{
                  //  MessageBox.Show("File saved with No Contents");
                //}

            }
        }

What about it is failing? You should probably update this line of code:

//saveFileDialog1.InitialDirectory = "My Documents";
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

Also -- Why are you moving this data over to the clipboard and saving it? Please explain what you are doing.

just want to save Text what ever entered by user on word file using onclick of my button on ribbon......

follow what sknake suggests.

Yash: Obviously you're doing some extra work here to stop word from saving in its' native file format. Are you trying to get the text out as a string, rich text, ... ? There is a .Save() method somewhere off of your word document you could call to save the .docx. There have been a number of posts on this forum about saving microsoft word documents so I would suggest you search if that is the case.

Something else you might consider doing:

string text = data.GetData(DataFormats.Text).ToString();
File.WriteAllText(saveFileDialog1.FileName, text);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.