0
votes

I created an Office Add-In project and I added ribbon menu for application. When I build my project word document have my ribbon there is no problem.

How can I save the active document as a file using StreamReader when clicking on a button from the ribbon menu using the button click event below?

 private void btnsavefile_Click(object sender, RibbonControlEventArgs e)
{
    //Getting FileStream here.

}
3
What filestream? System.IO.StreamReader works fine Office AddinsJohn Koerner
@John Koerner how can I read active document with StreamReader in Ribbon menu?user1624185
What exactly did you want to save? The body of the e-mail? The entire e-mail using equivalent to the file save-as .msg extension?Magnum
@Magnum I wanna save file to MsSql server so I needd to get file stream.user1624185

3 Answers

0
votes

I found the following solution in Stack Overflow. Hopefully it is relevant to you.

Serialize current ActiveDocument from office 2007 add-in

Personally, I have done the same when I was dealing with this scenario. I have saved a copy of the file to the temporary location and pushed the copy to the server. In this case, the active document stays as is.

Excel.Workbook xlb = Globals.ThisAddIn.Application.ActiveWorkbook;
xlb.SaveCopyAs(filePath);

Hope this helps!

0
votes

void Application_DocumentBeforeClose(Word.Document document, ref bool Cancel) { try {

        string filePath = this.Application.ActiveDocument.FullName.ToString();
        string fileName = this.Application.ActiveDocument.Name;

        //dialogFilePath = filePath;
        dialogFileName = fileName;


        string tempFile;
        string tempPath;


        if (true) 
        {

            var confirmResult = System.Windows.Forms.MessageBox.Show("Are you sure to save this document ??",
                    "Confirm Save!!",
                    System.Windows.Forms.MessageBoxButtons.YesNo);
            if (confirmResult == System.Windows.Forms.DialogResult.Yes)
            {
                //document.Save();
                var iPersistFile = (IPersistFile)document;
                iPersistFile.Save(tempPath, false);

               //Do some action here 
            }

            Word._Document wDocument = Application.Documents[fileName] as Word._Document;
            //wDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
            ThisAddIn.doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
        }

    }
    catch (Exception exception)
    {

    }

}
0
votes

Create Word Addin project-> Add Ribbon visual designer from add new item.

Add menu to Ribbon designer and write below code in ribbonsample.cs

public partial class RibbonSample
{
  private void RibbonSample_Load(object sender, RibbonUIEventArgs e)
  {
    // Initialise log4net 
  }
  //Adding items in menu from DB
  public RibbonSample()
        : base(Globals.Factory.GetRibbonFactory())
    {
        InitializeComponent();
        try
        {
            System.Data.DataTable dt = new DataAcces().GetData();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    RibbonButton Field = this.Factory.CreateRibbonButton();
                    Field.Label = dt.Rows[i][1].ToString();
                    Field.Tag = i;
                    Field.ControlSize =
                        Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
                    Field.Click += Field_Click;
                    menu1.Items.Add(Field);
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("No Fields are available in database");
            }
        }
        catch (Exception exception)
        {
            //thrw exception
        }
    }

//Select menu item text in word 
void Field_Click(object sender, RibbonControlEventArgs e)
{
    try
    {
        Microsoft.Office.Interop.Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
        currentRange.Text = (sender as RibbonButton).Label;
    }
    catch (Exception exception)
    {
        log.Error(friendlyErrorMessage + " Field_Click Details:" + exception.Message, exception);
    }
  }
}