Running a VB script inside solidworks macro?

Programming and macros
ResidentAtLarge
Posts: 35
Joined: Wed Jun 02, 2021 4:31 pm
Answers: 0
x 31
x 30

Running a VB script inside solidworks macro?

Unread post by ResidentAtLarge »

Is there a command in solidworks macro to run a VB script and then place a pause for 1 second while it runs? I am not finding any information on how to do this anywhere.
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

Re: Running a VB script inside solidworks macro?

Unread post by mattpeneguy »

This can get you a Sleep for 1 second:

Code: Select all

Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim swApp As Object
Sub main()
Set swApp = Application.SldWorks

Sleep (1000)

End Sub
Also, isn't VBScript a subset of VB? Could you just copy the code into the macro?
ResidentAtLarge
Posts: 35
Joined: Wed Jun 02, 2021 4:31 pm
Answers: 0
x 31
x 30

Re: Running a VB script inside solidworks macro?

Unread post by ResidentAtLarge »

mattpeneguy wrote: Tue Nov 02, 2021 11:11 am Also, isn't VBScript a subset of VB? Could you just copy the code into the macro?
I wasn't sure if vb could be run directly inside of solidworks VBA or not. I am not someone who knows a whole lot about programming either.

Thank you for the delay code!
User avatar
AlexB
Posts: 498
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 267
x 444

Re: Running a VB script inside solidworks macro?

Unread post by AlexB »

We've used this to run a compiled external program.

Code: Select all

Sub main()
    Shell ("C:\PathToYourEXEFile\SolidworksScript.exe")
End Sub
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

Re: Running a VB script inside solidworks macro?

Unread post by mattpeneguy »

AlexB wrote: Tue Nov 02, 2021 11:50 am We've used this to run a compiled external program.

Code: Select all

Sub main()
    Shell ("C:\PathToYourEXEFile\SolidworksScript.exe")
End Sub
Unfortunately that won't work for a vbs script... You need access to the WSH, I think. Someone like @josh or @gupta9665 would probably be able to answer this. I think it's possible it's not allowed because of security.
User avatar
RMcHugh
Posts: 29
Joined: Mon Mar 08, 2021 3:20 pm
Answers: 1
Location: NH, USA
x 89
x 29
Contact:

Re: Running a VB script inside solidworks macro?

Unread post by RMcHugh »

I remembered reading a similar question a while back. Didn't realize how long ago...

I found this in the SWYMp: https://r1132100503382-eu1-3dswym.3dexp ... MACNSJXzOg

TL/DR: Use RunMacro2 method, answered by @gupta9665

Disclaimer: I have no actual experience with the command, just a fuzzy, semi-functioning memory system.

Hope this helps,

Ray
User avatar
JSculley
Posts: 643
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 876

Re: Running a VB script inside solidworks macro?

Unread post by JSculley »

ResidentAtLarge wrote: Tue Nov 02, 2021 9:53 am Is there a command in solidworks macro to run a VB script and then place a pause for 1 second while it runs? I am not finding any information on how to do this anywhere.
Are you wanting to run a visual basic program or a VBScript script? They are two different things.
ResidentAtLarge
Posts: 35
Joined: Wed Jun 02, 2021 4:31 pm
Answers: 0
x 31
x 30

Re: Running a VB script inside solidworks macro?

Unread post by ResidentAtLarge »

JSculley wrote: Tue Nov 02, 2021 1:49 pm Are you wanting to run a visual basic program or a VBScript script? They are two different things.
I am looking to run a VBScript Script file.
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

Re: Running a VB script inside solidworks macro?

Unread post by mattpeneguy »

RMcHugh wrote: Tue Nov 02, 2021 1:28 pm I remembered reading a similar question a while back. Didn't realize how long ago...

I found this in the SWYMp: https://r1132100503382-eu1-3dswym.3dexp ... MACNSJXzOg

TL/DR: Use RunMacro2 method, answered by @gupta9665

