PDA

View Full Version : [SOLVED:] Chage default program to open a filetype?



Ago
04-15-2014, 08:27 AM
Is it possible to with VBA tell windows to open *.srt files in Excel?

Can't find anything when I google it.

snb
04-15-2014, 08:35 AM
?


shell "Excel.exe G:\OF\example.srt"

Ago
04-15-2014, 08:40 AM
I don't want to open the file, just bind the filetype with Excel so that when you try to open a srt-file you won't get the "windows can't open this file"-message.

snb
04-15-2014, 08:49 AM
You will have to adapt that in the windows registry.
But if you open the file (right click mouse) 'open with ' it presents you to bind the extension to Excel. After doing this once you are ready and won't need any VBA

Kenneth Hobs
04-15-2014, 06:21 PM
As snb said, manually modifying it or just running it by Shell() or even a batch file method would be sufficient for most.

Funny, the first google link worked for me.

Sub Test_associate() associate ".srt", "Microsoft Excel 97-2003 Worksheet", _
Application.Parent.Path & "\Excel.exe"
End Sub


' http://www.freevbcode.com/ShowCode.asp?ID=2799
Sub associate(EXT As String, FileType As String, _
FileName As String)
On Error Resume Next
Dim b As Object
Set b = CreateObject("wscript.shell")
With b
.regwrite "HKCR\" & EXT & "\", FileType
.regwrite "HKCR\" & FileType & "\", "MY file"
.regwrite "HKCR\" & FileType & "\DefaultIcon\", FileName
.regwrite "HKCR\" & FileType & "\shell\open\command\", FileName & " %L"
.regdelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\Application"
.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\Application", FileName
.regdelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\OpenWithList\"
.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & EXT & "\OpenWithList\a", FileName
End With
End Sub

Ago
04-15-2014, 09:38 PM
Oops... Your googling skills are much better than mine.

That looks exactly what I need, I will test it tonight and see if it works.

Kenneth Hobs
04-16-2014, 06:59 AM
You will find that I most always test my code before posting. The icon does not associate. Keep in mind that just because you set the file association, it does not mean that that program can open the file's "true" file type.

When searching for solutions, use terms like VBA rather than Excel. e.g.
VBA Set File Association

Egad, did I just share my secret? Shoot, now people will know that I know nothing.

mancubus
04-16-2014, 08:11 AM
:whistle:

you shared my secret as well. :)

i just use the name of the ms app before "VBA" keyword like "Excel VBA Set File Association".

Ago
04-16-2014, 09:07 AM
You will find that I most always test my code before posting. The icon does not associate. Keep in mind that just because you set the file association, it does not mean that that program can open the file's "true" file type.

When searching for solutions, use terms like VBA rather than Excel. e.g.
VBA Set File Association

Egad, did I just share my secret? Shoot, now people will know that I know nothing.

I think I searched for "VBA default program", "VBA open file with", etc...
My mojo failed....


As long as the file association gets done the files can be opened in Excel.
Both in stock Excel, but with my macro it makes it easier.
This code is just to simplify for the user.


Thanks!

snb
04-16-2014, 11:41 AM
Did you try to replace the .srt extension by '.xls' or '.csv' ?

Ago
04-16-2014, 12:32 PM
Did you try to replace the .srt extension by '.xls' or '.csv' ?

That works, but won't give the result I need.
The thought is to use Excel to "timeshift" a subtitle file.

Srt is subtitlefiles you can have playing when you watch a videofile and if the timeing is incorrect you need to shift the time with X number of seconds and miliseconds.
And I can do that with my macro but to make it easier for the end user I associate srt with Excel.



Anyways about the code.
It worked fine but I needed some modifications.
I just went to bed and turned of the computer, but I can post the modificated code tomorrow or this weekend.
But it was errortrapping issues.

snb
04-16-2014, 02:24 PM
- you can rename an .srt file (it's extension into .csv or .xls)
- you can load the .srt file in Excel
- you can modify the data
- you can write the modified data directly to an .srt file

so no need to interfere with the registry.

Ago
04-20-2014, 04:03 AM
Still won't be a a easy way.
Each SRT-file has 5 entries per second.
Each entry is four rows of data.
Each file is about 5 minutes long.
So.. 5 min x 60 seconds x 5 per second x 4 rows is too much to deal with manually.

OK?


This is what I had to do with the code.



Private Sub CommandButton1_Click() 'Associate SRT-files with Excel
Dim b As Object
Dim KeyPath As String


KeyPath = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\"
Set b = CreateObject("wscript.shell")
On Error Resume Next
b.RegRead (KeyPath)
If err.Number <> "0" Then 'Folder does not exsist
With b
.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\a", Application.Parent.Path & "\Excel.exe"
.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\MRUList", "a"
End With

Else 'Folder exsists, so delete it

With b
.regdelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\"
.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\a", Application.Parent.Path & "\Excel.exe"
.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\MRUList", "a"
End With
End If
Set b = Nothing
MsgBox "Done!"
Exit Sub


err:
With b
.regdelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\"
End With
Set b = Nothing
MsgBox ("Ooops! Something went wrong." & vbCr & "The association was aborted, please refer to the manual method.")
End Sub




Private Sub CommandButton2_Click() 'Delete association with Excel
On Error Resume Next
Dim b As Object
Set b = CreateObject("wscript.shell")
With b
.regdelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\"
End With
Set b = Nothing

MsgBox ("The association with Excel is deleted.")

End Sub




Private Sub CommandButton3_Click() 'Check current association with SRT-files
On Error GoTo err
Dim b As Object
Set b = CreateObject("WScript.Shell")
MsgBox b.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.srt\OpenW ithList\a")
Set b = Nothing
Exit Sub

err:
Set wsh = Nothing
MsgBox "Can't find any association with SRT-files"

End Sub


It seems it needs a MRUList. I associated the files manually and just "copied" what Windows did

snb
04-20-2014, 04:21 AM
Still won't be a a easy way.
Each SRT-file has 5 entries per second.
Each entry is four rows of data.
Each file is about 5 minutes long.
So.. 5 min x 60 seconds x 5 per second x 4 rows is too much to deal with manually.

OK?

Not OK

You are not getting at all what I am suggesting. Nothing to be done manually; it all can be done in 1 simple macro.

Kenneth Hobs
04-21-2014, 08:22 AM
Ago, you lost me. I don't know what Ok? means.

The file association method I posted worked. Other than that, what else are you trying to do? If the file is a text file, there are many ways to deal with that depending on what you need from it.

Why would an MRUlist be needed?

Maybe if you restated what you now need help with we could.

Ago
04-21-2014, 11:20 PM
Ago, you lost me. I don't know what Ok? means.

The file association method I posted worked. Other than that, what else are you trying to do? If the file is a text file, there are many ways to deal with that depending on what you need from it.

Why would an MRUlist be needed?

Maybe if you restated what you now need help with we could.

The "OK-part" was to snb.


About the code.
When I run the code you posted it does not bind the filetype with Excel. It only "suggests" it to be opened with Excel.
When I dubble click on the SRT-file I get the open with list and Excel is "marked".

If I choose to open with Excel and "always open this...." the registy changes to what I have in my code above.
A REG_SZ named "a" and with value "....Excel.exe".
and one named "MRUList" with value "a".

So your code gave me "step one" and helped me finnish it.

Thanks a lot!

Kenneth Hobs
04-22-2014, 05:36 AM
I guess I needed a sample file as my text file emulation of it opened in Excel just fine. Anyway, glad all is well now.