How To Call a Subroutine From Another Object - vba

I am not entirely sure if this is possible, but assuming with us being able to set object references I don't see why not.
To start off, the object that contains the subroutine in question is Excel itself. I am wanting to call one of Excel's VBA subroutines using a different program's VB6 script editor.
I have tried the following without success, but hopefully you can see what I am trying to accomplish here:
Sub Excel_Test()
Dim appXL As Object
Set appXL = GetObject(, "Excel.Application")
Call appXL.Project1.Module1.Test()
End Sub
Obviously this code is not working - but what would be the proper Syntax (if one exists) to call the Macro Test, located in Module1 contained in Excel's object?

You can automate other instances of excel if you identify them by some criteria like the workbook name,
try like
Code:
set otherinstance = getobject(,"fullpath\filename.xls")
otherinstance.application.run "macroname"

Related

LIst of Functions called during script execution

I'm trying to consolidate functions for a very large automation suite using UFT. There are many functions that get called. I'm curious if anyone has a code snippet that uses the UFT object model, or something similar, that would trace functions called into either the output tab, a text file, or something else that could be perused to see which said functions were called during a script run.
A call to write an info row in the test results reviewer, output tab, etc. can be added, but it's obviously a very laborious exercise.
Sub GetAction(piRow,ByRef pstrActionName)
'On Error Resume Next
'Declaration
Dim objxl,objwb,rowno,strAction,objws
Set objxl=CreateObject("Excel.Application")
'Open the Datasheet
Set objwb=objxl.workbooks.open(excelpath)
objxl.visible=False
objxl.displayalerts=False
Set objws=objxl.activeworkbook.worksheets("Sheet1")
'Get the ActionName
strAction=objxl.cells(piRow,1)
pstrActionName=strAction
End If
If Err.Number<>0 Then
Msgbox "GetAction:"&"Error :"&Cstr(Err.Description)
End If
Set objws=Nothing
Set objwb=Nothing
'Close and Quit from Excel
objxl.Quit
End Sub'GetAction
Write all the function names in a excel sheet
Call above function which will read function names from the excel sheet
call function using below step
call pstrActionName

transferring variable form one workbook to another workbook vba

I am creating a userform (userform1) in workbookA and in the userform, I'll be needing a module(module1) created in other workbook (workbookB) to made some calculation. In order to launch the calculation, the module need some info (TTAA, tolerance). I would really love to know the easiest way to transfer some variable from userform1(workbookA.userform1) to module1(workbookB.module1) and re-transfer the results to userform1.
below, you can find a small part of the programm which I've coded to call the module.
TTAA = CDbl(Me.TTAA_textbox.Value)
tolerance = CDbl(Me.tolerance_textbox.Value)
application.Run ("'Workbooks.xls'!module1")
I've declared as public, the variable TTAA and tolerance in the module1
The easiest way (which I know to work) is to temporarily "save" the variable on a sheet in one of either file and then reference it as
Worksheets.Add.Name = "tmp"
Workbooks(WorkbookA.Name).Worksheets("tmp").Range("A1").Value2
'And remove it later on
Application.DisplayAlerts = False
Workbooks(WorkbookA.Name).Worksheets("tmp").Delete
Application.DisplayAlerts = True
An alternative might be to declare the subs in WorkbookB as functions. Apart from public variables this is the only way to get return values (in a native format) from independent VBA code segments. Yet, if you had no luck with public variables I doubt this will work.
But why don't you import the module into WorkbookA instead? The following SO answer might be able to help you with that.
Writing a macro that writes a macro to another Excel-file

Access VBA: SUB designed to refresh an Excel Spreadsheet giving an error