Disclaimer: I have no actual experience with the command, just a fuzzy, semi-functioning memory system.

Hope this helps,

Ray
@RMcHugh,
It appears that will only run a Solidworks SWP macro, not a vbs script.
ResidentAtLarge
Posts: 35
Joined: Wed Jun 02, 2021 4:31 pm
Answers: 0
x 31
x 30

Re: Running a VB script inside solidworks macro?

Unread post by ResidentAtLarge »

My VBScript code I am looking to use is below:

Code: Select all

Const ForReading = 1

Dim arrLine()
Dim lnCnt

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\BOMTable_Export_TESTING.SLDASM.txt", ForReading)

'Read the csv file to get the header column names and data for each row.
Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.Readline
	lnCnt = lnCnt + 1
	If lnCnt = 2 Then
		arrHeaders = Split(strNextLine, vbTab)
	End If
	If lnCnt > 2 Then
		Redim Preserve arrLine(lnCnt - 2)
		arrLine(lnCnt - 2) = strNextLine
	End If	
Loop
objTextFile.Close	

'Load the data into a multidimentional array.
ReDim arrData(UBound(arrLine), UBound(arrHeaders))
For i = 0 to Ubound(arrLine)
	line = Split(arrLine(i), vbTab)
	For p = 0 to UBound(line)
		arrData(i, p) = line(p)
	Next
Next

'Find operations notes and raw material.
ReDim arrOperations(0)
ReDim tmpMaterial(0)
ReDim tmpMatIdx(0)
ReDim tmpMatQty(0)
For i = 0 to UBound(arrData, 1)

	curOper = arrData(i, GetColumnIndex("RawOperation"))
	If curOper <> "" Then
		idx = -1
		For o = 0 to UBound(arrOperations)
			If InStr(1, arrOperations(o), curOper) = 1 Then
				idx = o
				Exit For
			End If
		Next
		If idx = -1 Then
			Redim Preserve arrOperations(UBound(arrOperations) + 1)
			Redim Preserve arrRawMaterial(UBound(arrRawMaterial) + 1)
			idx = UBound(arrOperations)
			'Begin operation notes.
			arrOperations(idx) = curOper
			If arrData(i, GetColumnIndex("RawNote1")) <> "" Then arrOperations(idx) = arrOperations(idx) & vbCrlf & arrData(i, GetColumnIndex("RawNote1"))
			If arrData(i, GetColumnIndex("RawNote2")) <> "" Then
				arrOperations(idx) = arrOperations(idx) & vbCrlf & arrData(i, GetColumnIndex("RawNote2"))
				qty = arrData(i, GetColumnIndex("<FONT size=57PTS>QTY."))
				If qty > 1 Then arrOperations(idx) = arrOperations(idx) & vbCrlf & "(" & qty & " PER UNIT)"
			End If
			
			'Begin raw material
			mat = arrData(i, GetColumnIndex("RawPartNo"))
			If mat <> "" Then
				matIdx = -1
				For m = 0 to UBound(tmpMaterial)
					If mat = tmpMaterial(m) Then
						matIdx = m
						Exit For
					End If	
				Next
				If matIdx = -1 Then
					Redim Preserve tmpMaterial(UBound(tmpMaterial) + 1)
					Redim Preserve tmpMatIdx(UBound(tmpMatIdx) + 1)
					Redim Preserve tmpMatQty(UBound(tmpMatQty) + 1)
					matIdx = UBound(tmpMaterial)
					tmpMaterial(matIdx) = mat
					tmpMatIdx(matIdx) = idx
					tmpMatQty(matIdx) = CDbl(arrData(i, GetColumnIndex("RawMaterialQty")))
				Else
					tmpMatQty(matIdx) = CDbl(tmpMatQty(matIdx)) + CDbl(arrData(i, GetColumnIndex("RawMaterialQty")))
				End If
			End If
		Else
			'Add operation notes.
			If arrData(i, GetColumnIndex("RawNote1")) <> "" Then arrOperations(idx) = arrOperations(idx) & vbCrlf & vbCrlf & arrData(i, GetColumnIndex("RawNote1"))
			If arrData(i, GetColumnIndex("RawNote2")) <> "" Then 
				arrOperations(idx) = arrOperations(idx) & vbCrlf & arrData(i, GetColumnIndex("RawNote2"))
				qty = arrData(i, GetColumnIndex("<FONT size=57PTS>QTY."))
				If qty > 1 Then arrOperations(idx) = arrOperations(idx) & vbCrlf & "(" & qty & " PER UNIT)"
			End If
			
			'Add raw material
			mat = arrData(i, GetColumnIndex("RawPartNo"))
			If mat <> "" Then
				matIdx = -1
				For m = 0 to UBound(tmpMaterial)
					If mat = tmpMaterial(m) Then
						matIdx = m
						Exit For
					End If	
				Next
				If matIdx = -1 Then
					Redim Preserve tmpMaterial(UBound(tmpMaterial) + 1)
					Redim Preserve tmpMatIdx(UBound(tmpMatIdx) + 1)
					Redim Preserve tmpMatQty(UBound(tmpMatQty) + 1)
					matIdx = UBound(tmpMaterial)
					tmpMaterial(matIdx) = mat
					tmpMatIdx(matIdx) = idx
					tmpMatQty(matIdx) = CDbl(arrData(i, GetColumnIndex("RawMaterialQty")))
				Else
					tmpMatQty(matIdx) = CDbl(tmpMatQty(matIdx)) + CDbl(arrData(i, GetColumnIndex("RawMaterialQty")))
				End If
			End If
		End If
		
		
		
	End If
