C# Word加载项功能区

C# Word加载项功能区,c#,ms-office,add-in,office-addins,C#,Ms Office,Add In,Office Addins,我创建了一个Office加载项项目,并为应用程序添加了功能区菜单。当我构建项目word文档并使用功能区时,没有问题 使用下面的按钮单击事件从功能区菜单单击按钮时,如何使用StreamReader将活动文档保存为文件 private void btnsavefile_Click(object sender, RibbonControlEventArgs e) { //Getting FileStream here. } 我在堆栈溢出中找到了以下解决方案。希望这与你有关 就我个人而言

我创建了一个Office加载项项目,并为应用程序添加了功能区菜单。当我构建项目word文档并使用功能区时,没有问题

使用下面的按钮单击事件从功能区菜单单击按钮时,如何使用StreamReader将活动文档保存为文件

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

}

我在堆栈溢出中找到了以下解决方案。希望这与你有关

就我个人而言,我在处理这种情况时也做过同样的事情。我已将文件副本保存到临时位置,并将副本推送到服务器。在这种情况下,活动文档保持原样

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

希望这有帮助

创建Word加载项项目->从添加新项添加功能区可视化设计器

将菜单添加到Ribbon designer,并在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);
    }
  }
}
公共部分类RibbonSample
{
private void RibbonSample_Load(对象发送方,RibbonIEVENTargs e)
{
//初始化log4net
}
//从数据库在菜单中添加项目
公共RibbonSample()
:base(Globals.Factory.GetRibbonFactory())
{
初始化组件();
尝试
{
System.Data.DataTable dt=新数据访问().GetData();
如果(dt.Rows.Count>0)
{
对于(int i=0;i
关闭前作废应用程序文档(Word.Document文档,参考bool Cancel) { 尝试 {

        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)
    {

    }

}

什么文件流?System.IO.StreamReader在Office中运行良好Addins@JohnKoerner我如何使用功能区菜单中的StreamReader读取活动文档?您到底想保存什么?电子邮件正文?整个电子邮件使用相当于文件另存为.msg扩展名?@Magnum我想将文件保存到MsSql服务器,因此我需要获取文件流。