Hi
Manually we right click on a file and select the "open with" option to open in other format.Now i need to do this through vbscript
Thanks in Advance
Hi
Manually we right click on a file and select the "open with" option to open in other format.Now i need to do this through vbscript
Thanks in Advance
If you want to create an association script with VBScript, for example when you write click on a file and open it with certain program, you can use this script I had created way back:
'Run Script
InsertContextMenu
Sub InsertContextMenu ()
Dim sText
Dim sExePath
'For executable-only context menu, the key should be created here
'HKEY_CLASSES_ROOT\exefile\shell
sText = InputBox ("Enter the Text for the context menu." & vbNewLine & vbNewLine & "Example" & vbNewLine & "Open with Notepad")
If Len(Trim(sText)) > 0 Then
sExePath = InputBox ("Enter the path of the executable file for the context menu." & vbNewLine & vbNewLine & "Example" & vbNewLine & "C:\Windows\Notepad.exe")
If Len(Trim(sExePath)) > 0 Then
Set SHL = CreateObject ("WScript.Shell")
SHL.RegWrite "HKCR\*\Shell\" & sText & "\",sText
SHL.RegWrite "HKCR\*\Shell\" & sText & "\Command\", sExePath & " %1"
If Len(SHL.RegRead ("HKCR\*\Shell\" & sText & "\Command\")) > 0 Then
MsgBox "The Context Menu successfully created !.",vbInformation
Else
MsgBox "An unknown error has occured !!",vbCritical
End If
End If
End If
Set SHL = Nothing
End Sub
Just copy above code, and paste into a file and give that file .vbs
extension.
To open a file using a specific application, use the WshShell.Run
methood to run that application and pass the file name as a parameter.
Here's an example that opens the same text file in Notepad, Internet Explorer and Microsoft Word:
strFileName = "c:\myfile.txt"
Set oShell = CreateObject("WScript.Shell")
oShell.Run "notepad " & strFileName
oShell.Run "iexplore " & strFileName
oShell.Run "winword " & strFileName
Note that if the file name includes spaces, you need to enslose it in quotes, like this:
oShell.Run "winword ""c:\my file.txt"""