ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 900|回复: 2

[讨论] VBA 调用VSTO 外接程序中类方法代码

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-12-6 16:16 | 显示全部楼层 |阅读模式
通过复制了该链接

https://docs.microsoft.com/zh-cn ... est-the-vsto-add-in
可以操作Excel工作表和显示窗体控件,不明觉厉。

类:AddInUtilities.cs中的代码
  1. using System.Data;
  2. using System.Runtime.InteropServices;
  3. using Excel = Microsoft.Office.Interop.Excel;
  4. using System.Windows.Forms;

  5. namespace ExcelImportData
  6. {

  7.         [ComVisible(true)]
  8.         public interface IAddInUtilities
  9.         {
  10.             void ImportData();
  11.             void showform();
  12.         }

  13.         [ComVisible(true)]
  14.         [ClassInterface(ClassInterfaceType.None)]
  15.         public class AddInUtilities : IAddInUtilities
  16.         {
  17.             public void ImportData()
  18.             {
  19.                 Excel.Worksheet activeWorksheet = Globals.ThisAddIn.Application.ActiveSheet as Excel.Worksheet;

  20.                 if (activeWorksheet != null)
  21.                 {
  22.                     Excel.Range range1 = activeWorksheet.get_Range("A1", System.Type.Missing);
  23.                     range1.Value2 = "This is my data";
  24.                 }
  25.             }

  26.             public void showform()
  27.             {
  28.                 MessageBox.Show("显示窗体", "提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  29.                 NewForm f = new NewForm();
  30.                 f.Show();
  31.             }
  32.         }
  33.     }
复制代码
ThisAddIn.cs中的代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using Excel = Microsoft.Office.Interop.Excel;
  7. using Office = Microsoft.Office.Core;
  8. using Microsoft.Office.Tools.Excel;

  9. namespace ExcelImportData
  10. {

  11.     public partial class ThisAddIn
  12.     {
  13.         private AddInUtilities utilities;

  14.         protected override object RequestComAddInAutomationService()
  15.         {
  16.             if (utilities == null)
  17.                 utilities = new AddInUtilities();

  18.             return utilities;
  19.         }
  20.         private void ThisAddIn_Startup(object sender, System.EventArgs e)
  21.         {
  22.         }

  23.         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
  24.         {
  25.         }

  26.         #region VSTO 生成的代码

  27.         /// <summary>
  28.         /// 设计器支持所需的方法 - 不要修改
  29.         /// 使用代码编辑器修改此方法的内容。
  30.         /// </summary>
  31.         private void InternalStartup()
  32.         {
  33.             this.Startup += new System.EventHandler(ThisAddIn_Startup);
  34.             this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
  35.         }
  36.         
  37.         #endregion
  38.     }
  39. }
复制代码
VBA中两个调用代码:
  1. Private Sub CommandButton1_Click()
  2.     Dim addIn As COMAddIn
  3.     Dim automationObject As Object
  4.     Set addIn = Application.COMAddIns("ExcelImportData")
  5.     Set automationObject = addIn.Object
  6.     automationObject.ImportData
  7. End Sub

  8. Private Sub CommandButton2_Click()
  9.     Dim addIn As COMAddIn
  10.     Dim automationObject As Object
  11.     Set addIn = Application.COMAddIns("ExcelImportData")
  12.     Set automationObject = addIn.Object
  13.     automationObject.showform
  14. End Sub
复制代码


VBA调用C#类方法

VBA调用C#类方法

TA的精华主题

TA的得分主题

发表于 2018-12-6 23:49 来自手机 | 显示全部楼层
直接.Net类库接口就行,哪里还需要外家vsto

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-18 10:06 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-25 22:36 , Processed in 0.035483 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表