2010-08-10 127 views
3

手動我們右鍵單擊文件並選擇「打開方式」選項以其他格式打開。「Open with」選項通過vbscript

現在我需要通過VBScript中

+1

我不清楚你要求的是 - 你要求a)你的VBScript是「打開方式」菜單中的選項之一,或b)給定一個文件,檢索可用的「Open With」項目列表,並可能調用其中的一個(通過VBScript),或者c)通過VBScript給出一個文件,以導致「打開方式」對話框出現? – 2010-08-10 10:54:03

+0

我必須用其原始文件類型以外的其他格式打開文件。 例如:如果一個文件是通過VBScript的文字格式,我必須 以NOTpad格式打開它 – Ramesh 2010-08-10 11:41:04

+0

你的意思是,使用不同的應用程序打開一個文件,默認的文件? – Helen 2010-08-10 11:59:42

回答

0

如果你想創建使用VBScript關聯腳本當你寫點擊一個文件,並用一定的程序打開它要做到這一點,例如,你可以使用這個腳本我有創建歸途:

'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 

只需複製上面的代碼,並粘貼到文件中,並給該文件.vbs擴展。

+0

我們要在哪裏給文件路徑打開其他文件類型 – Ramesh 2010-08-10 10:40:26

+0

嘗試運行腳本,它會詢問你的路徑的例子。 – Sarfraz 2010-08-10 10:41:35

+0

我試過了,但文件沒有打開..它顯示消息爲「上下文菜單成功創建」..我想打開文件 我給它的格式時詢問 – Ramesh 2010-08-10 10:48:47

7

要使用特定應用程序打開文件,請使用methood來運行該應用程序並將文件名作爲參數傳遞。

下面是打開記事本,Internet Explorer和Microsoft Word中的同一個文本文件的例子:

strFileName = "c:\myfile.txt" 
Set oShell = CreateObject("WScript.Shell") 

oShell.Run "notepad " & strFileName 
oShell.Run "iexplore " & strFileName 
oShell.Run "winword " & strFileName 

請注意,如果文件名包含空格,則需要enslose它在引號,像這樣:

oShell.Run "winword ""c:\my file.txt""" 
+0

海倫..你的代碼工作第一次..它被拋出爲「Microsoft VBScript運行時錯誤'800a0046' - 權限被拒絕「但我沒有更改代碼,也沒有更改文件或文件權限的路徑 – Ramesh 2010-08-10 12:14:06