Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBA Create Shortcut With Dual Monitors

Posted on 2014-03-26
5
Medium Priority
?
386 Views
Last Modified: 2014-03-26
My version control code for Access applications sets up a shortcut on the user's desktop using the following code:

Private const mstrcModule as string = "basShortcut"
Private Sub CreateShortcut _
    ( _
        ByVal vstrCaption As String, _
        ByVal vstrFQTargetFile As String, _
        ByVal vfEachUser As Boolean _
    )
   
    On Error GoTo Proc_Err

    Const strcProc  As String = "CreateShortcut"
   
    Dim strDeskTop As String
   
    Dim sct As IWshShortcut_Class
    'Dim sct As WshShortcut
    Dim wsh As IWshShell_Class
    'im wsh As WshShell
   
    Set wsh = New IWshShell_Class
    'If vfEachUser Then
        strDeskTop = wsh.SpecialFolders("Desktop")
    'Else
        'strDeskTop = wsh.SpecialFolders("AllUsersDesktop")
    'End If
    Set sct = wsh.CreateShortcut(strDeskTop & "\" & vstrCaption & ".lnk")
    sct.TargetPath = vstrFQTargetFile
    sct.WindowStyle = 1
    sct.Save
    Set sct = Nothing
    Set wsh = Nothing
   
Proc_Exit:
    Exit Sub
   
Proc_Err:
    VBA.Err.Raise Err.Number, , mstrcModule & "." & strcProc & vbNewLine & Err.Source
End Sub

It's been working find for years, but yesterday I ran into the following problem. One of my users has dual monitors and he keeps his shortcuts on the 2nd monitor. For some reason my code always creates the shortcut on the 1st monitor. The user has been deleting the shortcut I create on the 1st monitor and continues to use the shortcut on the 2nd monitor. So obviously when the file pointed by the shortcut changed, my user had a problem.

Is there a way for my code to look for the shortcut on each of the user's monitors and replace that one with the new shortcut?
0
Comment
Question by:rmk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39956371
You can't specify a particular monitor, just the Desktop folder.  The monitor is dependent on where the user happens to drag the shortcut.

Of course, if the user has independently renamed the shortcut, this will not work.

Apart from this you should simply advise your users to use the correct shortcut (and move it to the desired monitor) rather than deleting it.

I'd suggest deleting any existing shortcut before creating the new one:


Private const mstrcModule as string = "basShortcut"
Private Sub CreateShortcut _
    ( _
        ByVal vstrCaption As String, _
        ByVal vstrFQTargetFile As String, _
        ByVal vfEachUser As Boolean _
    )
    
    On Error GoTo Proc_Err

    Const strcProc  As String = "CreateShortcut"
    
    Dim strDeskTop As String
    
    Dim sct As IWshShortcut_Class
    'Dim sct As WshShortcut
    Dim wsh As IWshShell_Class
    'im wsh As WshShell
    
    Set wsh = New IWshShell_Class
    'If vfEachUser Then
        strDeskTop = wsh.SpecialFolders("Desktop")
    'Else
        'strDeskTop = wsh.SpecialFolders("AllUsersDesktop")
    'End If
    ' check for and delete existing shortcut
    If Dir(strDeskTop & "\" & vstrCaption & ".lnk) <> "" then Kill strDeskTop & "\" & vstrCaption & ".lnk
    Set sct = wsh.CreateShortcut(strDeskTop & "\" & vstrCaption & ".lnk")
    sct.TargetPath = vstrFQTargetFile
    sct.WindowStyle = 1
    sct.Save
    Set sct = Nothing
    Set wsh = Nothing
    
Proc_Exit:
    Exit Sub
    
Proc_Err:
    VBA.Err.Raise Err.Number, , mstrcModule & "." & strcProc & vbNewLine & Err.Source
End Sub

Open in new window

0
 

Author Comment

by:rmk
ID: 39956554
The new shortcut I create has the same name as the old shortcut and it does NOT overwrite the existing shortcut. That's the problem.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 39956562
Did you try the code I posted, which explicitly deletes the existing shortcut?
0
 

Author Closing Comment

by:rmk
ID: 39956780
I just tested your delete code and does the trick on my computer. So then I went back the to the problem user and it worked there as well. However, it also worked properly without the delete code, i.e. I can't duplicate the original problem. I can only conclude that I was mistaken all along and the shortcut name must have been different.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39957021
Strange.  Your code does look fine.

I'd suggested the delete code speculating that something may have gone amiss either in testing or in normal use preventing the overwrite and causing the shortcut to be saved under a different name, such as C:\YourPath\YourFile(1).lnk or similar.

Glad you've got it resolved.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question