Results 1 to 17 of 17

Thread: [RESOLVED] Opening file

  1. #1

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Resolved [RESOLVED] Opening file

    I was just wondering,
    how can I right-click a file and open it with my VB program?
    For example a txt file, and place it in a testbox.

    thnx

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Opening file

    Right click a file from where? A listbox or something from within your program or from Windows Explorer?

  3. #3

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    just from windows explorer

  4. #4

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    doesn't VB have a standard option for that?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Opening file

    Quote Originally Posted by Private_sub
    doesn't VB have a standard option for that?
    You are probably talking about the Common Dialog control, but I've never used a right mouse click with that before.

    If you are referring to the Common Dialog, then a quick example would be pretty easy to whip up. Does this work for you?
    VB Code:
    1. Private Sub Command1_Click()
    2. With CommonDialog1
    3.    .CancelError = False
    4.    .InitDir = "c:\"
    5.    .Filter = "Text Files (*.txt)|*.txt|"
    6.    .ShowOpen
    7. End With
    8. Open CommonDialog1.FileName For Input As #1
    9.     Text1.Text = Input(LOF(1), 1)
    10. Close #1
    11.  
    12. End Sub

  6. #6

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    i know the common dialog thing.
    what i mean is right-clicking a file(in Windows Explorer), then Open with... and then my .exe
    if you don't know how it works, please don't waste any time on it.

  7. #7
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Opening file

    I think he means a Context Menu in explorer so that when he clicks a file it can open it in hes program...

    in your form_load you can do... msgbox command$ & " - thats the path of this file"
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  8. #8
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Opening file

    Do you mean as in (In explorer) Tools > Folder Options > File Types > Advanced > New. ?
    Last edited by schoolbusdriver; Dec 12th, 2006 at 01:09 PM.

  9. #9

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    some1uk03's code sort of works, but I get "C:\file.txt" instead of C:\file.txt
    So when I try to load it into my form, it doesn't work because of the ""s

  10. #10

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    I can use this:
    var = Replace(var,""", "")
    But the """ is not working, i've done it before with something like ascii.
    I forgot how to use it, when i know that, it's gonna work.

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

    Re: Opening file

    to do what you are asking you will need to edit the registry for the windows shell to add your program to the types of files you want to associate to your program
    the filetype keys are in the HKCR section of the registry,
    make sure to back up the registry before you edit it
    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

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

    Re: Opening file

    var = Replace(var,""", "")
    try
    VB Code:
    1. var = Replace(var,"""", "")
    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

  13. #13

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    That sounds pretty cool, but how can i add new filetypes to the registry?
    I don't get all the binary stuff

  14. #14

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    Thanx Westconn1!
    The """" works great!

  15. #15

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: Opening file

    If anyone is interested, here is the code I made:
    VB Code:
    1. Dim fileLocation As String
    2. Dim orLoc As String
    3. Private Sub Form_Load()
    4. If Not Command$ <> vbNullString Then
    5. Text1.Text = "Please begin here"
    6. Else
    7. Text1.Text = "An error has occured, please check if your file is a working text-file."
    8. End If
    9. On Error Resume Next
    10. orLoc = Command$
    11. fileLocation = Replace(orLoc, """", "")
    12. Open fileLocation For Input As #1
    13.     Text1.Text = Input(LOF(1), 1)
    14. Close #1
    15. End Sub

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

    Re: [RESOLVED] Opening file

    using on error resume next can cause probelms with debugging, better to use an error handler
    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

  17. #17

    Thread Starter
    Hyperactive Member Private_sub's Avatar
    Join Date
    Nov 2005
    Location
    +31
    Posts
    368

    Re: [RESOLVED] Opening file

    Ok, thanx.
    I used this for the standard icon etc.:
    http://www.vbcodemagician.dk/tips/files_association.htm

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