2015-04-30 86 views
0

由於某些原因,我的文字塊加載項被阻止。以下的行沒有結果。Word加載項被阻止

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

它顯示爲在調試時執行,但不影響ActiveDocument。你知道任何可能的原因嗎?在Word設置中允許使用宏/加載項。

邁克爾

用來存儲文件到數據庫關閉文件之前
+1

問題可以被刪除。只是MS Word在後臺啓動了另一個應用程序實例。 – user2868288

回答

0

基本Word插件。

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

     } 

    }