Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: MatthiasVDE on January 16, 2019, 06:35:44 pm
-
I'm trying to create an openFileDialog in VBScript, but it gave an error?
dim openFileDialog
set openFileDialog = CreateObject("MSComDlg.CommonDialog")
-
Hi
Try Below VBScript snippet
Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
wscript.echo sFileSelected
or can try below snippet from sparx ( EAScriptLib)
'
dim OF_FILEMUSTEXIST
dim DLG_MAXFILESIZE
OF_FILEMUSTEXIST = &H1000
DLG_MAXFILESIZE = 260
function DLGOpenFile( filterString, defaultFilterIndex )
if len(filterString) = 0 then
filterString = "All Files (*.*)|*.*"
defaultFilterIndex = 1
end if
' Create new CommonDialog object
dim openFileDialog
set openFileDialog = CreateObject("MSComDlg.CommonDialog")
' Set appropriate flags
openFileDialog.MaxFileSize = DLG_MAXFILESIZE
openFileDialog.Filter = filterString
openFileDialog.FilterIndex = defaultFilterIndex
openFileDialog.Flags = OF_FILEMUSTEXIST
' Show the dialog and return the selected file name
openFileDialog.ShowOpen()
DLGOpenFile = openFileDialog.FileName
end function
You can check in Sparx script library for more details.
To Access you need to enable EAScriptLib in MDG Technologies
HTH
Arshad
-
I always get 'ActiveX component can't create object: 'MSComDlg.CommonDialog'.
-
Try project.GetFileNameDialog
e.g:
dim project
set project = Repository.GetProjectInterface()
me.FileName = project.GetFileNameDialog ("", "Excel Files|*.xls;*.xlsx;*.xlsm|Excel Templates|*.xlt;*.xltx;*.xltm", 1, 0 ,"", 0) 'save as with overwrite prompt: OFN_OVERWRITEPROMPT
Other alternative:
Function ChooseFile (ByVal initialDir, filter)
dim shel, fso, tempdir, tempfile, powershellfile, powershellOutputFile,psScript, textFile
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
tempDir = shell.ExpandEnvironmentStrings("%TEMP%")
tempFile = tempDir & "\" & fso.GetTempName
' temporary powershell script file to be invoked
powershellFile = tempFile & ".ps1"
' temporary file to store standard output from command
powershellOutputFile = tempFile & ".txt"
'if the filter is empty we use all files
if len(filter) = 0 then
filter = "All Files (*.*)|*.*"
end if
'input script
psScript = psScript & "[System.Reflection.Assembly]::LoadWithPartialName(""System.windows.forms"") | Out-Null" & vbCRLF
psScript = psScript & "$dlg = New-Object System.Windows.Forms.OpenFileDialog" & vbCRLF
psScript = psScript & "$dlg.initialDirectory = """ &initialDir & """" & vbCRLF
'psScript = psScript & "$dlg.filter = ""ZIP files|*.zip|Text Documents|*.txt|Shell Scripts|*.*sh|All Files|*.*""" & vbCRLF
psScript = psScript & "$dlg.filter = """ & filter & """" & vbCRLF
' filter index 4 would show all files by default
' filter index 1 would should zip files by default
psScript = psScript & "$dlg.FilterIndex = 1" & vbCRLF
psScript = psScript & "$dlg.Title = ""Select a file""" & vbCRLF
psScript = psScript & "$dlg.ShowHelp = $True" & vbCRLF
psScript = psScript & "$dlg.ShowDialog() | Out-Null" & vbCRLF
psScript = psScript & "Set-Content """ &powershellOutputFile & """ $dlg.FileName" & vbCRLF
'MsgBox psScript
Set textFile = fso.CreateTextFile(powershellFile, True)
textFile.WriteLine(psScript)
textFile.Close
Set textFile = Nothing
' objShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn])
' 0 Hide the window and activate another window.
' bWaitOnReturn set to TRUE - indicating script should wait for the program
' to finish executing before continuing to the next statement
Dim appCmd
appCmd = "powershell -ExecutionPolicy unrestricted &'" & powershellFile & "'"
'MsgBox appCmd
shell.Run appCmd, 0, TRUE
' open file for reading, do not create if missing, using system default format
Set textFile = fso.OpenTextFile(powershellOutputFile, 1, 0, -2)
ChooseFile = textFile.ReadLine
textFile.Close
Set textFile = Nothing
fso.DeleteFile(powershellFile)
fso.DeleteFile(powershellOutputFile)
End Function
Geert
-
me
is likely a structure. This is example code. Just take a string variable since GetFileNameDialog returns a string (see the help).
q.
-
I always get 'ActiveX component can't create object: 'MSComDlg.CommonDialog'.
Did you try the below snippet?
Hi
Try Below VBScript snippet
Code: [Select]
Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
sFileSelected = oExec.StdOut.ReadLine
-
It works, thx!
-
me
is likely a structure. This is example code. Just take a string variable since GetFileNameDialog returns a string (see the help).
q.
I took the example from an excel file wrapper class: https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/ExcelFile.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/ExcelFile.vbs)
Geert