Author Topic: VBScript OpenFileDialog  (Read 18006 times)

MatthiasVDE

  • EA User
  • **
  • Posts: 196
  • Karma: +1/-0
    • View Profile
VBScript OpenFileDialog
« on: January 16, 2019, 06:35:44 pm »
I'm trying to create an openFileDialog in VBScript, but it gave an error?
Code: [Select]
dim openFileDialog
set openFileDialog = CreateObject("MSComDlg.CommonDialog")

Arshad

  • EA User
  • **
  • Posts: 285
  • Karma: +19/-1
    • View Profile
Re: VBScript OpenFileDialog
« Reply #1 on: January 16, 2019, 06:47:25 pm »
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
wscript.echo sFileSelected

or can try below snippet from sparx ( EAScriptLib)

Code: [Select]
'
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
« Last Edit: January 16, 2019, 06:59:54 pm by Arshad »

MatthiasVDE

  • EA User
  • **
  • Posts: 196
  • Karma: +1/-0
    • View Profile
Re: VBScript OpenFileDialog
« Reply #2 on: January 17, 2019, 12:50:58 am »
I always get 'ActiveX component can't create object: 'MSComDlg.CommonDialog'.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBScript OpenFileDialog
« Reply #3 on: January 17, 2019, 01:51:01 am »
Try project.GetFileNameDialog
e.g:
Code: [Select]
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:

Code: [Select]
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

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: VBScript OpenFileDialog
« Reply #4 on: January 17, 2019, 06:56:59 pm »
Code: [Select]
me is likely a structure. This is example code. Just take a string variable since GetFileNameDialog returns a string (see the help).

q.

Arshad

  • EA User
  • **
  • Posts: 285
  • Karma: +19/-1
    • View Profile
Re: VBScript OpenFileDialog
« Reply #5 on: January 17, 2019, 07:08:20 pm »
I always get 'ActiveX component can't create object: 'MSComDlg.CommonDialog'.

Did you try the below snippet?

Code: [Select]
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


MatthiasVDE

  • EA User
  • **
  • Posts: 196
  • Karma: +1/-0
    • View Profile
Re: VBScript OpenFileDialog
« Reply #6 on: January 17, 2019, 07:28:03 pm »
It works, thx!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBScript OpenFileDialog
« Reply #7 on: January 17, 2019, 07:50:21 pm »
Code: [Select]
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

Geert