【问题标题】:VBA Shell command with variables and spaces带有变量和空格的 VBA Shell 命令
【发布时间】:2019-03-26 00:39:32
【问题描述】:

在 Microsoft Outlook 宏中,我尝试使用 Shell() 调用打开 Excel 电子表格(其文件路径由我的 templatePath 变量引用)。我不断收到来自编辑器的语法错误和执行时出现“找不到文件”错误。

我从以下行开始:

Shell ("""C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"" ""C:\Users\My_Username\Desktop\My_Folder\Request Template.xlsx"""), vbNormalFocus

它可以很好地打开相应的文件;我只是不知道使用templatePath 变量而不是硬编码电子表格路径的正确语法。我见过类似的问题,但似乎没有一个与我的情况足够接近。任何帮助将不胜感激!

【问题讨论】:

  • application.TemplatesPath 每次用户名都在路径中时,您会打开它吗?文件是模板吗?如果是这样,它可能是模板扩展,而不是正常的 xls?

标签: excel vba shell outlook


【解决方案1】:

这应该可行:

Dim templatePath As String

templatePath = "C:\Users\My_Username\Desktop\My_Folder\RequestTemplate.xlsx"

Shell ("""C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"" """ & templatePath & """"), vbNormalFocus

如果您的模板位于Application.TemplatesPath 中并且您只想指定文件名称,则使用:

templatePath = Application.TemplatesPath & "RequestTemplate.xlsx"

更可调的版本:

Dim templatePath As String
Dim programPath As String
Dim templateName As String

templateName = "RequestTemplate.xlsx"
templatePath = Application.TemplatesPath
programPath = "C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"

Shell ("""" & programPath & """" & " " & """" & templatePath & templateName & """"), vbNormalFocus

【讨论】:

  • 完美运行!感谢您的快速回复——所有这些引号总是让我感到困惑。
  • 如有疑问,“证明”引号等的好方法是将文本发送到即时窗口,而不是 Shell 以查看您要发送的内容。
猜你喜欢
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 2019-02-22
  • 2015-04-18
  • 1970-01-01
相关资源
最近更新 更多