My problem is quite simple but I haven't found a solution so far...
I created a form in Access called Form1.
In this form, I inserted an "Unbound Object Frame", which is a new macro-enabled Excel worksheet.
In the Excel sheet, I create a macro we can call "ExcelMacro".
I want to know how to run macros in this Excel sheet from Access, e.g. in my Access macro, run "ExcelMacro"
I do NOT want to link my Excel sheet to an external Excel workbook.
Do you have any ideas?
Thanks a lot for your help!
Edit:
In case you need some additional information:
Name of Unbound Object Frame: xlObject
OLE Class: Microsoft Excel Macro-Enabled 12
Class: Excel.SheetMacroEnabled.12
Sheet name: Sheet1
You can use the following in Access:
Public Sub RunExcelMacro()
Dim excelApp As Object
Set excelApp = GetObject(, "Excel.Application")
excelApp.Run "HelloWorld"
End Sub
Where "HelloWorld" is the name of the Excel Macro.
GetObject(, "Excel.Application") gets the latest opened Excel application. This needs to be the Excel application that is running your embedded worksheet. If it's another, it will fail.
Also, the worksheet needs to be open, else it will fail (you can add either of the following code segments to open it before running this).
Me.MyOLEUnbound.Verb = 0 'vbOLEPrimary
Me.MyOLEUnbound.Action = 7
or
Me.MyOLEUnbound.AutoActivate = 1 'vbOLEActivateGetFocus
Me.MyOLEUnbound.SetFocus
To make sure there are no other instances of Excel running, and possibly quit them if they are (note: these make use of runtime errors and error handlers)
Public Function IsExcelRunning() As Boolean
IsExcelRunning = True
On Error GoTo ReturnFalse:
Dim obj As Object
Set obj = GetObject(, "Excel.Application")
Exit Function
ReturnFalse:
IsExcelRunning = False
End Function
Public Sub CloseAllExcel()
Dim obj As Object
On Error GoTo ExitSub
Dim i As Integer
'There shouldn't be more than 10000 running Excel applications
'Can use While True too, but small risk of infinite loop
For i = 0 To 10000
Set obj = GetObject(, "Excel.Application")
obj.Quit
Next i
ExitSub:
End Sub
Related
When I have don't have the excel APP open, the following error is thrown :
ActiveX Component can't create object
Steps to reproduce issue :
1 Open Outlook, ALT + F11 And Insert the following sub :
Sub Test()
Dim myXL As New Excel.Application
Set myXL = GetObject(, "Excel.Application")
Set wb = myXL.Workbooks.Open("MyPath\MyXL.xlsx")
End Sub
Close ALL your excel files
Run the sub Test from outlook.
The Error will be thrown on :
Set myXL = GetObject(, "Excel.Application")
How can I avoid this error ?
A better option should be the next way, I think:
Dim objexcel As Object
On Error Resume Next 'firstly, try catching the existing open session, if any:
Set objexcel = GetObject(, "Excel.Application")
If err.Number <> 0 Then 'if no any existing session, create a new one:
err.Clear: Set objexcel = CreateObject("Excel.Application")
End If
On Error GoTo 0
Having a reference to 'Microsoft Excel ... Object library` you can declare
Dim objexcel As Excel.Application
and benefit of the intellisense suggestions...
It is also possible to find an Excel open session if you know the full name of a specific workbook open in it:
Set objExcel = GetObject(ThisWorkbook.fullName).Application
Debug.Print objExcel.hwnd
Or even for a new workbook, open by a third party application, in a new session, as "Book1":
Set objExcel = GetObject("Book1").Application
Debug.Print objExcel.hwnd
If the respective application drops new workbooks (and opens them in the same session), naming them as "Book2", "Book3" and so on, a loop building the workbook name bay concatenation of "Book" root with the incremented variable can be used to get it.
I am trying to run an excel macro from access 2016. This has a lot of examples out their however they are all old. The exact error code I receive is run-time error '1004':
cannot run the macro "TestScript()". The macro may not be available in this workbook or all macros may be displayed. I am only running something easy for the test. I have ensured that macros is enabled in excel. the excel code is as follows
Public Sub TestScript()
MsgBox "IT WORKED"
End Sub
Real simple for the test. Access is opening the excel spreadsheet however it stops there with an error code.
My Access code is very simple and is below. I have also noted where the code stops. While I am new at VBA I have done a lot of research in this. I am testing as simple code as I could figure out. Any help would be welcomed.
Option Compare Database
Function runExcelmacro()
Dim XL As Object
Set XL = CreateObject("Excel.Application")
With XL
'Turn Off warnings
.Visible = False
.displayalerts = False
'WorkBook path such as "C:\Computer\WorkBook.xlsx"
.Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
'Run the Macro in excel getworkbook
.Run TestScript 'Code stops here!
'Close Workbook
.ActiveWorkbook.Close (True)
.Quite
End With
Set XL = Nothing
End Function
Public Sub runMacroSub()
Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub
I guess OP did not put the code of Testscript in an extra module. Instead the code was put into the class module of the workbook or a worksheet. In the latter case you have to add the workbook or worksheet name in front of the name of the script.
Either it is `
.Run "ThisWorkbook.TestScript"
or in case it is in Sheet1 (codename of the sheet!)
.Run "Sheet1.TestScript"
Do not forget the quotation marks!
PS The OP's code above is working if you put testscript into a module and add quotation marks.
.Run "TestScript"
Here is a description how to create a module and add code
Here try this:
Public Sub TestScript()
MsgBox "IT WORKED"
End Sub
Option Compare Database
Function runExcelmacro()
Dim XL As Object, wb as object
Set XL = CreateObject("Excel.Application")
With XL
.Visible = False
.displayalerts = False
set wb = .Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
wb.Run TestScript 'Code stops here!
.ActiveWorkbook.Close (True)
.Quite
End With
Set XL = Nothing
End Function
Public Sub runMacroSub()
Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub
Here i declared wb as an object and then set wb = to the workbook.
This is of course assumin your test script is in the actual workbook being opened. Would be real funny if that wasnt a thing lol
I am trying to ensure that an Excel file is not left open if a Word application that is gathering data from it via a macro suddenly crashes.
The macro handles most errors using the code below, but I want to ensure that the Excel file isn't left hanging open in the face of an unhandled event. Such an event might be the user closing the word document from the task manager while the macro is running.
Public Function GatherDataFromExcel(x) As Collection
On Error GoTo CloseExcel
Dim path_file_excel As String
path_file_excel = "\path to file\file.xlsx"
Dim objXL As Object ' Excel.Application
Dim objBook As Object ' Excel.Workbook
Set objXL = CreateObject("Excel.Application")
On Error Resume Next
Set objBook = objXL.Workbooks.Open(path_file_excel)
On Error Resume Next
'Code that gathers data and returns that data from the function
CloseExcel:
If Not objBook Is Nothing Then
objBook.ActiveWorkbook.Close
objBook.Close
Set objBook = Nothing
If Not objXL Is Nothing Then
Set objXL = Nothing
End If
End If
End Function
I have closed the word document mid execution to check, and indeed the excel file is left hanging open. Is there any way to prevent this from happening?
If Word crashes you are going to need a different process to close Excel.
A few options:
Show Excel to the user so that the user can close Excel objXL.visible = true
If your VBA error handler does run you probably want to change:
objBook.ActiveWorkbook.Close
objBook.Close
To
objBook.Close False ' Close workbook without saving changes
objXL.Quit ' Exit Excel
As Freeflow suggested, have another process (e.g. timer event in Excel or a separate VBScript process) that closes Excel if it can't find the Word document
i've got a subtle problem using VBA on MS Word. I try to refer to some workbooks that were opened before word was started up.
From within a short test-macro in word a simple
MsgBox Workbooks.Count
delievers a value of 0 although 3 (empty) workbooks are opened. When the 3 Workbooks are opened after Word was started, i get the correct value of 3.
How to fix this ?
jm2p Zeph
it's because you must get the running instance of Excel instead of creating a new one
the following code set an Excel application object trying to get any running instance first and then, should no excel session be already running, open a new one:
Option Explicit
Sub LateBindingExcel()
Dim xlApp As Object
Set xlApp = GetExcelObject
MsgBox xlApp.Workbooks.count
End Sub
Function GetExcelObject() As Object
Dim excelApp As Object
On Error Resume Next
Set excelApp = GetObject(, "Excel.Application") '<--| try getting a running Excel application
On Error GoTo 0
If excelApp Is Nothing Then Set excelApp = CreateObject("Excel.Application") '<--| if no running instance of Excel has been found then open a new one
Set GetExcelObject = excelApp '<--| return the set Excel application
End Function
Check this out:
'Option Explicit
'Option Explicit
Sub check()
Set objExcel = GetObject(, "Excel.Application")
Set wbs = objExcel.Workbooks
Debug.Print wbs.Count
Set objExcel = Nothing
Set wbs = Nothing
End Sub
In excel, you can have a user dynamically select a range of cells by selecting it with his/her mouse or keyboard (e.g., picture below, or simply when you're typing up a formula on ENTER mode and proceed to select a range of cells):
Does anyone know if such a functionality is callable from a Powerpoint macro/add-in? Essentially, I would like to have a piece of code in Powerpoint that generates a similar dialogue that allows a user to directly select a range of cells in an excel spreadsheet, and have the PPT macro record what that range is.
Thank you!
Here is an example you using GetObject
Note: The Application.InputBox opens modally. You cannot use it across workbooks.
Option Explicit
Sub GetAddressFromExcel()
Dim oXLApp As Object
Dim Ret As Object
'~~> Establish an EXCEL application object
Set oXLApp = GetObject(, "Excel.Application")
'~~> Show Excel
oXLApp.Visible = True
If oXLApp.Workbooks.Count = 0 Then
oXLApp.Workbooks.Add
End If
On Error Resume Next
Set Ret = oXLApp.InputBox("Please select Excel Range", Type:=8)
On Error GoTo 0
If Not Ret Is Nothing Then MsgBox Ret.Address
'~~> CLEANUP
oXLApp.Quit
Set oXLApp = Nothing
End Sub