OberonPlace.com Forums

OberonPlace.com Forums (http://forum.oberonplace.com/index.php)
-   CorelDRAW/Corel DESIGNER VBA (http://forum.oberonplace.com/forumdisplay.php?f=16)
-   -   Open file dialog (http://forum.oberonplace.com/showthread.php?t=7077)

ager 26-07-2010 21:13

Open file dialog
 
I’m trying to create Openfile dialog in VBA which allow to user select multifiles like in Excel VBA: http://img840.imageshack.us/img840/1...ltiselect2.png.
It perform quite simple in Excel with function Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", Title:="Get File", MultiSelect:=True). But in CorelDraw I know only one way to do this:
Code:

Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "All Files|*.*"
objDialog.flags = &H200
objDialog.FilterIndex = 1
objDialog.InitialDir = "C:\My Documents\"
intFileCount = objDialog.ShowOpen

And old dialog window appears that do not support long file names: http://img153.imageshack.us/img153/7...ltiselect1.png
The question is how to create normal Open-dialog window in CorelDraw VBA like in Excel?
Thank you!

shelbym 26-07-2010 21:20

GetFileBox
 
Try using the CorelScriptTools.GetFileBox. Here is a very simple example:
Code:

Sub MyFolderDialog()
    Dim sFileName As String
   
    sFileName = CorelScriptTools.GetFileBox("All Files (*.*)|*.*", "Select a file", 0, "c:\Temp\*.*")
End Sub

Best of luck,

-Shelby

ager 26-07-2010 21:47

Re: GetFileBox
 
Yes, it's simple, but it don't allow to select several files. I want to get array of filepathes, not a one string. May be I need WinAPI but it incomprehensible thing for me :)

runflacruiser 26-07-2010 22:19

Hi.
Try a listbox.
Use getFileBox for the folder string.
Create a form and populate listbox with the files in a folder after selecting path.\

Then you'll have the folder path string and a variant array of as many files as you select.

-John

shelbym 26-07-2010 23:57

MultiSelect
 
Alright here is a way to do the MultiSelect, returned as a Collection. You could modify to an array if you like. :-) Hopefully it does the trick for you.
Code:

Public Const OFN_ALLOWMULTISELECT = &H200&
Public Const OFN_EXPLORER = &H80000
Public Const OFN_FILEMUSTEXIST = &H1000&
Public Const OFN_HIDEREADONLY = &H4&
Public Const OFN_PATHMUSTEXIST = &H800&

Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As Long

Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Private Sub ShowFileOpenDialog(ByRef FileList As Collection)
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim FileDir As String
    Dim FilePos As Long
    Dim PrevFilePos As Long

    With OpenFile
        .lStructSize = Len(OpenFile)
        .hwndOwner = 0
        .hInstance = 0
        .lpstrFilter = "CDR - CorelDRAW (*.cdr)" + Chr(0) + "*.cdr" + _
            Chr(0) + "All Files (*.*)" + Chr(0) + "*.*" + Chr(0) + Chr(0)
        .nFilterIndex = 1
        .lpstrFile = String(4096, 0)
        .nMaxFile = Len(.lpstrFile) - 1
        .lpstrFileTitle = .lpstrFile
        .nMaxFileTitle = .nMaxFile
        .lpstrInitialDir = "c:\"
        .lpstrTitle = "Open Drawings"
        .flags = OFN_HIDEREADONLY + _
            OFN_PATHMUSTEXIST + _
            OFN_FILEMUSTEXIST + _
            OFN_ALLOWMULTISELECT + _
            OFN_EXPLORER
        lReturn = GetOpenFileName(OpenFile)
        If lReturn <> 0 Then
            FilePos = InStr(1, .lpstrFile, Chr(0))
            If Mid(.lpstrFile, FilePos + 1, 1) = Chr(0) Then
                FileList.Add .lpstrFile
            Else
                FileDir = Mid(.lpstrFile, 1, FilePos - 1)
                Do While True
                    PrevFilePos = FilePos
                    FilePos = InStr(PrevFilePos + 1, .lpstrFile, Chr(0))
                    If FilePos - PrevFilePos > 1 Then
                        FileList.Add FileDir + "\" + _
                            Mid(.lpstrFile, PrevFilePos + 1, _
                                FilePos - PrevFilePos - 1)
                    Else
                        Exit Do
                    End If
                Loop
            End If
        End If
    End With
End Sub

Sub SelectFiles()
    Dim colFileList As New Collection
    Dim lngI As Long
    Dim strOutput As String

    ShowFileOpenDialog colFileList
    With colFileList
        If .Count > 0 Then
            strOutput = "The following files were selected:" + vbCrLf
            For lngI = 1 To .Count
                strOutput = strOutput + .Item(lngI) + vbCrLf
            Next
            MsgBox strOutput
        Else
            MsgBox "No files were selected!"
        End If
    End With
End Sub

Best of luck,

-Shelby

ager 27-07-2010 00:41

Thank you, Shelbym! That's fantastic! :crazyjump This is a very thing I want! Of course, in order to create array I must just add in your code:
Code:

....
Dim arr
.....
arr=Split(strOutput, Chr(10))

The idea of Runflacruiser is also works but it's not so convenient

runflacruiser 27-07-2010 09:17

Hi.

Very Awesome Shelby!

-John

ager 11-03-2012 23:42

Modal mode
 
A small question to Shelby: how to set modal mode of ShowFileOpenDialog window (ShowModal=true)?
Thank you!

shelbym 15-03-2012 23:16

ShowFileOpenDialog Modal
 
You just need to change the following line:
Code:

.hwndOwner = 0
to
Code:

.hwndOwner = AppWindow.Handle
Hope that helps,

-Shelby

ager 24-08-2016 05:49

x64
 
A question to Shelby again!
How to make working version of this code for CorelDRAW 64-bit? I try to use PtrSafe and LongPtr, but it does not help.


All times are GMT -5. The time now is 10:16.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com