简体   繁体   English

Word附加功能区

[英]Word Add-In Ribbon

I created an Office Add-In project and I added ribbon menu for application. 我创建了一个Office加载项项目,并为应用程序添加了功能区菜单。 When I build my project word document have my ribbon there is no problem. 当我建立项目Word文档时,我的功能区就没有问题。

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? 当使用以下按钮单击事件从功能区菜单中单击按钮时,如何使用StreamReader将活动文档另存为文件?

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

}

I found the following solution in Stack Overflow. 我在堆栈溢出中找到以下解决方案。 Hopefully it is relevant to you. 希望它与您有关。

Serialize current ActiveDocument from office 2007 add-in 从Office 2007加载项序列化当前的ActiveDocument

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! 希望这可以帮助!

Create Word Addin project-> Add Ribbon visual designer from add new item. 创建Word加载项项目->从添加新项添加功能区可视设计器。

Add menu to Ribbon designer and write below code in ribbonsample.cs 向功能区设计器添加菜单,并在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);
    }
  }
}

void Application_DocumentBeforeClose(Word.Document document, ref bool Cancel) { try { void Application_DocumentBeforeClose(Word.Document文档,参考bool取消){尝试{

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

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Word加载项-功能区 - Word add-in - ribbon C#Word加载项-功能区 - C# Word add-in - ribbon 创建自定义 Word 插件功能区栏 - 功能区标题应与默认单词功能区标题的大小写相匹配 - Create custom Word add-in ribbon bar - Ribbon title should match the case of default word ribbon titles 在Word 2007功能区加载项中启动线程时出现问题 - Problems starting a thread in a Word 2007 Ribbon Add-in 手动安装我的单词加载项VSTO(功能区)? 用C#构建 - install manually my word add-in VSTO (Ribbon)? built with c# 我们如何使用 Open XML SDK for Office 创建 MS WORD 插件功能区 - How can we create a MS WORD Add-in ribbon uisng Open XML SDK for Office 如何仅在VSTO4加载项中按需自定义Word 2010功能区? - How do I customize Word 2010 ribbon only on demand in a VSTO4 add-in? VSTO中的加载项 - 如何使用带按钮的功能区从Word文档获取文本 - add-in in VSTO - How to get text from Word document using Ribbon with button 是否可以从C#功能区加载项修改高级Microsoft Word选项? - Is it possible to modify advanced Microsoft Word options from a C# ribbon add-in? Visual Studio 上的 Word VSTO 加载项。 为什么我无法打开功能区视觉设计器? - Word VSTO Add-In on Visual Studio. Why can't I open the Ribbon Visual Designer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM