-
Notifications
You must be signed in to change notification settings - Fork 1
/
VbaHelper.cs
57 lines (46 loc) · 1.9 KB
/
VbaHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iDiTect.Excel.Demo
{
public static class VbaHelper
{
public static void AddVba()
{
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("VbaSheet");
//Create a vba project
excelApp.Workbook.CreateVBAProject();
//Add vba code
//Simplely way to bind vba code
var sb = new StringBuilder();
sb.AppendLine("Private Sub Workbook_Open()");
sb.AppendLine("MsgBox \"VBA Test!\"");
sb.AppendLine("End Sub");
excelApp.Workbook.CodeModule.Code = sb.ToString();
excelApp.Save("vba.xlsm");
}
public static void AddVba2()
{
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("VbaSheet");
//Create a vba project
excelApp.Workbook.CreateVBAProject();
//More customized way to bind vba code
//Add a rectangle shape
worksheet.Drawings.AddShape("VbaRect", ShapeStyle.Rect);
var sb = new StringBuilder();
sb.AppendLine("Public Sub AddTextToRect()");
sb.AppendLine("[VbaSheet].Shapes(\"VbaRect\").TextEffect.Text = \"Vba sample!\"");
sb.AppendLine("End Sub");
//Create a new module and set the code
var module = excelApp.Workbook.VbaProject.Modules.AddModule("VbaTest");
module.Code = sb.ToString();
//Call the newly created sub from the workbook open event
excelApp.Workbook.CodeModule.Code = "Private Sub Workbook_Open()\r\nAddTextToRect\r\nEnd Sub";
excelApp.Save("vba.xlsm");
}
}
}