Solved

VBA Create Shortcut With Dual Monitors

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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 …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

691 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