Page 1 of 1 1
Topic Options
#78351 - 2001-09-04 08:09 AM WMI/WSH port - Open and browse for dialog...
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Shawn/Bryce/Lonkero/DOC/Whoever,

I saw some code last night that looked really cool, which would be pretty sweet under Kix. I am kinda lost, anybody care to take a crack at it? As you may notice, it is a take off from a dialog that was "hacked" to work under VBA and specifically Excel. Anyway, here goes..

code:

break on cls
; -- code idea is from - http://www.BMSLtd.co.uk - Stephen Bullen
; -- Port over to KIX by Kent Dyer
;-------------------------------------------------------
; Global Declaration Section
;-------------------------------------------------------
;data buffer for the GetOpenFileName and GetSaveFileName functions

;VARTYPE($variable)
;2 Integer
;3 Long integer
;8 String
;11 Boolean

function $OpenFilename()
VARTYPE($lStructSize) = 3
VARTYPE($hwndOwner) = 3
VARTYPE($hInstance) = 3
VARTYPE($lpstrFilter) = 8
VARTYPE($lpstrCustomFilter) = 3
VARTYPE($nMaxCustFilter) = 3
VARTYPE($iFilterIndex) = 3
VARTYPE($lpstrFile) = 8
VARTYPE($nMaxFile) = 3
VARTYPE($lpstrFileTitle) = 8
VARTYPE($nMaxFileTitle) = 3
VARTYPE($lpstrInitialDir) = 8
VARTYPE($lpstrTitle) = 8
VARTYPE($flags) = 3
VARTYPE($nFileOffset) = 2
VARTYPE($nFileExtension) = 2
VARTYPE($lpstrDefExt) = 8
VARTYPE($lCustData) = 3
VARTYPE($lpfnHook) = 3
VARTYPE($lpTemplateName) = 8
Endfunction

Function $GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" VARTYPE(($pOpenfilename As $OpenFilename)) = 3 Endfunction
Function $FindWindow Lib "user32" Alias "FindWindowA" VARTYPE(($ByVal VARTYPE($lpClassName) = 8, $ByVal VARTYPE($lpWindowName) = 8)) = 3 Endfunction

$OFN_READONLY = &1
$OFN_OVERWRITEPROMPT = &2
$OFN_HIDEREADONLY = &4
$OFN_NOCHANGEDIR = &8
$OFN_SHOWHELP = &10
$OFN_ENABLEHOOK = &20
$OFN_ENABLETEMPLATE = &40
$OFN_ENABLETEMPLATEHANDLE = &80
$OFN_NOVALIDATE = &100
$OFN_ALLOWMULTISELECT = &200
$OFN_EXTENSIONDIFFERENT = &400
$OFN_PATHMUSTEXIST = &800
$OFN_FILEMUSTEXIST = &1000
$OFN_CREATEPROMPT = &2000
$OFN_SHAREAWARE = &4000
$OFN_NOREADONLYRETURN = &8000
$OFN_NOTESTFILECREATE = &10000
$OFN_NONETWORKBUTTON = &20000
$OFN_NOLONGNAMES = &40000 ; force no long names for 4.x modules
$OFN_EXPLORER = &80000 ; new look commdlg
$OFN_NODEREFERENCELINKS = &100000
$OFN_LONGNAMES = &200000 ; force long names for 3.x modules
$OFN_SHAREFALLTHROUGH = 2
$OFN_SHARENOWARN = 1
$OFN_SHAREWARN = 0

Function $OpenCommDlgTest()

Dim sFile As String

