首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Word加载项被阻止

Word加载项被阻止
EN

Stack Overflow用户
提问于 2015-04-30 17:18:08
回答 1查看 128关注 0票数 0

出于某种原因,我的word会阻止外接程序。下面这一行没有给出任何结果。

代码语言:javascript
复制
ActiveDocument.Application.Selection.TypeText("Some String");

它在调试时显示为已执行,但对ActiveDocument没有影响。你知道什么可能的原因吗?Word设置中允许使用宏/加载项。

迈克尔

EN

回答 1

Stack Overflow用户

发布于 2015-07-23 23:46:29

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

代码语言:javascript
复制
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
}

创建功能区设计器:

代码语言:javascript
复制
 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)
            {
            }

        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29963736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档