I don't know why I can't call my macro code from R, This is my code that I'm trying to save as vbs file: (should I save it in Notepad application?)
Sub vb()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("path.xlsm", 0, False)
xlApp.Visible = True
xlApp.Run "Countries"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
How can I save the above code as vbs ?
You can export a code module from the project explorer, right-click on it and select Export File....
You can also do it with VBA, i.e. to export "Module1":
With ThisWorkbook.VBProject.VBComponents("Module1")
.Export "c:\so\" & .Name & ".bas"
End With
Use the below VBS code as you cannot save VBA code as VBS as the schema or say they are not in the same page as C and C++ are different it is in the same way
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("path.xlsm")
objExcel.Application.Visible = True
objExcel.Application.Run "path.xlsm!Countries" 'Refer to the below if the code is under sheet
objExcel.ActiveWorkbook.Close
WScript.Echo "Finished."
WScript.Quit
If you have placed the code in the sheet. user this line
objExcel.Application.Run "path.xlsm!sheet1.dog"
Hope this resolve your query. Happy Coding.
Related
I have a macro that I'm trying to run through Access that will open up an excel sheet, do some actions on it, and then leave the sheet open.
I have most of it working, with the exception of not being able to get my excel document to open visibly. If I check the task manager, an excel process is running in the background so something does happen, just nothing that I can physically see.
I've attempted to sample some code found through stackoverflow and other resources, which I'm sure you'll see some of that in my current code. But I tried for about an hour with no avail.
Private Sub Command1_Click()
Dim fd As FileDialog
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
On Error GoTo ErrorHandler
'allowing selection of the time
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
If fd.Show = True Then
If fd.SelectedItems(1) <> vbNullString Then
MySheetPath = fd.SelectedItems(1)
End If
Else
End
End If
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
ShowaWindow (MySheetPath)
Set XlSheet = XlBook.Worksheets(1)
XlSheet.Rows(2).EntireRow.Insert
XlSheet.Range("D2") = "ABC"
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
Exit Sub
ErrorHandler:
Set fd = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
End Sub
Sub ShowaWindow(sFileName As String)
Dim oWb As Workbook
Set oWb = GetObject(sFileName)
For Each oWb In Workbooks
If LCase(oWb.Name) <> LCase(sFileName) Then
oWb.Windows(1).Visible = True
Exit For
End If
Next
End Sub
Ideally I would like to be able to see the worksheet appear.
Set Xl = CreateObject("Excel.Application")
Xl.Visible=True
You don't need to put it immediately after creating the object, just before you set the object to Nothing.
Set XlBook = GetObject(MySheetPath)
This is wrong. Don't use GetObject to open the workbook, use the Excel.Application instance you just created:
Set XlBook = Xl.Workbooks.Open(MySheetPath)
Later you iterate all opened workbooks:
For Each oWb In Workbooks
But that's not the Workbooks collection from the Xl application instance, it's the Workbooks collection from the instance that's currently running your code - you need to qualify it with the Xl object:
Private Sub ShowaWindow(ByVal app As Excel.Application, ByVal sFileName As String)
'...
For Each oWb In app.Workbooks
Also, make the app instance visible after you created it, and don't forget to invoke XlBook.Close and Xl.Quit to properly tear down that EXCEL.EXE process when you're done.
I would like to open an excel file that is saved with a password for modify with VBScript. My current code VBS code is below, which works, but it keeps popping up with boax asking for a password. How can i open the excel spreadsheet with excel prompting me for a password?
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\jasons\Documents\TestFile.xlsm",,,,"yep123")
xlApp.Visible = True
xlApp.Run "Refresh_data_ss"
xlApp.Save
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Now I see, there is a password for modify on your file but not for open. Open password is the fifth parameter to Workbooks.Open, and modify is the sixth. SO you need to have the following instead (one more comma):
Set xlBook = xlApp.Workbooks.Open("C:\Users\jasons\Documents\TestFile.xlsm",,,,,"yep123")
You can get the password from an InputBox
myPass = InputBox("Write the password: ")
Set xlBook = xlApp.Workbooks.Open("C:\Users\jasons\Documents\TestFile.xlsm",,,, myPass)
I have a macro that I need to be run every 5 min. I have a vbs file that schedules the macro.
The macro is checking for new files in some folder, writes their info into a table, and moving the files into archive.
The table is in the same excel file as the macro!
It is running the macro fine but in the end, its asking me if I wont to save the file.
I need it to save the changes that the macro did to the file automatically!
this is my current vbs code:
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open("W:\Excel Macro\EIM File Maneger\EIM_file_check.xlsm", 0, True)
xlApp.Run "GetFiles"
xlBook.Close true
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
Updated code below, I have also tweaked the logic of the clean-up
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open("W:\Excel Macro\EIM File Maneger\EIM_file_check.xlsm", 0, True)
xlApp.Run "GetFiles"
xlbook.Save
xlBook.Close False
set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
There is a way to convert VBA code to a script, so it can be executed without running it from an EXCEL macro?
For example this code below, I want to run it without the need of opening Excel file with the macro that contains it:
Sub XML_FILE()
Dim filename
MsgBox "Open File"
filename = Application.GetOpenFilename
If filename <> False Then
Workbooks.Open (filename)
MsgBox "Save File"
ActiveWorkbook.SaveAs filename:=filename, FileFormat:=xlOpenXMLWorkbook
End If
End Sub
What are the steps to make this and other macros to run as scripts?
Try this 1. Open notepad file and paste below code:
Dim objExcel, objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Folder\example.xlsm")
objExcel.Visible = False
objExcel.Run "XML_FILE"
objWorkbook.Close
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Echo "Finished."
WScript.Quit
Explanation
1.change objExcel.Run "Report" with your macro function name.
Then save the file as test.vbs then run the file it will run without opening the excel file.
I want to be able to run a macro in a Word document that will create an Excel document and then save that spreadsheet in a shared folder.
This is the code I have so far:
Public Sub Monthly_Commission_Extract()
Dim objExcel
Dim objDoc
Dim objSelection
Dim SaveAs1 As String
SaveAs1 = ("\\stnlinasshd01\P403759\Month End\Monthly Commission Extract\1st Save")
Set objExcel = CreateObject("Excel.Application")
Set objDoc = objExcel.Workbooks.Add
objExcel.Visible = True
Set objSelection = objExcel.Selection
ActiveWorkbook.SaveAs FileName:=SaveAs1, FileFormat:=-4158, CreateBackup:=False
Application.DisplayAlerts = True
End Sub
The code is giving me an error:
Run-time error '424': Object required
At the following piece of code:
ActiveWorkbook.SaveAs FileName:=SaveAs1, FileFormat:=xlText, CreateBackup:=False
Please advise how I get around this.
objExcel.ActiveWorkbook.SaveAs
not just
ActiveWorkbook.SaveAs
Anything which "belongs" to Excel must be prefixed with your objExcel application reference.
Below is a simple and straight forward code that creates a new excel workbook and saves it into a path on your local disk. This VBA code has ben tested from inside MS Word.
Private Sub CreateExcel()
Dim myExcel As Object
Dim myWb As Object
Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add
Application.DisplayAlerts = False
myWb.SaveAs FileName:="D:\test\dump.xls"
Application.DisplayAlerts = True
myWb.Close False
Set myWb = Nothing
myExcel.Quit
Set myExcel = Nothing
End Sub
In your case, the following would have worked because 'objDoc' is derived from the excel object reference.
objDoc.SaveAs FileName:=SaveAs1, FileFormat:=-4158, CreateBackup:=False
Oh captain, yes my captain,
the errormessage says clearly that a object is missed, I think this line
SaveAs1 = ("\stnlinasshd01\P403759\Month End\Monthly Commission Extract\1st Save")
is the problem You should a SET-Statement out of this, by starting this line with "SET ", then you have the object required.