Call a solidworks macro from a sub and pass global variables - vba

In Solidworks I record two macros.
Macro 1 is empty:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
'added code
Dim distance_of_second_plane
'end of added code
Sub main()
Set swApp = _
Application.SldWorks
Set Part = swApp.ActiveDoc
'added code: here I want to call the second macro and send it distance_of_second_plane, and have it use that value
distance_of_second_plane = .05
'.. now what?
'end of added code, don't know what to add.
End Sub
Macro 2 does something that requires data from macro 1:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = _
Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
Dim myRefPlane As Object
Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, 0.05334, 0, 0, 0, 0)
Part.ClearSelection2 True
End Sub
these macros are of course saved in different files. How do I call the second from the first, passing in the data from the first, and using it in the second?
things I`ve tried: http://support.microsoft.com/kb/140033, http://www.cadsharp.com/macros/run-macro-from-another-macro-vba/, VBA module that runs other modules, Call a Subroutine from a different Module in VBA
all of them are problematic. I'll give details of the errors I get if asked for them.

You could create a separate module to encapsulate your common methods. Then add reference to that module and call it from both your Macro1 and Macro2 sub routines.
So for example in your Macro1:
In the project explorer, right click on the Modules folder and Insert-Module, name in Module1 (or whatever you want)
Create a subroutine in Module1, name it InsertPlane (or whatever name makes sense), and construct the subroutine with parameters that will be needed to accomplish what needs to be done
Sub InsertPlane(ByRef swApp As Object, ByVal distance As Double)
'enter your code here, the parameters passed to InsertPlane can be whatever you need
'to do what the method needs to do.
End Sub
In your main() method of your macro, call the InsertPlane() method when you need it
Sub main()
Set swApp = _
Application.SldWorks
Set Part = swApp.ActiveDoc
'added code: here I want to call the second macro and send it distance_of_second_plane, and have it use that value
distance_of_second_plane = 0.05
'example of calling the subroutine
boolstatus = Module1.InsertPlane(swApp, distance_of_second_plane)
End Sub
The modules can be exported and imported, so you can reuse them in other macros, to do that right click in the project tree on Module1 and export file. Similarly, you can right click on the Modules folder and Import modules.
Hope this helps.

While it is possible to run a macro from another macro using ISldWorks::RunMacro2, it is not possible to have this method return any values that you can use in your first macro. Even if this was possible, I would not recommend it.
Everything you need to accomplish can be accomplished in a single macro, you simply need to learn how to use the SolidWorks API to reach that end. Could you please explain what you are trying to accomplish with your macro? Then I could show you what code you need.
Please also note that the macro recorder is really not a good tool for creating macros of any significance. If you plan on using the SolidWorks API seriously, then you really need these three skills under your belt:
How to do basic programming with VBA (variables, arrays, conditionals, loops, etc)
How to navigate and read the SolidWorks API Help (the offline version)
How API objects are related to one another (SolidWorks API Object Model)
I have some videos at my web site (in my profile) that can help you get started. Again, if you want me to help here with your current problem, please explain what you're trying to automate.

Related

Terminate method not called in Access 2021

Anyone else finding that their Terminate() method in Access isn't being called?
Here's my code for my cBusyPointer class with the comments removed for brevity:
Option Compare Database ' Use database order for string comparisons
Option Explicit ' Declare EVERYTHING!
Option Base 1 ' Arrays start at 1
Public Sub show()
DoCmd.hourGlass True
End Sub
Private Sub Class_Terminate()
DoCmd.hourGlass False
End Sub
Typical usage is:
Sub doTehThings()
Dim hourGlass As New cBusyPointer
hourGlass.show()
' Do all teh things
End Sub
In previous versions, this class would restore the hourglass whenever the object went out of scope and was destroyed.
I even tried to manually destroy my hourglass object:
Sub doTehThings()
Dim hourGlass As cBusyPointer
Set hourGlass = New cBusyPointer
hourGlass.show()
' Do all teh things
Set hourGlass = Nothing
End Sub
The only way to fix this is to add a hide() method and call that.
Has anyone else encountered this problem?
I cannot replicate the issue. The Terminate() method is called upon reaching the Set hourGlass = Nothing.
A couple of points:
Dim hourGlass As New cBusyPointer
This will create a new instance every time you call the hourGlass variable even to set it to Nothing. See the answer in the link below for additional info:
What's the difference between Dim As New vs Dim / Set
You should always use Dim/Set when working with objects.
hourGlass.show()
This does not even compile in VBA. Subs do not accept parentheses even when arguments are being expected, unless they are preceded with the Call keyword.
Lastly, the cleanest way to reference an object is to access it using the With statement which ensures the object is terminated when the End With statement is reached.
With New cBusyPointer
.show
End With

Creating a new IUI Automation Handler object in order to subscribe to an automation event

So, here it goes. To start, A disclaimer, I understand that MS Access is not built for this kind of work. It is my only option at this time.
I have done just a bit of Automation using UIAutomationClient and I have successfully used its other features, however I cannot for the life of me get it to subscribe to events.
Normally, it is supposed to be a bit like this:
Dim CUI as new CUIAutomation
Dim FocusHandler as IUIAutomationFocusChangedEventHandler
Set FocusHandler = new IUIAutomationFocusChangedEventHandler(onFocusChanged)
C.AddFocusChangedEventHandler(Element,TreeScope_Children, null, FocusHandler)
end function
'
'
Function onFocusChanged(src as Object, args as AutomationEventArgs)
''my code here
end function
Yet when I attempt this, I get the error "expected end of statement" on the line:
FocusHandler = new IUIAutomationFocusChangedEventHandler(onFocusChanged)
additionally, if I leave off the (onFocusChanged) I get the error "Invalid use of new Keyword".
It seems like I am missing a reference somewhere. The usual drop down when using "new" does not contain the IUI handler classes though they are in the object library.
I am not sure if there is just some piece I am not accounting for in the code since I am using vba, but all examples seem to be for .net or C#/C++. Any help would be appreciated.
Additionally, I have no problem finding the element in question and all other pieces work fine. If you need any other pieces of the code let me know.
Edit: added set to line 3. No change in the problem though.
After two years this probably isn't relevant any more, but perhaps somebody else encounters this problem... The answer is to create a new class that implements the HandleAutomationEvent method.
Here I created a class named clsInvokeEventHandler and (importantly) set the Instancing property to PublicNotCreatable:
Option Explicit
Implements IUIAutomationEventHandler
Private Sub IUIAutomationEventHandler_HandleAutomationEvent(ByVal sender As UIAutomationClient.IUIAutomationElement, ByVal eventId As Long)
Debug.Print sender.CurrentName
End Sub
And to use it:
Sub StartInvokeHandler()
Dim oUIA As New CUIAutomation8
Dim oRoot As IUIAutomationElement
Dim InvokeHandler As clsInvokeEventHandler
Set InvokeHandler = New clsInvokeEventHandler
Set oRoot = oUIA.GetRootElement
oUIA.AddAutomationEventHandler UIA_Invoke_InvokedEventId, oRoot, TreeScope_Descendants, Nothing, InvokeHandler
End Sub

VBA event raise only once (intermittent)

Context
My VBA code often replace worksheets inside the Workbook. Therefore I can't use code directly in the worksheet module as it would be eventually deleted in the process.
I use a user-defined class to handle my events (strongly inspired from Chip Pearson's withevents article)
Public WithEvents ws As Worksheet
Private Sub ws_Activate()
If ActiveSheet.Name = FREEBOM_SHEET_NAME Then
Call FREEBOM_Worksheet_Activate_Handler
End If 'ActiveSheet.Name = FREEBOM_SHEET_NAME
End Sub
Private Sub ws_Change(ByVal Target As Range)
'MsgBox Target.Parent.Name
If Target.Parent.Name = FREEBOM_SHEET_NAME Then
Call FREEBOM_Worksheet_Change_Handler(Target)
End If 'Target.Parent.Name = FREEBOM_SHEET_NAME
If Target.Parent.Name = BOM_SHEET_NAME Then
Call BOM_Worksheet_Change_Handler(Target)
End If 'Target.Parent.Name = BOM_SHEET_NAME
End Sub
The class is being instantiated when the workbook is opened.
Private Sub Workbook_Open()
Dim WSObj_FreeBOM As FreeBOM_CWorkSheetEventHandler
Dim WSObj_BOM As FreeBOM_CWorkSheetEventHandler
If Freebom_EventCollection Is Nothing Then
Set Freebom_EventCollection = New Collection
End If
Set WSObj_FreeBOM = New FreeBOM_CWorkSheetEventHandler
Set WSObj_FreeBOM.ws = Sheets(FREEBOM_SHEET_NAME)
Set WSObj_BOM = New FreeBOM_CWorkSheetEventHandler
Set WSObj_BOM.ws = Sheets(BOM_SHEET_NAME)
Freebom_EventCollection.Add Item:=WSObj_FreeBOM, Key:=Sheets(FREEBOM_SHEET_NAME).Name
Freebom_EventCollection.Add Item:=WSObj_BOM, Key:=Sheets(BOM_SHEET_NAME).Name
End Sub
During my reading on the subject, I saw that linking your object to a public collection (the declaraiton is in another module (an ordinary module - not a Worksheet module and not a Class module). : Public Freebom_EventCollection As Collection would keep my instance alive even if the execution leaves the scope of the current initianlization function.
Problem Description
In most scenario, I will get only one ws_change event being raised. After that, the sheet behave as if there is no event handler in my code. Nothing is being raised, not just the worksheet events.
I have look at Application.EnableEvents but it is always set to True after the first run.
Also, when I use the build in Private Sub Worksheet_Change(ByVal Target As Range) function it worked well.
To me it is probably linked to the fact that I use a class and it is not staying alive after the first run. But then, I do not know what I am doing wrong.
Thank you in advance for you time and help in this matter.
you need to declare a module level Public instance of the Collection in an ordinary module (not a Worksheet module and not a Class module). You may as well put the code to manage the collection there as well and simply have calls from the event handlers of the worksheet modules. You may need to re-initialise the collection whenever you delete a sheet as this will probably trigger a re-compile and reset your project, which will terminate your objects.
Once you have the collection in the standard module, you can monitor its life cycle by adding a watch (SHIFT-F9 in VBE). Then you can keep track of exactly what is going on.

Excel VBA - QueryTable AfterRefresh function not being called after Refresh completes

I am developing an Excel (2010+) Application using VBA and have run into an issue where the AfterRefresh event function is not being invoked once the query finishes executing.
I have not been able to find many decent resources or documentation for how to have this event function triggered in a Class Module. I decided to use the Class Module design route instead of putting the event handlers in the worksheet after receiving a response to an earlier question about QueryTables (found here Excel VBA AfterRefresh).
Here is the code for my Class Module called CQtEvents
Option Explicit
Private WithEvents mQryTble As Excel.QueryTable
Private msOldSql As String
' Properties
Public Property Set QryTble(ByVal QryTable As QueryTable): Set mQryTble = QryTable:
End Property
Public Property Get QryTble() As QueryTable: Set QryTble = mQryTble:
End Property
Public Property Let OldSql(ByVal sOldSql As String): msOldSql = sOldSql:
End Property
Public Property Get OldSql() As String: OldSql = msOldSql:
End Property
Private Sub Class_Initialize()
MsgBox "CQtEvents init"
End Sub
' Resets the query sql to the original unmodified sql statement
' This method is invoked when the Refresh thread finishes executing
Private Sub mQryTble_AfterRefresh(ByVal Success As Boolean)
' Problem is here
' This function is never called :( Even if the query successfully runs
Me.QryTble.CommandText = Me.OldSql
End Sub
Here is a quick snapshot of the code the creates an instance of this class, finds a relevant QueryTable, then calls Refresh
Option Explicit
Sub RefreshDataQuery()
'Dependencies: Microsoft Scripting Runtime (Tools->References) for Dictionary (HashTable) object
'From MGLOBALS
cacheSheetName = "Cache"
Set cacheSheet = Worksheets(cacheSheetName)
Dim querySheet As Worksheet
Dim interface As Worksheet
Dim classQtEvents As CQtEvents
Set querySheet = Worksheets("QTable")
Set interface = Worksheets("Interface")
Set classQtEvents = New CQtEvents
Dim qt As QueryTable
Dim qtDict As New Scripting.Dictionary
Set qtDict = UtilFunctions.CollectAllQueryTablesToDict
Set qt = qtDict.Item("Query from fred2")
''' Building SQL Query String '''
Dim sqlQueryString As String
sqlQueryString = qt.CommandText
Set classQtEvents.QryTble = qt
classQtEvents.OldSql = sqlQueryString ' Cache the original query string
QueryBuilder.BuildSQLQueryStringFromInterface interface, sqlQueryString
' Test message
MsgBox sqlQueryString
qt.CommandText = sqlQueryString
If Not qt Is Nothing Then
qt.Refresh
Else
' ... Error handling code here...
End If
''' CLEAN UP '''
' Free the dictionary
Set qtDict = Nothing
End Sub
Also here is a screenshot of the Module structure http://imgur.com/8fUcfLV
My first thought on what might be the issue was passing the QueryTable by value. I am not the most experienced VBA developer, but I reasoned this would create a copy and be calling the event on an unrelated table. However, this was not the case and passing by Reference did not fix the problem either.
Also the query is confirmed to run successfully as the data is correctly showing up and being refreshed.
EDIT
I added the BeforeRefresh event function to CQtEvents class Module and confirmed this function is called once Refresh is called
Private Sub mQryTble_BeforeRefresh(Cancel As Boolean)
MsgBox "Start of BeforeRefresh"
End Sub
How might I alter this code get my QueryTable from the QTableModule's RefreshDataQuery() Sub routine to have the AfterRefresh function invoked when the query is successfully ran?
How to catch the AfterRefresh event of QueryTable?
Explanation: in your situation, before event was fired you lost reference of your QueryTable by setting it to nothing when you made cleaning or procedure ended.
General solution: you must be sure that your code is still running and/or you need to keep any references to your QueryTable.
1st solution. When calling QT.Refresh method set the parameter to false in this way:
qt.Refresh false
which will stop further code execution until your qt is refreshed. But I don't consider this solution to be the best one.
2nd solution. Make your classQtEvents variable public and after RefreshDataQuery sub is finished check the status with some other code.
in you CQtEvents class module add the following public variable:
Public Refreshed As Boolean
in your BeforeRefresh event add this:
Refreshed = False
in your AfterRefresh event add this line of code:
Refreshed = True
Make your classQtEvents variable declaration public. Put this before Sub RefreshDataQuery()
Public classQtEvents as CQtEvents
but remove appropriate declaration from within your sub.
Now, even your sub is finished you will be able to check status of refreshment by checking .Refreshed property. You could do it in Immediate or within other Sub. This should work for Immediate:
Debug.Print classQtEvents.Refreshed
3rd solution. (a bit similar to 1st one) Follow steps 1 to 3 from 2nd solution. After you call qt.Refresh method you could add this loop which will stop further code execution until qt is refreshed:
'your code
If Not qt Is Nothing Then
qt.Refresh
Else
' ... Error handling code here...
End If
'checking
Do Until classQtEvents.Refreshed
DoEvents
Loop
Final remark. I hope I didn't mixed up qt variable with classQtEvents variable. I didn't tried and tested any solution using your variables but wrote all above with referenced to code I use.
A github repo that demonstrates the minimum code needed to get this working can be found here.
As mentioned, if your event handler isn't in scope, or your QueryTable reference is lost, you won't catch the event. The key factors to ensuring you catch the event are:
Declare a global variable of your event-handling class module's type outside of any subroutines/methods, at the top of a file (I chose the ThisWorkbook file).
Add a Workbook_Open event handler and instantiate that variable there, so that it is available immediately and will remain in scope (since it's global).
At that point, or at any downstream point when you have a QueryTable you're interested in, pass that QueryTable to the global instance to wire up its events.
(It took me a couple tries to figure this out myself, when someone pointed me in this direction as an answer to this question.)

vb.net "PasteSpecial method of Range class failed"

I'm trying to write an application in VB.net that assembles an Excel Workbook by wisely coping cells from another opened Workbook. [Note: as for now, the two workbooks are opened within the same Excel application - Originally I was using two different Excel instances, but only later I realized that the PasteSpecial between two Instances behaves differently]
I'm using Visual Studio 2012, Excel 2007 and I'm including Microsoft Excel 12.0 Object Library in the project references
The code is something like that:
Dim appXL As Excel.Application
Dim wbXLsource As Excel.Workbook
Dim wbXLtarget As Excel.Workbook
''with two different buttonclick event handlers
''I assign wbXLsource and wbXLtarget
''the full code is omitted
...
wbXLsource = appXL.Workbooks.Open(strFileNameAndPath)
...
...
wbXLtarget = appXL.Workbooks.Add
...
''I use a third button handler for the
''Copy and PasteSpecial Operations
Private Sub btnAppendWorksheet_Click(sender As Object, e As EventArgs) _
Handles btnAppendWorksheet.Click
Dim shXLtar As Excel.Worksheet
Dim shXLsou As Excel.Worksheet
shXLtar = wbXLtarget.ActiveSheet
shXLtar.Cells.Clear()
shXLsou = wbXLsource.ActiveSheet
shXLsou.Range("A1:H433").Copy()
Try
shXLtar.Range("A1:H433").PasteSpecial(Excel.XlPasteType.xlPasteAll, False, False) ''Paste special Format:=
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
The PasteSpecial method throws the exception "PasteSpecial method of Range class failed".
What is strange is that the same code originally worked within two workbooks that run in different Excel instances [At that time I had appXLtarget and appXLsource].
Needless to say that I tried all the possible combinations of "Selection", "Activate" in any part of the code: eg between Copy and PasteSpecial etc etc.
Probably there is something really coarse that I'm missing <- I'm new of VB.net
Thanks for any help and Best Regards!
If you are new in VB.Net, you should first do research about OptionStrict. With optionStrict set to ON, VS won't compile your code...
Replace
shXLtar.Range("A1:H433").PasteSpecial(Excel.XlPasteType.xlPasteAll, False, False)
With
shXLtar.Range("A1:H433").PasteSpecial(Excel.XlPasteType.xlPasteAll,Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,False, False)
or
shXLtar.Range("A1:H433").PasteSpecial(Excel.XlPasteType.xlPasteAll)
Hope this helps.