Next

'Display output
Set outFile = objFSO.CreateTextFile("c:\temp\operationnotes.csv", True)
Dim oLine
For i = 0 to UBound(arrOperations)
	oLine = chr(34) & Replace(arrOperations(i), chr(34), chr(34) & chr(34)) & chr(34)
	For m = 0 to UBound(tmpMatIdx)
		If tmpMatIdx(m) = i Then
			oLine = oLine & ", " & tmpMaterial(m) & ", " & tmpMatQty(m)
		End If
	Next
	outFile.WriteLine(oLine)
	'Wscript.Echo arrOperations(i) & vbCrlf & vbCrlf
Next
outFile.Close
	
'Returns the index number of the column name passed.
Function GetColumnIndex(columnName)
	GetColumnIndex = 0
	
	For c = 0 to UBound(arrHeaders)
		If arrHeaders(c) = columnName Then
			GetColumnIndex = c
			Exit For
		End If
	Next
End Function
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

Re: Running a VB script inside solidworks macro?

Unread post by mattpeneguy »

@ResidentAtLarge,
I'm not sure if it'll run from within a SW macro. I thought the "Scripting.FileSystemObject" wouldn't work, but it may according to this:
https://help.solidworks.com/2019/Englis ... nction.htm.

If you can setup a sandbox, to make sure you don't mess any production data up, you could try running it and see what happens. I was hoping Josh or Deepak would've replied by now. I'm sure they can conclusively say whether what you want to do is possible and if so how best to do it.
ResidentAtLarge
Posts: 35
Joined: Wed Jun 02, 2021 4:31 pm
Answers: 0
x 31
x 30

Re: Running a VB script inside solidworks macro?

Unread post by ResidentAtLarge »

mattpeneguy wrote: Tue Nov 02, 2021 4:01 pm @ResidentAtLarge,
I'm not sure if it'll run from within a SW macro. I thought the "Scripting.FileSystemObject" wouldn't work, but it may according to this:
https://help.solidworks.com/2019/Englis ... nction.htm.

If you can setup a sandbox, to make sure you don't mess any production data up, you could try running it and see what happens. I was hoping Josh or Deepak would've replied by now. I'm sure they can conclusively say whether what you want to do is possible and if so how best to do it.
I will wait and see what Josh or Deepak say.
Post Reply