I use about a dozen different Excel files in one of my projects. Each time the program runs it opens up one of the files, writes to and reads from it then closes it down. One of these old Excel files has a macro which must be run each time in order to get the calculations needed on one of the sheets. I'm using this same file in a process like this, the Excel file is opened, data input, the macro runs and then shuts down fine.
So one of the web developers here had an idea that instead of running Excel each time, why don't we run every possible combination through it and collect the answers into a database which can be read from. OK, cool so I write some VB.NET code to do it. It works fine on the spreadsheet which do not contain macros. But the one which I'm getting trouble from has a macro...I see it, I can run it manually, I can debug and test it inside of VBA, etc.... the darn thing is there. But when I run my code which is basically a nested loop which goes through all the combinations of possibly entries where I get the error message that the Macro Cannot be Found. Doing searches on this comes up with a bunch of unrelated issues.
The Excel program on this workstation has no Add-Ins enabled, Macro Security is low and VB Apps are trusted. The macro runs fine manually. I even stepped through the code and it worked...but then I tried it again at full speed and stepping through it again and it fails, saying it cannot find the macro. I put a 2 second sleep in it and no good. And a 2 second sleep is going to be a drag because this thing will write 133,124 records to a file. And stopping to pause means unlike it's brother Excel file without a macro only takes about 10 minutes, this run will take about 9 hours.
So, am I just trying to ram to much garbage down the sink or is there a possible work around for this?
So one of the web developers here had an idea that instead of running Excel each time, why don't we run every possible combination through it and collect the answers into a database which can be read from. OK, cool so I write some VB.NET code to do it. It works fine on the spreadsheet which do not contain macros. But the one which I'm getting trouble from has a macro...I see it, I can run it manually, I can debug and test it inside of VBA, etc.... the darn thing is there. But when I run my code which is basically a nested loop which goes through all the combinations of possibly entries where I get the error message that the Macro Cannot be Found. Doing searches on this comes up with a bunch of unrelated issues.
The Excel program on this workstation has no Add-Ins enabled, Macro Security is low and VB Apps are trusted. The macro runs fine manually. I even stepped through the code and it worked...but then I tried it again at full speed and stepping through it again and it fails, saying it cannot find the macro. I put a 2 second sleep in it and no good. And a 2 second sleep is going to be a drag because this thing will write 133,124 records to a file. And stopping to pause means unlike it's brother Excel file without a macro only takes about 10 minutes, this run will take about 9 hours.
So, am I just trying to ram to much garbage down the sink or is there a possible work around for this?
Code:
Sub Main()
'************************************************************************************************************
' Open Excel and Populate Cells with data from text file
'************************************************************************************************************
Dim vExcelApp As Excel.Application = Nothing
Dim vExcelWorkbooks As Excel.Workbooks = Nothing
Dim vExcelWorkbook1 As Excel.Workbook = Nothing
Dim vExcelWorkbook2 As Excel.Workbook = Nothing
Dim vExcelSheet1 As Excel.Worksheet = Nothing
Dim vExcelSheet2 As Excel.Worksheet = Nothing
Dim vExcelRange1 As Excel.Range = Nothing
Dim vExcelSheet3 As Excel.Worksheet = Nothing
vExcelApp = New Excel.Application
vExcelApp.DisplayAlerts = False
vExcelApp.Visible = False
.
. MORE EXCEL STUFF IN HERE
.
.
'***********************************************************************************************************
' FIRST RESET ALL THE FIELDS IN THE EXCEL FILE JUST IN CASE
'***********************************************************************************************************
vExcelRange1 = vExcelSheet1.Range("D5,D6,F5,F6,F8,F10,F35,F45")
vExcelRange1.Value = ""
vExcelRange1 = vExcelSheet1.Range("F11:F21,F23,F25:F29,F31:F32,F34,F37:F40,F42,I17")
vExcelRange1.Value = "N"
vExcelRange1 = vExcelSheet1.Range("F41,F43")
vExcelRange1.Value = "1"
vExcelSheet1.Range("F8").Value = "300"
' POPULATE EXCEL FILE
' FOR APS & APD RETROFIT
vExcelSheet1.Range("F21").Value = "N"
' FOR APD HSS
vExcelSheet1.Range("F11").Value = "N"
Dim count As Integer = 1
For i As Double = 12 To 193
For j As Double = 12 To 193
count = count + 1
vExcelSheet1.Range("D5").Value = i
vExcelSheet1.Range("D6").Value = j
'**********************************************************************************************************
' APD BOM SPREADSHEET REQUIRES THE MACRO BE RUN IN ORDER TO COMPLETE CALCS
'**********************************************************************************************************
vExcelApp.Run("RunCalcs")
vExcelSheet3.Range("M" & count.ToString).Value = "APD"
vExcelSheet3.Range("N" & count.ToString).Value = i
vExcelSheet3.Range("O" & count.ToString).Value = j
vExcelSheet3.Range("P" & count.ToString).Value = vExcelSheet1.Range("K6").Value
vExcelSheet3.Range("Q" & count.ToString).Value = vExcelSheet1.Range("M6").Value
Console.WriteLine(count)
Next
Next