Solved

VBA Create Shortcut With Dual Monitors

Posted on 2014-03-26
5
375 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now