3
votes

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

2
I'm not clear what you're asking for - are you asking for a) your VBScript to be one of the options available in the "Open With" menu, or b) Given a file, to retrieve the list of available "Open With" items, and possibly invoke one of them (through VBScript), or c) Given a file, through VBScript, to cause the "Open With" dialog to appear? - Damien_The_Unbeliever
i have to open a file with other format than its original filetype. for eg: if a file is in word format through vbscript i have to open it in notpad format - Ramesh
You mean, open a file using a different application that the default one? - Helen

2 Answers

9
votes

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"""
0
votes

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.