Results 1 to 3 of 3

Thread: Use system open with dialogue box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Posts
    174

    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

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Use system open with dialogue box

    Places this in a module

    vb Code:
    1. Public Declare Function GetOpenFileName Lib "comdlg32.dll" _
    2. Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    3.  
    4. Public Type OPENFILENAME
    5.     lStructSize As Long
    6.     hwndOwner As Long
    7.     hInstance As Long
    8.     lpstrFilter As String
    9.     lpstrCustomFilter As String
    10.     nMaxCustFilter As Long
    11.     nFilterIndex As Long
    12.     lpstrFile As String
    13.     nMaxFile As Long
    14.     lpstrFileTitle As String
    15.     nMaxFileTitle As Long
    16.     lpstrInitialDir As String
    17.     lpstrTitle As String
    18.     flags As Long
    19.     nFileOffset As Integer
    20.     nFileExtension As Integer
    21.     lpstrDefExt As String
    22.     lCustData As Long
    23.     lpfnHook As Long
    24.     lpTemplateName As String
    25. End Type
    26.  
    27. Public Function SelectFileOpenDialog()
    28. Dim strTemp, strTemp1, pathStr As String
    29. Dim i, n, j As Long
    30. Dim OpenFile As OPENFILENAME
    31. Dim lReturn As Long
    32. Dim sFilter As String
    33. Dim Fname As String
    34.  
    35. OpenFile.lStructSize = Len(OpenFile)
    36. sFilter = "Text Files (*.txt)" & Chr(0) & "*.TXT" & Chr(0)
    37. OpenFile.lpstrFilter = sFilter
    38. OpenFile.nFilterIndex = 1
    39. OpenFile.lpstrFile = String(257, 0)
    40. OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    41. OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    42. OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    43. OpenFile.lpstrInitialDir = "C:\"
    44. OpenFile.lpstrTitle = "Select File"
    45. OpenFile.flags = 0
    46.  
    47. lReturn = GetOpenFileName(OpenFile)
    48.  
    49. If lReturn = 0 Then
    50. MsgBox "You didn't select any file"
    51. Else
    52.  
    53. Fname = Trim$(OpenFile.lpstrFileTitle) ' copy the filename to "Fname"
    54.  
    55. n = FileLen(OpenFile.lpstrFile) 'length of the file
    56. End If
    57.  
    58. End Function

    Place this in the command button click

    vb Code:
    1. Private Sub Command1_Click()
    2.     SelectFileOpenDialog
    3. End Sub

    Please amend the code as per your requirements...
    Last edited by Siddharth Rout; Nov 7th, 2008 at 03:43 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Const SEE_MASK_INVOKEIDLIST = &HC
    2. Const SEE_MASK_NOCLOSEPROCESS = &H40
    3. Const SEE_MASK_FLAG_NO_UI = &H400
    4. Type SHELLEXECUTEINFO
    5.     cbSize As Long
    6.     fMask As Long
    7.     hwnd As Long
    8.     lpVerb As String
    9.     lpFile As String
    10.     lpParameters As String
    11.     lpDirectory As String
    12.     nShow As Long
    13.     hInstApp As Long
    14.     lpIDList As Long
    15.     lpClass As String
    16.     hkeyClass As Long
    17.     dwHotKey As Long
    18.     hIcon As Long
    19.     hProcess As Long
    20. End Type
    21. Private Declare Function ShellExecuteEx Lib "shell32.dll" (SEI As SHELLEXECUTEINFO) As Long
    22.  
    23. Sub openwith()
    24.     Dim SEI As SHELLEXECUTEINFO
    25.     Dim r As Long, filename As String
    26.     filename = "c:\test\123.txt"
    27.    
    28.     With SEI
    29.         'Set the structure's size
    30.         .cbSize = Len(SEI)
    31.         'Seet the mask
    32.         .fMask = SEE_MASK_NOCLOSEPROCESS Or _
    33.          SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
    34.         'Set the owner window
    35.         .hwnd = 0 ' or someform.hwnd
    36.         'Show the properties
    37.         .lpVerb = "openas"
    38.         'Set the filename
    39.         .lpFile = filename
    40.         .lpParameters = vbNullChar
    41.         .lpDirectory = vbNullChar
    42.         .nShow = 0
    43.         .hInstApp = 0
    44.         .lpIDList = 0
    45.     End With
    46.     r = ShellExecuteEx(SEI)
    47. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width