トップ回答者
VSTOアドイン、イベントからアドインの表示、非表示の切り替え方

質問
-
お世話になっております。
Outlookのエクスプローラ画面で、ポップアウトさせない返信や転送のみ、アドインを使用できる方法を探しています。
現状、通常のエクスプローラー画面(ポップアウトさせない返信や転送を選択していない)でアドインをクリックすると、
アラートがでて、使用できないようになっているのですが、そもそもアドイン自体をクリックさせない、もしくは非表示したいと考えています。
Reply,ReplyAll,Forwardイベントから、VSTOアドインの表示、非表示を切り替えられればと考えていたのですが、うまく実装できません。
ヒントになりそうなものなど教えていただけると幸いです。
宜しくお願いします。
P.S. Office365の環境だとRibbonXMLのgetVisibleの実装がうまく動作しない。。。
回答
-
「アドインの表示」だとか「アドインをクリック」というのが「リボンのボタンなどの表示」「リボンのボタンのクリック」という意味だとして。
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load"> <ribbon> <tabs> <tab idMso="TabAddIns"> <group id="MyGroup" label="My Group"> <button label="BUTTON1" id="button1" getVisible="Button1_GetVisible" onAction="Button1_OnAction"/> <button label="BUTTON2" id="button2" /> </group> </tab> </tabs> </ribbon> </customUI>
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using Office = Microsoft.Office.Core; namespace OutlookAddIn1 { [ComVisible(true)] public class Ribbon1 : Office.IRibbonExtensibility { private Office.IRibbonUI ribbon; public Ribbon1() { System.Diagnostics.Debug.WriteLine("Ribbon1()"); } #region IRibbonExtensibility のメンバー public string GetCustomUI(string ribbonID) { System.Diagnostics.Debug.WriteLine("GetCustomUI:" + ribbonID); return GetResourceText("OutlookAddIn1.Ribbon1.xml"); } #endregion #region リボンのコールバック //ここでコールバック メソッドを作成します。コールバック メソッドの追加について詳しくは https://go.microsoft.com/fwlink/?LinkID=271226 をご覧ください public void Ribbon_Load(Office.IRibbonUI ribbonUI) { System.Diagnostics.Debug.WriteLine("Load:" + ribbonUI.ToString()); this.ribbon = ribbonUI; } #endregion #region リボン内のボタンの表示非表示 public bool Button1_GetVisible(Office.IRibbonControl control) { return this.Button1Visible; } public bool Button1Visible { get { return _Button1Visible; } set { _Button1Visible = value; this.ribbon.InvalidateControl("button1"); } } private bool _Button1Visible = false; public void Button1_OnAction(Office.IRibbonControl control) { System.Windows.Forms.MessageBox.Show("CLICK"); } /// <summary>タブをアクティブにする</summary> public void Activate() { this.ribbon.ActivateTabMso("TabAddIns"); } #endregion #region ヘルパー private static string GetResourceText(string resourceName) { Assembly asm = Assembly.GetExecutingAssembly(); string[] resourceNames = asm.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; ++i) { if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0) { using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i]))) { if (resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Outlook = Microsoft.Office.Interop.Outlook; using Office = Microsoft.Office.Core; namespace OutlookAddIn1 { public partial class ThisAddIn { Ribbon1 ribbon1; Outlook.MailItem mailItem; Outlook.Explorer exp; private void ThisAddIn_Startup(object sender, System.EventArgs e) { exp= Application.ActiveExplorer(); if(exp != null) { exp.InlineResponse += Exp_InlineResponse; exp.InlineResponseClose += Exp_InlineResponseClose; } } private void Exp_InlineResponse(object Item) { this.mailItem= Item as Outlook.MailItem; this.ribbon1.Button1Visible=true; this.ribbon1.Activate(); } private void Exp_InlineResponseClose() { this.mailItem=null; this.ribbon1.Button1Visible = false; } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { //注: Outlook はこのイベントを発行しなくなりました。Outlook が // を Outlook のシャットダウン時に実行する必要があります。https://go.microsoft.com/fwlink/?LinkId=506785 をご覧ください } protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() { this.ribbon1=new Ribbon1(); return this.ribbon1; } #region VSTO で生成されたコード /// <summary> /// デザイナーのサポートに必要なメソッドです。 /// このメソッドの内容をコード エディターで変更しないでください。 /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion } }
#office 365 pro plus 16.0.11727.20222で問題なく動作
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク 美味しい魚屋さん 2019年8月2日 12:02
すべての返信
-
「アドインの表示」だとか「アドインをクリック」というのが「リボンのボタンなどの表示」「リボンのボタンのクリック」という意味だとして。
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load"> <ribbon> <tabs> <tab idMso="TabAddIns"> <group id="MyGroup" label="My Group"> <button label="BUTTON1" id="button1" getVisible="Button1_GetVisible" onAction="Button1_OnAction"/> <button label="BUTTON2" id="button2" /> </group> </tab> </tabs> </ribbon> </customUI>
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using Office = Microsoft.Office.Core; namespace OutlookAddIn1 { [ComVisible(true)] public class Ribbon1 : Office.IRibbonExtensibility { private Office.IRibbonUI ribbon; public Ribbon1() { System.Diagnostics.Debug.WriteLine("Ribbon1()"); } #region IRibbonExtensibility のメンバー public string GetCustomUI(string ribbonID) { System.Diagnostics.Debug.WriteLine("GetCustomUI:" + ribbonID); return GetResourceText("OutlookAddIn1.Ribbon1.xml"); } #endregion #region リボンのコールバック //ここでコールバック メソッドを作成します。コールバック メソッドの追加について詳しくは https://go.microsoft.com/fwlink/?LinkID=271226 をご覧ください public void Ribbon_Load(Office.IRibbonUI ribbonUI) { System.Diagnostics.Debug.WriteLine("Load:" + ribbonUI.ToString()); this.ribbon = ribbonUI; } #endregion #region リボン内のボタンの表示非表示 public bool Button1_GetVisible(Office.IRibbonControl control) { return this.Button1Visible; } public bool Button1Visible { get { return _Button1Visible; } set { _Button1Visible = value; this.ribbon.InvalidateControl("button1"); } } private bool _Button1Visible = false; public void Button1_OnAction(Office.IRibbonControl control) { System.Windows.Forms.MessageBox.Show("CLICK"); } /// <summary>タブをアクティブにする</summary> public void Activate() { this.ribbon.ActivateTabMso("TabAddIns"); } #endregion #region ヘルパー private static string GetResourceText(string resourceName) { Assembly asm = Assembly.GetExecutingAssembly(); string[] resourceNames = asm.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; ++i) { if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0) { using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i]))) { if (resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Outlook = Microsoft.Office.Interop.Outlook; using Office = Microsoft.Office.Core; namespace OutlookAddIn1 { public partial class ThisAddIn { Ribbon1 ribbon1; Outlook.MailItem mailItem; Outlook.Explorer exp; private void ThisAddIn_Startup(object sender, System.EventArgs e) { exp= Application.ActiveExplorer(); if(exp != null) { exp.InlineResponse += Exp_InlineResponse; exp.InlineResponseClose += Exp_InlineResponseClose; } } private void Exp_InlineResponse(object Item) { this.mailItem= Item as Outlook.MailItem; this.ribbon1.Button1Visible=true; this.ribbon1.Activate(); } private void Exp_InlineResponseClose() { this.mailItem=null; this.ribbon1.Button1Visible = false; } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { //注: Outlook はこのイベントを発行しなくなりました。Outlook が // を Outlook のシャットダウン時に実行する必要があります。https://go.microsoft.com/fwlink/?LinkId=506785 をご覧ください } protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() { this.ribbon1=new Ribbon1(); return this.ribbon1; } #region VSTO で生成されたコード /// <summary> /// デザイナーのサポートに必要なメソッドです。 /// このメソッドの内容をコード エディターで変更しないでください。 /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion } }
#office 365 pro plus 16.0.11727.20222で問題なく動作
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク 美味しい魚屋さん 2019年8月2日 12:02