Create Shortcut to Excel & an Excel file using VBA

Scotster

Board Regular
Joined
May 29, 2017
Messages
59
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I've never had a problem creating shortcuts using VBA but I'm a bit stumped at how to create a shortcut that has 2 parts to it.

The end result, and the shortcut that I currently have working that I made manually, has a target path as follows:


"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" "https://site.sharepoint.com/Filepath/File.xls"

When I attempt to use that as the target path programmatically, I get an error. My guess is it's due to the 2 part nature of the string, with the space in the middle. So I started playing around and gave the following a go:

Code:
TargetPath = """C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"" ""https://Site.sharepoint.com/Filepath/File.xls"""
TargetPath = "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" & " " & "https://Site.sharepoint.com/Filepath/File.xls"
TargetPath = "'C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE' 'https://Site.sharepoint.com/Filepath/File.xls'"

And a few others that I can't remember. I've tried extracting the targetpath from the current working shortcut but it only pulls the first part of the path "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE", ignoring the latter half.

Is there a way of programmatically creating this shortcut path?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Just out of interest, can't you just use the file path? Why do you need to include the application?
 
Upvote 0
Just out of interest, can't you just use the file path? Why do you need to include the application?

Web security unfortunately. The file link is a URL, if I open directly it opens via Edge(IE) which then has additional security checks which makes the file initially read-only and disables the macros. This causes headaches as when the macros are enabled the first thing it does is check to see if it's read-only. If I enable workbook for editing I get a weird access error and it stays read-only.

If I open directly from Excel itself the internet security check is bypassed and it doesn't give me headaches.

The reason for creating the shortcut is a lot of employees in work that will be using the file set aren't overly comfortable with teams and I want to try and bypass any of the files opening inside teams regardless of user choices. People also like shortcuts on their desktop.

I can't email a shortcut or add it to teams to share, due to aforementioned security, so I was hoping to check if it already existed and if not.... create it.

I'm stuck with passing the filepath string though :(
 
Upvote 0
Problem solved. The key is .Arguments.

Code:
.TargetPath = "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
.Arguments = " ""https://Site.sharepoint.com/Filepath/File.xls"""

That was doing my head in haha.
 
Upvote 0
Solution
Pass it as the .Arguments property rather than part of the target path.

[edit: oops a bit late!]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top