Solved

VBA Create Shortcut With Dual Monitors

Posted on 2014-03-26
5
378 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
  • 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 500 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

777 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