I generally use VB.Net for programming but I have been delegated to a VBA project that would benefit from using script control for running script. Here is sample code but errors out on the .Run line of code. I can do this in VB.Net easy but can't get it to work in vba.
ERROR = Wrong number of arguments or invalid property assignent
Option Explicit
Dim sc As New ScriptControl
Sub RunFunctions()
MsgBox (Eval("2 * PI"))
End Sub
Function Eval(expr As String) As Object
Dim code As String
code = "Dim PI" & vbCrLf & _
"PI = 3.1416" & vbCrLf & _
" " & vbCrLf & _
"Function Result" & vbCrLf & _
" Result = " & expr & vbCrLf & _
"End Function"
sc.Language = "VBScript"
sc.AllowUI = True
sc.AddCode (code)
Dim myparams() as variant
Eval = sc.Run("Result", myparams)
End Function
Using the .Eval function from the script control object runs ok in vba but does not run scripts. Here is an example of that if someone cares to know...
Sub SimpleTask()
Dim expr As String
sc.Language = "VBScript"
expr = "2 + 2 * 50"
MsgBox sc.Eval(expr)
End Sub
I think the issue is with your parameters to sc.Run. Try changing this call from
Eval = sc.Run("Result", myparams)
to
Eval = sc.Run("Result")
Update
Here is a complete form using your code that compiles and runs correctly:
Option Explicit On
Option Strict On
Imports MSScriptControl
Public Class Form1
Dim sc As New ScriptControl
Sub RunFunctions()
MsgBox(Eval("2 * PI"))
End Sub
Function Eval(expr As String) As Object
Dim code As String
code = "Dim PI" & vbCrLf & _
"PI = 3.1416" & vbCrLf & _
" " & vbCrLf & _
"Function Result" & vbCrLf & _
" Result = " & expr & vbCrLf & _
"End Function"
sc.Language = "VBScript"
sc.AllowUI = True
sc.AddCode(code)
Dim myparams() As Object
Eval = sc.Run("Result")
End Function
Sub SimpleTask()
Dim expr As String
sc.Language = "VBScript"
expr = "2 + 2 * 50"
MsgBox(sc.Eval(expr))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
SimpleTask()
RunFunctions()
End Sub
End Class
Here is the final working code for VBA. competent_tech has one for VB.Net.
Option Explicit
Dim sc As New ScriptControl
Sub RunFunctions()
MsgBox (Eval("2 * PI"))
End Sub
Function Eval(expr As String) As String
Dim code As String
code = "Dim PI" & vbCrLf & _
"PI = 3.1416" & vbCrLf & _
" " & vbCrLf & _
"Function Result" & vbCrLf & _
" Result = " & expr & vbCrLf & _
"End Function"
sc.Language = "VBScript"
sc.AllowUI = True
sc.AddCode (code)
Eval = sc.Run("Result")
End Function
Related
i would like to call an Octave script from VBA with a batch file and passing some arguments to it.
The Octave script creates a .csv file which is needed in the VBA code later on.
The Problem is, that the VBA code doesn't wait for Octave creating the .csv file and i get an error message that the file is not there. So i need a way to wait until Octave has created the file.
Here is my VBA code:
Public Sub LinkOctave(ByVal Projekt As String, ByVal Winddaten As String, ByVal Ak As String, ByVal Typ As String, _
ByVal Anz As Double, ByVal NH As Double, ByVal Netto As Double, _
ByVal StartGW As Double, ByVal EndGW As Double, _
ByVal StartP As Double, ByVal EndP As Double)
Dim PID As Variant
Dim BatchFilePath As String
Dim ProjektBat As String
Dim WinddatenBat As String
Dim AkBat As String
Dim TypBat As String
Dim AnzBat As Double
Dim NHBat As String
Dim NettoBat As String
Dim StartGWBat As String
Dim EndGWBat As String
Dim StartPBat As String
Dim EndPBat As String
ProjektBat = "'" & Projekt & "'"
WinddatenBat = "'" & Winddaten & "'"
AkBat = "'" & Ak & "'"
TypBat = "'" & Typ & "'"
AnzBat = Anz
NHBat = str(NH)
NettoBat = str(Netto)
StartGWBat = str(StartGW)
EndGWBat = str(EndGW)
StartPBat = str(StartP)
EndPBat = str(EndP)
BatchFilePath = "C:\Users\bla\Documents\Octave\PFM\runfctPFM.bat"
PID = Shell(BatchFilePath & " " & ProjektBat & " " & WinddatenBat & " " & AkBat & " " & _
TypBat & " " & AnzBat & " " & NHBat & " " & NettoBat & " " & StartGWBat & " " & _
EndGWBat & " " & StartPBat & " " & EndPBat)
End Sub
I already found the way to do it with wsh.Run like this:
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim lngErrorCode As Long
lngErrorCode = wsh.Run(BatchFilePath & " " & ProjektBat & " " & WinddatenBat & " " & AkBat & " " & _
TypBat & " " & AnzBat & " " & NHBat & " " & NettoBat & " " & StartGWBat & " " & _
EndGWBat & " " & StartPBat & " " & EndPBat, windowStyle, waitOnReturn)
If lngErrorCode <> 0 Then
MsgBox "Something went wrong with the batch file!"
Exit Sub
End If
But it still doesn't wait for Octave to finish.
What did i wrong? Or is there another solution?
This is the code of the bat file, where i call the octave script:
#echo off
set OCT_HOME=C:\Octave\Octave-5.1.0.0\mingw64\
set "PATH=%OCT_HOME%\bin;%PATH%"
set SCRIPTS_DIR=%~dp0
set ProjektBat=%1
set WinddatenBat=%2
set AkBat=%3
set TypBat=%4
set AnzBat=%5
set NHBat=%6
set NettoBat=%7
set StartGWBat=%8
set EndGWBat=%9
shift /1
set StartPBat=%9
shift /1
set EndPBat=%9
start octave-cli.exe --eval "cd(getenv('SCRIPTS_DIR')); fctPFM(%ProjektBat%, %WinddatenBat%, %AkBat%, %TypBat%, %AnzBat%, %NHBat%, %NettoBat%, %StartGWBat%, %EndGWBat%, %StartPBat%, %EndPBat%); quit;"
Thanks for your support
Cheers
The bat file has a 'start' command in it which according to the Microsoft docs opens a separate command prompt for the command - so its not going to wait for the separate command window to finish.
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/start
Note also in windows, you should probably not be calling the octave executables directly, rather use the .bat files that come with it, as they set up expected environment variables in order for octave to run correctly.
I'm using a COM interface to export animations from a third-party program. I'm sending an exporting COM command with script from my tool with a shell command.
There's a problem with when I send the animation export command to the third-party tool. It starts to export, but my tool is sending a second animation export command while the last one is not finished. How can I prevent from this situation?
I'd like to sending my shell command from the for loop after the file was created.
My code is like below.
Private Sub tlbCheckSolveEvaCtrl_exportmodeshape_Click(sender As Object, e As EventArgs) Handles tlbCheckSolveEvaCtrl_exportmodeshape.Click
Try
Dim strArgument As String
Dim strfilePathEV As String
Dim strfilePathANI As String
Dim strfilePathPIC As String
strfilePathEV = strProjMdlDir & My.Settings.txtCheckSolverOuputDir & strProjMdlName & ".ev.sbr"
strfilePathANI = strProjMdlDir & "\" & My.Settings.txtProjDirDOC & "\" & My.Settings.txtProjDirANI & "\"
strfilePathPIC = strProjMdlDir & "\" & My.Settings.txtProjDirDOC & "\" & My.Settings.txtProjDirPIC & "\"
For i As Integer = 0 To dgvCheckSolveEva.RowCount - 1
strArgument = strfilePathEV & " " & _
strfilePathANI & strProjMdlName & "_" & i & ".mpg" & " " & _
i
Shell(My.Settings.txtSpckDir & "simpack-post.exe -s qs_mode_shape.qs " & strArgument)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I'd like to continue my for loop if strfilePathANI & strProjMdlName & "_" & i & ".mpg", the animation file was created, so I can start to export the next one.
The best way would be to use the .NET Process class and call the WaitForExit() method in order to wait for simpack-post.exe to close itself.
Shell() is an outdated function from the VB6-era which exists purely for partial backwards compatibility with that language. It shouldn't be used in new code.
Basic example:
Dim filePath As String = Path.Combine(My.Settings.txtSpckDir, "simpack-post.exe")
Process.Start(filePath, "-s qs_mode_shape.qs " & strArgument).WaitForExit()
The problem with this of course is that it might block the UI thread and thus cause it to freeze, depending on how long it takes for the process to exit. Therefore we should wrap it in a Task:
Dim c As Integer = dgvCheckSolveEva.RowCount - 1
Task.Run( _
Sub()
For i As Integer = 0 To c
strArgument = strfilePathEV & " " & _
strfilePathANI & strProjMdlName & "_" & i & ".mpg" & " " & _
i
Dim filePath As String = Path.Combine(My.Settings.txtSpckDir, "simpack-post.exe")
Process.Start(filePath, "-s qs_mode_shape.qs " & strArgument).WaitForExit()
Next
End Sub _
)
Just note that you cannot directly access the UI from within the task. If you want to do so you need to Invoke.
EDIT:
If you are targeting .NET Framework 3.5 or lower, or using VS 2008 or lower, tasks aren't available and we have to resort to using regular threads and/or regular methods instead of lamba expressions.
Note that the same rules apply, though - you cannot access the UI without invoking.
.NET 3.5 (or lower) using VS 2010 (and higher):
Dim c As Integer = dgvCheckSolveEva.RowCount - 1
Dim t As New Thread( _
Sub()
For i As Integer = 0 To c
strArgument = strfilePathEV & " " & _
strfilePathANI & strProjMdlName & "_" & i & ".mpg" & " " & _
i
Dim filePath As String = Path.Combine(My.Settings.txtSpckDir, "simpack-post.exe")
Process.Start(filePath, "-s qs_mode_shape.qs " & strArgument).WaitForExit()
Next
End Sub _
)
t.IsBackground = True
t.Start()
.NET 3.5 (or lower) using VS 2008 (or lower):
Private Sub tlbCheckSolveEvaCtrl_exportmodeshape_Click(sender As Object, e As EventArgs) Handles tlbCheckSolveEvaCtrl_exportmodeshape.Click
...your code...
Dim c As Integer = dgvCheckSolveEva.RowCount - 1
Dim t As New Thread(New ParameterizedThreadStart(AddressOf ExportAnimationsThread))
t.IsBackground = True
t.Start(c)
...your code...
End Sub
Private Sub ExportAnimationsThread(ByVal Count As Integer)
For i As Integer = 0 To Count
strArgument = strfilePathEV & " " & _
strfilePathANI & strProjMdlName & "_" & i & ".mpg" & " " & _
i
Dim filePath As String = Path.Combine(My.Settings.txtSpckDir, "simpack-post.exe")
Process.Start(filePath, "-s qs_mode_shape.qs " & strArgument).WaitForExit()
Next
End Sub
I'm trying to write a code module using the InsertLines method, but am getting the
Statement too complex Error
My code boils down to this loop:
Dim extractorModule As VBComponent
With extractorModule.codeModule
Dim singItem As codeItem
Dim i As Long
For i = LBound(codeItems) To UBound(codeItems)
singItem = codeItems(i) 'array of private type with .value property
.InsertLines 5, singItem.value 'write to line 5
Next i
End With
Which loops through an array of custom codeItems, and writes their .value to a new module with .InsertLines
singItem.value is a base64 encoded string. If it is a short one, 100 characters say, like this string:
.code_content = "QXR0cmlidXRlIFZCX05hbWUgPSAic2ltcGxlTW9kdWxlIg0KUHJpdmF0ZSBhIEFzIExvbmcNCg=="
no problem. However I want a longer string, this one for example (19000 chars, contains newlines):
.code_content = "QXR0cmlidXRlIFZCX05hbWUgPSAicHJvamVjdENvbXByZXNzb3IiDQonQ29tcHJlc3NvciBtb2R1bGUsIGNvbXByZXNzZXMgYSBsb2FkIG9mIGZpbGVz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ICAgIGZpbGxNb2R1bGUgPSByZXN1bHQNCkVuZCBJZg0KRW5kIFdpdGgNCkVuZCBGdW5jdGlvbg0KDQoNCg0K"
According to the docs, linefeed character vbCrLf should just make code on separate lines (what I want), so that shouldn't be causing the error.
However the length of string is not the problem either, as if .value = String(19000,"a") I have no issues. What's the cause of this error and how do I get around it?
Update
Something more re-createable:
Sub testAdd()
Dim codeStuff As codeModule
On Error Resume Next
Set codeStuff = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule).codeModule
'check for vbProj access
If Err.Number <> 0 Then MsgBox "Access to VBProject disabled": Exit Sub
On Error GoTo 0
'try to add code
With codeStuff
Dim i As Long
For i = 1 To 3
'i = 1 fails for me
On Error Resume Next
.InsertLines 1, exampleString(i) 'causes the error
Debug.Print "Case"; i; IIf(Err.Number = 0, " suceeded", " failed with err:" & Err.Number)
On Error GoTo -1
Next i
End With
ThisWorkbook.VBProject.VBComponents.Remove codeStuff.Parent
End Sub
Function exampleString(stringType As Long) As String
Dim result As String
Select Case stringType
Case 1 'lots of linefeed
Dim bit As Long
For bit = 1 To 19
result = result & """" & String(1000, "a") & """ & _" & vbCrLf
Next bit
result = result & """" & String(1000, "a") & """"
Case 2 'long string
result = String(20000, "a")
Case Else 'short string
result = String(100, "a")
End Select
exampleString = result
End Function
There is a limit of "prolonging" lines with _ at the end, try building your string part by part:
.code_content = "first part"
.code_content = .code_content & "second part"
I've created a Windows Form application. It is my understanding that you do not have to have a Sub Main() in a Windows Form app. However I'm getting this error when I build my project:
BC30420 'Sub Main' was not found in 'LoanCalculator.Module1'.
First of all I don't know why it's saying 'LoanCalculator.Module1'. Both my form and my class are named LoanCalculator.vb. When I started the project I started writing the code in the original module. Then I added a module, named it 'LoanCalculator' and moved what code I had written to that module and finished it there. I deleted the original module. Now it builds fine with the exception of this one error. Here's my code:
Imports System.Windows.Forms
Public Class LoanCalculator
Private Sub Calculate()
Dim str As String
Dim intLoanAmt As Integer
Dim intDown As Integer
Dim intFees As Integer
Dim intBalance As Integer
Dim dblIntsRate As Single
Dim intLoanTerm As Integer
Dim sngInterestPaid As Single
Dim intTermMonths As Integer
Dim dblMonthlyPmt As Integer
Dim intTotalPaid As Integer
Dim dblYon As Double
Dim dblXon As Double
Dim dblZon As Double
If Not CheckInput() Then
Return
End If
intLoanAmt = Convert.ToInt32(txtLoan.Text)
intFees = Convert.ToInt32(txtFees.Text)
intDown = Convert.ToInt32(txtDown.Text)
intBalance = Convert.ToInt32(intLoanAmt - intDown + intFees)
intLoanTerm = Convert.ToInt32(txtTerm.Text)
dblIntsRate = Convert.ToDouble(txtTerm.Text)
intTermMonths = intLoanTerm * 12
dblYon = dblIntsRate / 1200
dblXon = dblYon + 1
dblZon = Math.Pow(dblXon, intTermMonths) - 1
dblMonthlyPmt = (dblYon + (dblYon / dblZon)) * intBalance
intTotalPaid = dblMonthlyPmt * intTermMonths
sngInterestPaid = intTotalPaid - intBalance
str = "Loan balance =" & Space(11) & intBalance.ToString & vbCrLf
str = str & "Loan Term =" & Space(16) & intLoanTerm.ToString & " years" & vbCrLf
str = str & "Interest paid =" & Space(17) & intTotalPaid.ToString & vbCrLf
str = str & "Monthly payment =" & Space(5) & dblMonthlyPmt.ToString
lblResults.Text = str
End Sub
Private Function CheckInput() As Boolean
Dim strErr As String = ""
If txtLoan.Text.Length = 0 Then
strErr = "Enter loan amount" & vbCrLf
End If
If txtDown.Text.Length = 0 Then
strErr = strErr & "Enter down payment" & vbCrLf
End If
If txtInterest.Text.Length = 0 Then
strErr = strErr & "Enter interest rate" & vbCrLf
End If
If txtFees.Text.Length = 0 Then
strErr = strErr & "Enter fees" & vbCrLf
End If
If txtTerm.Text.Length = 0 Then
strErr = strErr & "Enter loan term" & vbCrLf
End If
If strErr.Length > 0 Then
MessageBox.Show(strErr)
Return False
Else
Return True
End If
End Function
End Class
How can I fix this?
In Access, using VBA, I want to use Application.Run and trap any errors. Unfortunately, Application.Run seems to hijack error trapping. Is there a way to fix this?
On Error Resume Next
Application.Run ...
I never get past Application.Run on an error, even if I specify On Error Resume Next or On Error GoTo ErrCatch. My error trapping setting is ignored.
If the procedure you're calling is inside your VBA project, then you can just call the procedure directly with:
Sub Foo()
'Application.Run "SomeProc"
SomeProc
End Sub
If you need to be able to call things dynamically by name, you could explore using classes and CallByName:
'In a standard module
Sub Foo()
Dim o as New ProcRunner
CallByName o, "SomeProc", VbMethod, args
End Sub
'In a class module called ProcRunner
Sub SomeProc()
DoSomethingHere
'Or, do something in a standard module
Module1.SomeOtherProc
End Sub
Or, you could write your own dynamic handler, along the lines of:
Sub AppRun(ProcName As String, ParamArray Args)
Select Case ProcName
Case "SomeProc"
SomeProc
Case "SomeFunc"
SomeFunc
End Select
End Sub
If you're calling procedures in another VBA project, you may need to add a reference to that project, depending upon the VBA host.
However, if you're using Application.Run because you're calling functions registered by a DLL or XLL, then you don't have any option other than to use Application.Run
There is a way to do this. We have a test harness in VBA and need a way of testing whether a procedure throws an error or not, giving a true/false result. We want True to indicate that an error was thrown, and False to indicate that no error was thrown. This only works for public procedures, but you can pass in a variable amount of arguments.
mIsErrorThrownDuringRunProcedure allows you to pass in a proc name and a varags list of arguments. It creates a new module, then writes another procedure to a the new module, calls that other procedure, and returns the result. The other procedure checks whether running the proc with the given args had any errors. When the dynamically created procedure is finished running, the new module is deleted.
An auxiliary function called mCreateCodeToExecute creates the code that is run from the new module to actually get the true/false result.
Public Function mIsErrorThrownDuringRunProcedure(pProcName As String, ParamArray pArgs() As Variant) As Boolean
Dim lVbComp As Object
Set lVbComp = ThisWorkbook.VBProject.VBComponents.Add(1)
Dim lProcNameToExecute As String
lProcNameToExecute = "mIsErrroRunDuringProcedure" & pProcName
Dim lCodeToExecute As String
Dim lNumArgs As Integer: lNumArgs = 0
Dim lArg As Variant
For Each lArg In pArgs
lNumArgs = lNumArgs + 1
Next
lCodeToExecute = mCreateCodeToExecute(pProcName, lProcNameToExecute, lNumArgs)
lVbComp.CodeModule.AddFromString lCodeToExecute
mIsErrorThrownDuringRunProcedure = Application.Run(lProcNameToExecute, pArgs)
ThisWorkbook.VBProject.VBComponents.Remove lVbComp
End Function
Private Function mCreateCodeToExecute(pProcName As String, lProcNameToExecute As String, numArgs As Integer)
Dim lCodeToExecute As String
lCodeToExecute = "Function " & lProcNameToExecute & "("
lCodeToExecute = lCodeToExecute & "ParamArray pArgs() As Variant) As Boolean" & vbCrLf
Dim lGoToLabel As String: lGoToLabel = "gtCodeHadError"
lCodeToExecute = lCodeToExecute & " On Error GoTo " & lGoToLabel & vbCrLf
lCodeToExecute = lCodeToExecute & " Call " & pProcName & "("
Dim lIndex As Integer
lIndex = 0
For lIndex = 0 To numArgs - 1
lCodeToExecute = lCodeToExecute & "pArgs(" & lIndex & "), "
lIndex = lIndex + 1
Next
Dim lCutOff As Integer: lCutOff = 2
If lIndex = 0 Then lCutOff = 1
lCodeToExecute = left(lCodeToExecute, Len(lCodeToExecute) - lCutOff)
If lCutOff = 2 Then lCodeToExecute = lCodeToExecute & ")"
lCodeToExecute = lCodeToExecute & vbCrLf & " " & lProcNameToExecute & "= False" & vbCrLf & " Exit Function"
lCodeToExecute = lCodeToExecute & vbCrLf & lGoToLabel & ":" & vbCrLf
lCodeToExecute = lCodeToExecute & " " & lProcNameToExecute & "= True"
lCodeToExecute = lCodeToExecute & vbCrLf & "End Function"
mCreateCodeToExecute = lCodeToExecute
End Function
References
How to run a string as a command in VBA