using System.Runtime.InteropServices;
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using IRibbonControl = ExcelDna.Integration.CustomUI.IRibbonControl;
using IRibbonUI = ExcelDna.Integration.CustomUI.IRibbonUI;
namespace TradeService
{
[ComVisible(true)]
public class MyRibbon : ExcelRibbon
{
private static IRibbonUI _ribbonUi;
private Workbook ws;
public void Ribbon_Load(IRibbonUI sender)
{
var xlApp = ExcelDnaUtil.Application as Application;
ws = xlApp?.ActiveWorkbook;
sender.Invalidate();
_ribbonUi = sender;
}
public string GetEditBoxText(IRibbonControl control1)
{
return GetDocumentProperty(control1.Id);
}
public void EditBoxTextChanged(IRibbonControl control1, string text)
{
switch (control1.Id)
{
case "envComboBox":
var config = new Config();
SetDocumentProperty("url", config[text]);
_ribbonUi?.InvalidateControl("url");
break;
}
SetDocumentProperty(control1.Id, text);
}
public void SetDocumentProperty(string propertyName, string propertyValue)
{
var props = ws.CustomDocumentProperties;
if (!ContainsKey(propertyName))
{
props.Add(propertyName, false, MsoDocProperties.msoPropertyTypeString, propertyValue);
}
else
{
var property = props[propertyName];
property.Value = propertyValue;
}
}
public bool ContainsKey(string key)
{
var props = ws.CustomDocumentProperties;
foreach (var property in props)
if (property.Name == key)
return true;
return false;
}
public string GetDocumentProperty(string propertyName,
MsoDocProperties type = MsoDocProperties.msoPropertyTypeString)
{
if (!ContainsKey(propertyName))
return string.Empty;
var props = ws.CustomDocumentProperties;
var value = props[propertyName];
return value.Value;
}
}
}