Hello,
I'm looking to call an excel macro (excel version 07) within a separate visual studio application (visual studio express 2010). Basically, a user clicks a browse dialog button to select a xls or xlsm file which is then added to a listbox. Next, I would like the macro to run when another button is clicked. The macro looks at the first column in excel and moves the listed .tif images from one folder to another. I referenced the excel libraries, but seem to be getting hung up on a few code statements (''''ERROR see code below). I would like to run the macro within visual studio for any selected macro or add the macro somehow add the macro to the selected excel file and run it. Thanks for your help!
'Here is the button click procedure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
filenames = lstFiles.SelectedItem
Dim oExcelApp As Object
'oExcelApp = filenames
' Create a reference to the currently running excel application
oExcelApp = GetObject(filenames)
' Make the Excel Application Visible.
oExcelApp.Visible = True
' Run the excel procedure/macro "MoveFiles"
oExcelApp.run("MoveFiles")
oExcelApp.quit()
End Sub
'Here is the excel macro
Private Function movefiles()
Dim strDirectory As String
Dim strDestFolder As String
Dim strExt As String
strDirectory = "C:\Temp\Logs"
strDestFolder = "C:\Temp\Logs\Temp2"
strExt = "xls"
Dim myfilesystemobject As Object
Dim myfiles As Object
Dim myfile As Object
Dim rng As Excel.Range
Dim oexcel As Excel.Workbook
rng = oexcel.ActiveSheet.Range("A1:A3000") 'set this to the range of your filtered list
myfilesystemobject = CreateObject("Scripting.FileSystemObject")
myfiles = myfilesystemobject.GetFolder(strDirectory).Files
For Each cell In rnd ''''''ERROR
For Each myfile In myfiles
If Not IsNull(cell.Value) Then
If myfile = strDirectory & "\" & cell.Value Then
'Stop
With myfile
.copy(strDestFolder & "\" & myfile.Name)
End With
Else
End If
End If
Next myfile
Next cell
End Function
I'm looking to call an excel macro (excel version 07) within a separate visual studio application (visual studio express 2010). Basically, a user clicks a browse dialog button to select a xls or xlsm file which is then added to a listbox. Next, I would like the macro to run when another button is clicked. The macro looks at the first column in excel and moves the listed .tif images from one folder to another. I referenced the excel libraries, but seem to be getting hung up on a few code statements (''''ERROR see code below). I would like to run the macro within visual studio for any selected macro or add the macro somehow add the macro to the selected excel file and run it. Thanks for your help!
'Here is the button click procedure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
filenames = lstFiles.SelectedItem
Dim oExcelApp As Object
'oExcelApp = filenames
' Create a reference to the currently running excel application
oExcelApp = GetObject(filenames)
' Make the Excel Application Visible.
oExcelApp.Visible = True
' Run the excel procedure/macro "MoveFiles"
oExcelApp.run("MoveFiles")
oExcelApp.quit()
End Sub
'Here is the excel macro
Private Function movefiles()
Dim strDirectory As String
Dim strDestFolder As String
Dim strExt As String
strDirectory = "C:\Temp\Logs"
strDestFolder = "C:\Temp\Logs\Temp2"
strExt = "xls"
Dim myfilesystemobject As Object
Dim myfiles As Object
Dim myfile As Object
Dim rng As Excel.Range
Dim oexcel As Excel.Workbook
rng = oexcel.ActiveSheet.Range("A1:A3000") 'set this to the range of your filtered list
myfilesystemobject = CreateObject("Scripting.FileSystemObject")
myfiles = myfilesystemobject.GetFolder(strDirectory).Files
For Each cell In rnd ''''''ERROR
For Each myfile In myfiles
If Not IsNull(cell.Value) Then
If myfile = strDirectory & "\" & cell.Value Then
'Stop
With myfile
.copy(strDestFolder & "\" & myfile.Name)
End With
Else
End If
End If
Next myfile
Next cell
End Function