0

In a Microsoft Outlook macro, I'm trying to use a Shell() call to open an Excel spreadsheet (whose filepath is referenced by my templatePath variable). I keep getting syntax errors from the editor and "file not found" errors when executing it.

I started with the following line:

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

It opens the appropriate file just fine; I just don't know the proper syntax to use the templatePath variable instead of hard-coding the path to the spreadsheet. I've seen questions similar to this, but none seemed to fit my situation closely enough. Any help would be greatly appreciated!

1
  • application.TemplatesPath Will this be opened by you each time as the the username is in the path. Is the file a template? If so it may be template extension, not normal xls? Commented Mar 25, 2019 at 16:20

1 Answer 1

1

This should work:

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

If your template resides in the Application.TemplatesPath and you just want to specify the filename then use:

templatePath = Application.TemplatesPath & "RequestTemplate.xlsx"

A more adjustable version:

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
Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfectly! Thanks for the quick response-- all those quotation marks always confuse me.
When in doubt, a good way to 'proof' the quotes etc. is to send the text to the Immediate Window, instead of Shell to see what you're trying to send.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.