tags:

views:

51

answers:

2

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

A: 

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
where do we have to give the file path to be opened with other file type
Ramesh
Try running the script it will ask you for the path with example.
Sarfraz
i tried but the file is not opening..Its showing the message as "The Context Menu successfully created"..i want to open the filein the format i had given when it asked
Ramesh
@Ramesh: What **type** of file you had specified? and the **path** of the file you want to open that file type with?
Sarfraz
notepad and path is C:\test.doc..i have open the .doc filein notepad format
Ramesh
+1  A: 

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
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