【问题标题】:"Open with" Options through vbscript通过 vbscript 的“打开方式”选项
【发布时间】:2011-03-27 17:38:30
【问题描述】:

手动我们右键单击文件并选择“打开方式”选项以其他格式打开。

现在我需要通过 vbscript 来做到这一点

【问题讨论】:

  • 我不清楚您的要求 - 您是否要求 a) 您的 VBScript 成为“打开方式”菜单中可用的选项之一,或者 b) 给定一个文件,检索可用“打开方式”项目的列表,并可能调用其中一个(通过 VBScript),或 c)给定一个文件,通过 VBScript,导致“打开方式”对话框出现?
  • 我必须打开一个格式不同于其原始文件类型的文件。例如:如果文件是通过 vbscript 的 word 格式,我必须以记事本格式打开它
  • 您的意思是,使用与默认应用程序不同的应用程序打开文件?

标签: vbscript


【解决方案1】:

要使用特定应用程序打开文件,请使用 WshShell.Run 方法运行该应用程序并将文件名作为参数传递。

这是一个在记事本、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

请注意,如果文件名包含空格,则需要将其括在引号中,如下所示:

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

【讨论】:

  • Helen..您的代码第一次工作..后来它抛出“Microsoft VBScript 运行时错误'800a0046' - 权限被拒绝”但我没有更改代码或文件的路径或文件权限
  • @Helen 如果你想在同一个编辑器中从文件扩展名打开文件,你需要:oShell.Run "" & strFileName
【解决方案2】:

如果你想用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 扩展名。

【讨论】:

  • 我们必须在哪里提供要使用其他文件类型打开的文件路径
  • 尝试运行脚本,它会通过示例询问您的路径。
  • 我试过了,但文件没有打开..它显示消息为“上下文菜单成功创建”..我想以我在询问时给出的格式打开文件
  • @Ramesh:您指定的文件是什么类型?以及您要打开该文件类型的文件的路径
  • 记事本,路径是 C:\test.doc..我已经打开记事本格式的 .doc 文件
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多