0

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

4
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by sknake
0

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)

0

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

0

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");
                //}

            }
        }

Edited by mike_2000_17: Fixed formatting

0

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.

0

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

0

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);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.