[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?
Re: Open a file with wrong extension
Shell Word with the file as its Paramaters, or set .diz to a MSWord filetype.
vb Code:
WordPath = "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.exe"
Shell WordPath & " """ & ToOpen & """", vbNormalFocus
Something like that?
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.
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
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.)
Re: Open a file with wrong extension
Where do these files come from? How do they get created?
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.
Re: Open a file with wrong extension
Ok.....that wasn't what I was expecting to hear :D, 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?
Re: Open a file with wrong extension
Quote:
Originally Posted by Hack
Ok.....that wasn't what I was expecting to hear :D, 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.
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.
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
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
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.
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.
Quote:
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).
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.
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
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.