Use system open with dialogue box
Hi All,
Hope u all are in fine tune. Friends I am just trying to do some R & D with the system dialogue boxes. can anyone help me to know how I can use the system Open With Dialogue Box to open a file in the VB Exe.
For example:
Suppose I am having a form with a textbox or a RTFBox. Now I will click on a text file on the desktop and indicate it to open in my form using the system Open With Dialogue Box. Is this possible. If Yes the how I can do it. Please anyone give me some idea.
Thanx in advance
Arghya
Re: Use system open with dialogue box
Places this in a module
vb Code:
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
Public Function SelectFileOpenDialog()
Dim strTemp, strTemp1, pathStr As String
Dim i, n, j As Long
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
Dim Fname As String
OpenFile.lStructSize = Len(OpenFile)
sFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Select File"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "You didn't select any file"
Else
Fname = Trim$(OpenFile.lpstrFileTitle) ' copy the filename to "Fname"
n = FileLen(OpenFile.lpstrFile) 'length of the file
End If
End Function
Place this in the command button click
vb Code:
Private Sub Command1_Click()
SelectFileOpenDialog
End Sub
Please amend the code as per your requirements...
Re: Use system open with dialogue box
to display the open with dialog is the default action for shellexecute if the file type is unregistered, to force the dialog to display you need to use shellexecuteex like this
vb Code:
Const SEE_MASK_INVOKEIDLIST = &HC
Const SEE_MASK_NOCLOSEPROCESS = &H40
Const SEE_MASK_FLAG_NO_UI = &H400
Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Private Declare Function ShellExecuteEx Lib "shell32.dll" (SEI As SHELLEXECUTEINFO) As Long
Sub openwith()
Dim SEI As SHELLEXECUTEINFO
Dim r As Long, filename As String
filename = "c:\test\123.txt"
With SEI
'Set the structure's size
.cbSize = Len(SEI)
'Seet the mask
.fMask = SEE_MASK_NOCLOSEPROCESS Or _
SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
'Set the owner window
.hwnd = 0 ' or someform.hwnd
'Show the properties
.lpVerb = "openas"
'Set the filename
.lpFile = filename
.lpParameters = vbNullChar
.lpDirectory = vbNullChar
.nShow = 0
.hInstApp = 0
.lpIDList = 0
End With
r = ShellExecuteEx(SEI)
End Sub