3

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

Aubin
  • 14,617
  • 9
  • 61
  • 84
Ramesh
  • 31
  • 1
  • 2
  • 6
  • 1
    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 Aug 10 '10 at 10:54
  • 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 Aug 10 '10 at 11:41
  • You mean, open a file using a different application that the default one? – Helen Aug 10 '10 at 11:59

2 Answers2

9

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"""
Helen
  • 87,344
  • 17
  • 243
  • 314
  • Helen..your code worked for first time..later it is throwing as "Microsoft VBScript runtime error '800a0046' - Permission denied" But i did not changed the code nor the path of the file or the file permissions – Ramesh Aug 10 '10 at 12:14
  • 1
    @Helen if you want to open the file in the same editor from the file extension, you will need that: `oShell.Run "" & strFileName` – TorbenIT Sep 15 '21 at 07:34
0

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.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • where do we have to give the file path to be opened with other file type – Ramesh Aug 10 '10 at 10:40
  • Try running the script it will ask you for the path with example. – Sarfraz Aug 10 '10 at 10:41
  • i tried but the file is not opening..Its showing the message as "The Context Menu successfully created"..i want to open the file in the format i had given when it asked – Ramesh Aug 10 '10 at 10:48
  • @Ramesh: What **type** of file you had specified? and the **path** of the file you want to open that file type with? – Sarfraz Aug 10 '10 at 10:53
  • notepad and path is C:\test.doc..i have open the .doc file in notepad format – Ramesh Aug 10 '10 at 11:43