Created
September 6, 2015 10:27
-
-
Save angelovstanton/81dc39620bf089f73517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static string GetMacro(int macroId, int row, int endCol) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
string range = "ActiveSheet.Range(Cells(" + row + "," + 3 + | |
"), Cells(" + row + "," + (endCol + 3) + ")).Select"; | |
sb.AppendLine("Sub Macro" + macroId + "()"); | |
sb.AppendLine("On Error Resume Next"); | |
sb.AppendLine(range); | |
sb.AppendLine("ActiveSheet.Shapes.AddChart.Select"); | |
sb.AppendLine("ActiveChart.ChartType = xlLine"); | |
sb.AppendLine("ActiveChart.SetSourceData Source:=" + range); | |
sb.AppendLine("On Error GoTo 0"); | |
sb.AppendLine("End Sub"); | |
return sb.ToString(); | |
} | |
private static void AddChartButton(MSExcel.Workbook workBook, MSExcel.Worksheet xlWorkSheetNew, | |
MSExcel.Range currentRange, int macroId, int currentRow, int endCol, string buttonImagePath) | |
{ | |
MSExcel.Range cell = currentRange.Next; | |
var width = cell.Width; | |
var height = 15; | |
var left = cell.Left; | |
var top = Math.Max(cell.Top + cell.Height - height, 0); | |
MSExcel.Shape button = xlWorkSheetNew.Shapes.AddPicture(@buttonImagePath, MsoTriState.msoFalse, | |
MsoTriState.msoCTrue, left, top, width, height); | |
VBIDE.VBComponent module = workBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule); | |
module.CodeModule.AddFromString(GetMacro(macroId, currentRow, endCol)); | |
button.OnAction = "Macro" + macroId; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment