Word加载项被阻止

时间:2015-04-30 09:18:08

标签: c# ms-word add-in

由于某些原因,我的字块会阻止加载项。以下行没有结果。

ActiveDocument.Application.Selection.TypeText("Some String");

它在调试时显示为执行,但对ActiveDocument不起作用。你知道任何可能的原因吗? Word设置允许使用宏/加载项。

迈克尔

1 个答案:

答案 0 :(得分:0)

在关闭文件之前将文件存储到数据库中的基本单词插件。

public partial class ThisAddIn
{
    private string friendlyErrorMessage = "An error has occurred inside the Case Manager Word Addin";
    //static string filePath = null;           

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.DocumentBeforeClose += Application_DocumentBeforeClose;
    }

    //public static string dialogFilePath;
    public static string dialogFileName;
    public static Word.Document doc;


    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;
            doc = document;

            string tempFile;
            string tempPath;

            //var form = new SaveFileDialog();
            //form.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            //form.ShowDialog();
            //System.Windows.Forms.MessageBox.Show(form.ToString());
            //_T is for editing template and save file after saving.
            //+ is for saving 

                    //document.Save();
                    var iPersistFile = (IPersistFile)document;
                    iPersistFile.Save(tempPath, false);

                    if (filePath.Contains("_T"))
                    {
                        //Store file into DB
                    }
                    else
                    {
                        //Store file into DB
                    }
                //object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                //document.Close(ref doNotSaveChanges, ref missing, ref missing);
                Word._Document wDocument = Application.Documents[fileName] as Word._Document;
                //wDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
                ThisAddIn.doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
        }
        catch (Exception exception)
        {
        }

    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
}

创建功能区设计器:

 public partial class TestRibbon
    {
        private string friendlyErrorMessage = "An error has occurred inside the Case Manager Outlook Addin";

        private void TestRibbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        public TestRibbon()
            : base(Globals.Factory.GetRibbonFactory())
        {
            InitializeComponent();
            try
            {
                System.Data.DataTable dt = new DBCall().GetData();//Get all menu in word tmenu tab and bind them using following code
                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)
            {
            }


        }

        /// <summary>
        ///  Ribbon Button Click Event 
        /// </summary>
        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)
            {
            }

        }

    }
相关问题