Results 1 to 17 of 17

Thread: [RESOLVED] Open a file with wrong extension

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Resolved [RESOLVED] Open a file with wrong extension

    ShellExecute works great, but the problem I have is that the file doesn't have the correct extension.

    As an example, let's say I want to open "file_id.diz" with Word. I specifically do not want to rename it to "file_id.doc", even temporarily.

    What is the best way to accomplish this?

  2. #2
    New Member RealityRipple's Avatar
    Join Date
    Apr 2007
    Location
    Nipomo, California
    Posts
    4

    Re: Open a file with wrong extension

    Shell Word with the file as its Paramaters, or set .diz to a MSWord filetype.

    vb Code:
    1. WordPath = "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe"
    2. Shell WordPath & " """ & ToOpen & """", vbNormalFocus
    Something like that?
    Last edited by RealityRipple; Apr 17th, 2007 at 04:01 AM.

  3. #3

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    I won't know what the default program is to open the file, and the extension is associated with my program so I can't change the association in Windows.

    ShellExecute is perfect for this except that it needs the filename to be the correct extension.

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Open a file with wrong extension

    How about? just an alternative.

    Code:
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    
    
    Private Sub Command1_Click()
        Set oWord = New Word.Application
        Set oDoc = New Word.Document
        
        oDoc.Application.Documents.Open "c:\file_id.diz"
      
    End Sub
    
    
    Private Sub Form_Unload(Cancel As Integer)
        oWord.Quit False
        Set oDoc = Nothing
        Set oWord = Nothing
    End Sub

  5. #5

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    To clarify with a different example, let's say they're mpg audio files, (they aren't, but it gives you an idea) named with a custom extension to associate them with my program.

    I want my program to be able to open the mpg file using whatever the computer normally uses to play mpg files. It could be WinAmp, Windows Media Player, RealPlayer, or anything.

    That's the beauty of ShellExecute: I don't have to worry about what the program actually is, because it just lets the OS sort it out.

    Can I somehow hack into the ShellExecute command line without actually executing it? Or is there another way?

    I'm perfectly comfortable looking up the file extension in the registry (under ClassesRoot) and generating the command line manually. But from what I can tell that won't work reliably under Vista. So I'm hoping for an alternative.

    Also, these files range in size anywhere from 25k to 25 megs, and they could be burned to a CD. That's why I don't want to rename or copy them. (It would take too long to copy 25 megs from the CD to the hard drive, and you can't rename files on a CD-R disc.)

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

    Re: Open a file with wrong extension

    Where do these files come from? How do they get created?

  7. #7

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    Quote Originally Posted by Hack
    Where do these files come from? How do they get created?
    They may or may not be created from scratch by the user, and can be any file he can create using any program he has on his computer.

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

    Re: Open a file with wrong extension

    Ok.....that wasn't what I was expecting to hear , but it does lead to another question.

    If these are user created files, how do they get the extention .diz? Wouldn't user created files have their default extension?

  9. #9

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    Quote Originally Posted by Hack
    Ok.....that wasn't what I was expecting to hear , but it does lead to another question.

    If these are user created files, how do they get the extention .diz? Wouldn't user created files have their default extension?
    heh. Think of it as a backup, if that helps. Or a version control program.

    Note that the .diz and .mp3 examples are not the actual files; they're just examples. (file_id.diz is an oldschool filename; I used it for nostalgia.) The actual files are from another program. Well, set of programs.

    It's fairly involved, to be honest. Let's just say that I need to be able to execute a program the same way ShellExecute does, but the file has the wrong extension.

    Given a 25 meg file burned to a CD-R as MyFile.abc, I need to ShellExecute it as MyFile.xyz without disturbing the OS's existing *.abc association. What is the best way to accomplish this?

    Note that when it gets executed/opened, whatever program just opened it should see it as MyFile.abc, not MyFile.xyz.

    For example, if you create a text file and name it file_id.diz, then right-click it and "Open With" notepad, notepad recognizes the file as named *.diz, not *.txt.
    Last edited by Ellis Dee; Apr 17th, 2007 at 07:10 AM.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Open a file with wrong extension

    1) You MUST save the original extension somewhere. A filetype that's unknown has no app associated with it. (Every possible 3-letter combination probably has an app associated with it, but that's beside the point.)

    2) Use the FindExecutable API to find the app associated with the original extension on that computer. Shell the app with the file as the param.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  11. #11

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    Quote Originally Posted by Al42
    Use the FindExecutable API to find the app associated with the original extension on that computer. Shell the app with the file as the param.
    Sweet, this is exactly what I was looking for. Sadly, I still need a little help. Could you or someone else help flesh out this function?
    Code:
    Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    
    ' pstrFile contains full path and filename ("c:\File_id.diz")
    ' pstrExt contains original extension ("txt")
    Public Sub RunFile(pstrFile As String, pstrExt As String)
        Dim strFake As String
    
       strFake = Left(pstrFile, InstrRev(pstrFile, ".")) & pstrExt
       ' Now what?
    End Sub
    Last edited by Ellis Dee; Apr 17th, 2007 at 10:15 PM. Reason: flesh, not flush

  12. #12
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Open a file with wrong extension

    Try this:
    Code:
    Option Explicit
    
    Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, _
        ByVal lpDirectory As String, ByVal lpResult As String) As Long
    
    ' pstrFile contains full path and filename ("c:\File_id.diz")
    ' pstrExt contains original extension ("txt")
    Public Function RunFile(pstrFile As String, pstrExt As String) As String
        Dim strFake As String, strRet As String
        Dim lonRet As Long
        
        strFake = Left$(pstrFile, InStrRev(pstrFile, ".")) & pstrExt
        ' Now what?
       
        strRet = String$(255, Chr$(0))
        lonRet = FindExecutable(strFake, vbNullString, strRet)
        
        If lonRet > 32 Then
            RunFile = Left$(strRet, InStr(strRet, Chr$(0)) - 1)
        End If
    
    End Function
    
    Private Sub Form_Load()
        Dim strPath As String
        
        strPath = RunFile("C:\test\test.diz", "txt")
        MsgBox strPath
        'ShellExecute strPath here.
    End Sub

  13. #13

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    FindExecutable appears to only work if the file exists on disk.

    While I can create one empty dummy file for every extension in the database, that's pretty inelegant and wasteful. It's also less than desirable for other reasons specific to my project.

    Any other ways to solve the OP?

    Speaking of which, what's the deal with Vista and the registry? It seems that my existing solution of creating the command line based on the ClassesRoot file association in the registry is the only way to really achieve what I'm looking for. I'm concerned about it working under Vista, (thus my reason for posting this thread,) but if it'll still work under Vista, I'll just keep it as-is. Here's the current steps that I do, using Excel xls files as an example:

    Query ClassesRoot\.xls
    - Remember the (default) value (Excel.Sheet.8) for next step
    Query ClassesRoot\Excel.Sheet.8\Open\Shell\Command
    - The (default) value holds the command line to open that type of file

    In this case, it's "C:\Program Files\Microsoft Office\Office10\EXCEL.EXE" /e

    I then slap the filename (with the wrong extension) to the end of that and shellexecute it. Works great under XP; will it work under Vista?

    ETA: Note that most of the ClassesRoot\<Type>\Shell\Open\Command registry entries include a "%1" placeholder somewhere in the string to allow easy insertion of the desired filename. I'm not completely sure why Microsoft Office programs like Excel and Word don't inlcude a %1 in their command line, but whatever. They get to be different I guess.
    Last edited by Ellis Dee; Apr 18th, 2007 at 12:05 AM.

  14. #14
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Open a file with wrong extension

    Quote Originally Posted by Ellis Dee
    FindExecutable appears to only work if the file exists on disk.

    While I can create one empty dummy file for every extension in the database, that's pretty inelegant and wasteful. It's also less than desirable for other reasons specific to my project.

    Any other ways to solve the OP?
    Create an empty dummy file just before running FindExecutable, then delete it.

    ETA: Note that most of the ClassesRoot\<Type>\Shell\Open\Command registry entries include a "%1" placeholder somewhere in the string to allow easy insertion of the desired filename. I'm not completely sure why Microsoft Office programs like Excel and Word don't inlcude a %1 in their command line, but whatever. They get to be different I guess.
    Strange, since Excel can be run with an argument (it looks at its command line).
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  15. #15

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    Quote Originally Posted by Al42
    Create an empty dummy file just before running FindExecutable, then delete it.
    Yeah, that's probably my best bet.

    My original concern was that if the program should fail for whatever reason -- eg: the user decides to End Task -- I don't want dummy files left laying around.

    But if I create the dummy file the line before the FindExecutable call, then delete it the line after, that's pretty much as secure as it gets.

    Thanks much, Al42.

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Open a file with wrong extension

    Crazy question.... if the file has the"wrong" extension..... how are you going to know WHAT app to open it with.... For instance.... I have a word doc.... I change the extension form DOC to MP9..... there's no way to know that it's a Word Doc.

    or am I still missing something?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open a file with wrong extension

    Quote Originally Posted by techgnome
    Crazy question.... if the file has the"wrong" extension..... how are you going to know WHAT app to open it with
    The original extension is stored in a database.

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