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
Solved

VBA Create Shortcut With Dual Monitors

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

Back Up Your Microsoft Windows Server®

Back up 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.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

839 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