; Calls the OpenCommDlg function (listed above) and places
; return value in sFile variable
$sFile = $funOpenCommDlg("Excel Files (*.xls)|*.xls|All files (*.*)|*.*", "Open Excel File", "c:\", "*.xls", False)
MessageBox sFile

EndFunction

;-------------------------------------------------------
; Open Common Dialog Function
;-------------------------------------------------------
Function $funOpenCommDlg($ByVal VARTYPE($sFilter) = 8, $ByVal VARTYPE($sDlgTitle) = 8, $ByVal VARTYPE($sDir) = 8, $ByVal VARTYPE($sDefExt) = 8, $ByVal VARTYPE($bMustExist) = 11) = 8

Dim VARTYPE($sFullName) = 8, VARTYPE($sFileName) = 8
Dim VARTYPE($lResult) = 3, VARTYPE($lFlags) = 3, VARTYPE($i) = 2
Dim uFileDlgData As OpenFilename

; Define the filter string, converting all "|" to nulls
$sFilter = $funSubstitute(sFilter, "|", Chr$(0))

; Allocate string space for the returned strings.
$sFullName = Space$(254)
$sFileName = Space$(254)

$lFlags = $OFN_HIDEREADONLY Or $OFN_NOCHANGEDIR

If $bMustExist
$lFlags = $lFlags Or $OFN_FILEMUSTEXIST
endif

; Set up the data structure before you call the GetOpenFilename
While uFileDlgData
.hwndOwner = FindWindow("MAIN", Application.Caption)
.lpstrFilter = $sFilter
.iFilterIndex = 1
.lpstrFile = $sFullName & Chr$(0)
.nMaxFile = Len($sFullName) + 1
.lpstrFileTitle = $sFileName & Chr$(0)
.nMaxFileTitle = Len($sFileName) + 1
.lpstrTitle = $sDlgTitle
.flags = $lFlags
.lpstrDefExt = $sDefExt
.hInstance = 0
.lpstrCustomFilter = 0&
.nMaxCustFilter = 0
.lpstrInitialDir = $sDir
.nFileOffset = 0
.nFileExtension = 0
.lCustData = 0
.lpfnHook = 0
.lpTemplateName = ""
.lStructSize = Len($uFileDlgData)
Loop

; This will pass the desired data structure to the Windows API,
; which will in turn use it to display the Open Dialog form.
$lResult = GetOpenFileName($uFileDlgData)

; Return the file selected
If $lResult = 0
$funOpenCommDlg = ""
Else
$funOpenCommDlg = RTrim(uFileDlgData.lpstrFile)
EndIf

EndFunction


Function $funSubstitute($ByVal VARTYPE($sString) = 8, $ByVal VARTYPE($sFind) = 8, $ByVal VARTYPE($sReplace) = 8)

Dim VARTYPE($i) = 2, VARTYPE($sTmp) = 8

For $i = 1 To Len($sString)
If Mid($sString, $i, 1) = "|"
$sTmp = $sTmp & Chr$(0)
Else
$sTmp = $sTmp & Mid($sString, i, 1)
EndIf
Next

$funSubstitute = $sTmp

EndFunction


Thanks!

- Kent

_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#78352 - 2001-09-04 03:49 PM Re: WMI/WSH port - Open and browse for dialog...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Kent,

Here's my take on it ... It uses the MSComDlg.CommonDialog ActiveX control that - I think - gets installed and licensed with Office97+. Not everyone has this control exposed at the COM Automation level. Maybe that's why this guy went "under the covers" and called it throught the Windows API ...

code:

break on


$Filename = OpenCommonDialog("Excel Files (*.xls)|*.xls|All files (*.*)|*.*", "Open Excel File", "c:\", "*.xls", 0)


?"file = " $Filename


exit


function OpenCommonDialog($Filter,$Title,$Directory,$Extension,$FileMustExist)
$OpenCommonDialog = ""
$Dialog = CreateObject("MSComDlg.CommonDialog")
if $Dialog
$Dialog.Filter = $Filter
$Dialog.DialogTitle = $Title
$Dialog.InitDir = $Directory
$Dialog.Extension = $Extention
$Dialog.MaxFileSize = 128
If $FileMustExist
$Dialog.Flags = 4096
EndIf
$Dialog.ShowOpen()
$OpenCommonDialog = $Dialog.Filename
endif
endfunction


-Shawn

Top
#78353 - 2001-09-04 09:57 PM Re: WMI/WSH port - Open and browse for dialog...
Alex.H Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 406
Loc: France
Right, even with ms Access you can't use it. You must have Visual Basic installed to use it elsewhere. API is more easier to use it.

Well, that's a nice one (using API through Kix, that would be kinda neat!! )

_________________________
? getobject(Kixtart.org.Signature)

Top
Page 1 of 1 1


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.078 seconds in which 0.061 seconds were spent on a total of 12 queries. Zlib compression enabled.