工作表的控制方法不可用
control method of worksheet is not available
我在为 excel 编程加载项时遇到了一个奇怪的问题;
我想在 excel 中添加控件,我使用这段代码启发 https://msdn.microsoft.com/en-us/library/cc442817.aspx
private void button_Click(object sender, RibbonControlEventArgs e)
{
var worksheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
{
const string buttonName = "MyButton";
if (((RibbonCheckBox)sender).Checked)
{
var selection = Globals.ThisAddIn.Application.Selection as Range;
if (selection != null)
{
var button =
new Microsoft.Office.Tools.Excel.Controls.Button();
worksheet.Controls.AddControl(button, selection, buttonName);
}
}
else
{
worksheet.Controls.Remove(buttonName);
}
}
}
但工作表的控制方法不可用,我无法访问它,我添加了 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 程序集和以下语句
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Ribbon;
using Office = Microsoft.Office.Core;
顺便说一下,我使用的是 office 2007 和 vs 2013,所以我在 DebugInfoExeName 中将 office 版本更改为 12。
您需要使用GetVstoObject方法在工作簿中获取一个代表该工作表的宿主项,然后在当前选中的单元格中添加一个Button控件。
private void Button_Click(object sender, RibbonControlEventArgs e)
{
Worksheet worksheet = Globals.Factory.GetVstoObject(
Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1]);
string buttonName = "MyButton";
if (((RibbonCheckBox)sender).Checked)
{
Excel.Range selection = Globals.ThisAddIn.Application.Selection as Excel.Range;
if (selection != null)
{
Microsoft.Office.Tools.Excel.Controls.Button button =
new Microsoft.Office.Tools.Excel.Controls.Button();
worksheet.Controls.AddControl(button, selection, buttonName);
}
}
else
{
worksheet.Controls.Remove(buttonName);
}
}
使用正确的命名空间解决了我的类似问题:
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;
我在为 excel 编程加载项时遇到了一个奇怪的问题; 我想在 excel 中添加控件,我使用这段代码启发 https://msdn.microsoft.com/en-us/library/cc442817.aspx
private void button_Click(object sender, RibbonControlEventArgs e)
{
var worksheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
{
const string buttonName = "MyButton";
if (((RibbonCheckBox)sender).Checked)
{
var selection = Globals.ThisAddIn.Application.Selection as Range;
if (selection != null)
{
var button =
new Microsoft.Office.Tools.Excel.Controls.Button();
worksheet.Controls.AddControl(button, selection, buttonName);
}
}
else
{
worksheet.Controls.Remove(buttonName);
}
}
}
但工作表的控制方法不可用,我无法访问它,我添加了 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 程序集和以下语句
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Ribbon;
using Office = Microsoft.Office.Core;
顺便说一下,我使用的是 office 2007 和 vs 2013,所以我在 DebugInfoExeName 中将 office 版本更改为 12。
您需要使用GetVstoObject方法在工作簿中获取一个代表该工作表的宿主项,然后在当前选中的单元格中添加一个Button控件。
private void Button_Click(object sender, RibbonControlEventArgs e)
{
Worksheet worksheet = Globals.Factory.GetVstoObject(
Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1]);
string buttonName = "MyButton";
if (((RibbonCheckBox)sender).Checked)
{
Excel.Range selection = Globals.ThisAddIn.Application.Selection as Excel.Range;
if (selection != null)
{
Microsoft.Office.Tools.Excel.Controls.Button button =
new Microsoft.Office.Tools.Excel.Controls.Button();
worksheet.Controls.AddControl(button, selection, buttonName);
}
}
else
{
worksheet.Controls.Remove(buttonName);
}
}
使用正确的命名空间解决了我的类似问题:
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;