Results 1 to 12 of 12

Thread: Opening a file

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    34

    Opening a file

    Hiya
    Need a bit of help with this code. What I'am attempting to do is take the file selected from my open dialog box and open it in that format.

    1 With cdg
    2 .InitDir = "N:\A2 SUBJECTS" '*
    3 .Filter = "All files|*.*|Image files|*.gif*.bmp;*.png;*.bmp|Text files|*.txt;*.doc;*.docx|"
    4 .Flags = cdlOFNHideReadOnly
    5 .Flags = cdlOFNFileMustExist
    6 .Flags = cdlOFNOverwritePrompt
    7 .DialogTitle = "Open the document you wish to edit"
    8 .ShowOpen
    9 End With


    What I have so far as a solution really dont know how to approach this 1

    Open cdg.FileName For Input As #1

  2. #2

  3. #3
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Opening a file

    opening a text file or picture file requires different components... you cannot open a picture in a textbox for example

    like rhinobull said you can launch it with its default program with the shellexecute api

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    34

    Re: Opening a file

    I have had a look but from what I can see none of the posts really explain how to open a file name stored within a variable as I have more than one file and shell requires a pathname I cannot use it can someone walk me through it please

  5. #5
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Opening a file

    here plinkster take a look at this i just made it i tried to explain as i went thru the code... tell me if you have any questions look at my attachment! (made it just for you)

    if you want you could do one that detects if its a txt file or picture but i dont feel like doing all that coding.. look at my example and you will understand how it works i used common dialog to open and save good luck
    Attached Files Attached Files

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Opening a file

    Quote Originally Posted by Plinkster View Post
    ... shell requires a pathname I cannot use it
    You are using the common dialog for a user to select from. That dialog does return the full path & file name of the selected file(s). If the example posted in previous reply isn't what you are looking for, I think it is time to provide a bit more information and/or code that you are now using.

    Edited: Oh, and the 1st two times you set the .Flags member, are obsolete since the last one is the only one that is saved. Try this instead:
    .Flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
    cdlOFNOverwritePrompt is only appropriate if calling ShowSave, not ShowOpen
    Last edited by LaVolpe; Nov 25th, 2011 at 02:15 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    34

    Re: Opening a file

    Thanks guys you have been a lot of help sorry for my late reply I've been rather busy. Max187 thanks again for going to this amount of effort, I've had a look and it does answer a lot of questions however the file is opened in Vb is there a way to open it in word for example?
    Thanks again I appreciate it

  8. #8
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Opening a file

    yes there is try shell command

    Code:
    Shell Chr(34) & "C:\Windows\system32\notepad.exe" & Chr(34) & " " & Chr(34) & StringFileName & Chr(34), vbMaximizedFocus
    '"C:\Windows\system32\notepad.exe" instead of this put your word.exe location "C:\Windows\system32\word.exe" (i dont know if thats the right location just a guess)
    'and at strfilename put you "C:\FoldersNamehere\Filenamehere.txt" so it would look like this
    'Shell Chr(34) & "C:\Windows\system32\word.exe" & Chr(34) & " " & Chr(34) & "C:\FoldersNamehere\Filenamehere.txt" & Chr(34), vbMaximizedFocus

  9. #9
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Opening a file

    i will give you this project of mine still got things to adjust and add but try this its a drag n drop file that opens file with certain program... you can change or add programs tell me what you think
    Attached Files Attached Files

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    34

    Re: Opening a file

    thanks this is definitely what I need I'll look over your program tonight and let you know how I get on

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    34

    Re: Opening a file

    The program is looking nice still some rough edges but I think thats mostly because my Visual basic is screwing up by not loading some of the files that you have uploaded. However have you thought about implementing another way of opening your files as well as in windows live gallery?

    I have one more question however is there any method of enabling the file selected with a common dialog box to be opened with its default program because as of now I have to specify the actual program that I wish to open it with which kind of defeats the purpose of the dialog box, Thanks again you've been a great help.

    P.S the directory to open Microsoft Word is ...
    Code:
    Shell Chr(34) & "C:\Program files\Microsoft Office\Office14\WINWORD.EXE" & Chr(34) & " " & Chr(34) & cdg.FileName & Chr(34), vbMaximizedFocus

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Opening a file

    There is indeed, using the ShellExecute API.

    There is an explanation and example in the "Working with Other Programs" section of our Classic VB FAQs (in the FAQ forum)

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