Relative VBA newbie here. I created a new Module "QCDataRefresh" with a Public Sub "RefreshExcel" that is designed to open an Excel spreadsheet (housed on my network drive) in the background and refresh it. This is done in order to refresh the many data links contained within said spreadsheet.
Here is the code for this Sub:
Public Sub RefreshExcel()
Dim appExcel As Object
Set appExcel = CreateObject("Excel.Application")
appExcel.workbooks.Open ("\\renssfile2\shares\Supply Chain Project Management\QCData.xlsx")
appExcel.activeworkbook.refreshall
Set appExcel = Nothing
End Function
Running this gives a run-time error '1004': "Application Defined or object-defined error. Debugging seems to highlight the line of code "appExcel.activeworkbook.refreshall". Not sure why though?
Additionally, I am also attempting to create a button in Access to run this from my dashboard. To do this, I created a new button, and created a new Event Procedure for this entitled, "Command101_Click".
The following is the code for that Event, which attempts to run my RefreshExcel Sub upon clicking the button:
Private Sub Command101_Click()
QCDataRefresh.RefreshExcel
End Sub
When I run this Event, I would expect it to give me the same error from my refresh Function above. However, instead it opens a window asking me to select a Macro. I'm unsure what my mistake is here, but I'm sure that it must be a simple oversight. Thoughts on this as well?
Thanks all!
I think the problem is that the workbooks.open method returns a workbook.
Try this:
Public Sub RefreshExcel()
Dim appExcel As Object
Dim excelwb As Excel.Workbook
Set appExcel = CreateObject("Excel.Application")
Set excelwb = appExcel.workbooks.Open ("\\renssfile2\shares\Supply Chain Project Management\QCData.xlsx")
excelwb.refreshall
Set excelwb = Nothing
Set appExcel = Nothing
End Sub
Whenever I have unexpected and unusual issue\error for using excel to output to or read from I allways try the following, especially when the code breaks in the middle and doesn't get to the line where you close the spreadsheet and close the Excel object from memory.
So close your spreadsheet and any Excel instances and objects you may have as follow:
Close all Excel files and excel program if you have any open
Go to the Task Manager (by doing Alt+Ctrl+Del) then select Task Manager
Go to processes tab
Sort by process name
Look for any Excel.exe in the list and force close them all one by one
Let us know.

Get the filename of open Excel workbook in Word VBA

Does anyone know how to get the filename of an open Excel wordbook using Word VBA, so that I can copy some information to my Word document?
This can get a lot more complicated depending on how sure you need to be, and whether it is for personal or public use:
Set objWithName = GetObject("C:\docs\testx.xls")
Set objClassOnly = GetObject("", "Excel.Application")
Debug.Print objWithName.Name
Debug.Print objClassOnly.Name
It is possible to have more than one instance of Excel running and each instance may have more than one workbook open, but get object will only return one instance. If you know the name of the file you want, it is a lot easier, because you can use the first version above.
If you know the application will be open and it’ll be the first (if only) instance open, using the following code. In Word, you’ll need to add Excel 12 reference (Tools| References, Microsoft Excel 12.0 Object Library).
Sub test()
Dim objClassOnly As Excel.Application
Set objClassOnly = GetObject(, "Excel.Application")
Debug.Print objClassOnly.Name
Debug.Print objClassOnly.ActiveWorkbook.Name
End Sub

How to access a constant defined in another Excel workbook?

Question
How do I access a public constant defined in another Excel workbook?
Setup
Workbook.xls
My VBA code runs in the current Excel workbook called "MyWorkbook.xls".
It has some VBA code in it which reads a file name --"OtherWorkbook.xls in this case-- from cell "A1". Then, the code should read a constant defined in that other workbook and write it to cell "A2". Here is what my code looks like:
Sub GetValueFromOtherWorkbook()
otherWorkbook = Range("A1").Value ' Value = "OtherWorkbook.xls"
Dim returnedValue As String
' Idea 1: Doesn't work
Set returnedValue = OtherWorkbook.xls!Module1.MY_CONSTANT
' Idea 2: Doesn't work either
Set returnedValue = Application.Run(otherWorkbook & "!Module1.MY_CONSTANT")
Range("A2").Value = returnedValue ' MY_CONSTANT = "testValue"
End Sub
OtherWorkbook.xls
The second Excel workbook is called "OtherWorkbook.xls" as mentioned above.
It has a VBA module called Module1.
Module1 defines a public constant. The code looks like this:
Public Const MY_CONSTANT As String = "testValue"
Problem
The code from MyWorkbook.xls does not run, it returns the following error
Run-time error '424'
Object required
I don't quite know what to do with it even after reading the help documentation. In another context, I managed to call a routine in another workbook with this code:
otherWorkbookName = "OtherWorkbook.xls"
Application.Run (otherWorkbookName & "!Module1.SomeRoutine")
So, calling a routine works, but accessing a public constant does not.
Any ideas what I could try next?
A code module is not an object that can be accessed like a worksheet.
You can access and manipulate it, but in order to do that you need to reference the Microsoft Visual Basic for Applications Extensibility library and allow access to the VBA project (Tools - Options - Security - Macro security - Trusted publishers - Trust access to VBA project), after which you can use the Workbook.VBProject property of the second workbook.
You don't need to do this. Instead you can create a reference to the other workbook: in VBA IDE, go Tools - References, click Browse and locate your workbook with the constant.
If that's no good, you can create a method in that workbook that only returns the value of the constant, and call it as you have demonstrated.