0

ファイルを開いたときにファイルを変更するワードアドインとしてアプリケーションを作成したいと考えています。

だから私はビジュアルスタジオでワードアドインプロジェクトを作成しました.これは基本的に私が持っているコードです:

namespace WordAddIn1
{
    public partial class ThisAddIn
    {
     private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
    {
    MessageBox.Show("doc opened");
    // do my stuff
    }

    #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.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
    }

    #endregion
}
}

問題は、空の単語アプリケーションを起動して (word.exe をダブルクリック)、文書を開いた場合はうまく機能しますが、文書を開くと同時に単語アプリケーションを起動した場合 (.doc ファイルをダブルクリック) は機能しません。

4

2 に答える 2

0

ドキュメントをダブルクリックして Word を開いた場合、DocumentOpen は起動しません。

これを回避するには、ドキュメントが Word で開かれたかどうかを確認し、開いている場合はドキュメントを Application_DocumentOpen メソッドに渡します。

ところで - InternalStartup メソッドのコードを変更したようです。コメントで示されているように、これを行うべきではありませんが、代わりに ThisAddIn_Startup を使用します。

于 2012-06-21T15:19:41.250 に答える
0

アビーが提案したことを行うコードは次のとおりです。

private void ThisAddIn_Startup(object sender, System.EventArgs a)
{
  try
  {
    Word.Document doc = this.Application.ActiveDocument;
    if (String.IsNullOrWhiteSpace(doc.Path))
    {
      logger.Debug(String.Format("Word initialized with new document: {0}.", doc.FullName));
      ProcessNewDocument(doc);
    }
    else
    {
      logger.Debug(String.Format("Word initialized with existing document: {0}.", doc.FullName));
      ProcessOpenedDocument(doc);
    }
  }
  catch (COMException e)
  {
    logger.Debug("No document loaded with word.");
  }
}
于 2012-10-31T19:59:53.200 に答える