Problem with script, please help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all, i am using the script below to check for and install a desktop
shortcut. However it does not see that there is a shortcut already, but
installs it ok. I do not know if i must have a certain "referance" flagged or
not. Any help would be appreciated...


Sub testme02()
Dim oWSH As Object
Dim oShortcut As Object
Dim sPathDeskTop As String
Dim testStr As String

Set oWSH = CreateObject("WScript.Shell")
sPathDeskTop = oWSH.SpecialFolders("Desktop")

testStr = ""
On Error Resume Next
testStr = Dir(sPathDeskTop & "\" & ActiveWorkbook.Name & ".lnk")
On Error GoTo 0

If testStr = "" Then
'not found
Set oShortcut = oWSH.CreateShortCut(sPathDeskTop & "\" & _
ActiveWorkbook.Name & ".lnk")
With oShortcut
.Description = "hi there"
.TargetPath = ActiveWorkbook.FullName
.IconLocation = "\\nv09002\tpdrive\TM-Recon\macro\scale.ico"
.Save
End With
Set oWSH = Nothing
Else
MsgBox "found it"
End If
end sub
 
The code works on my PC with Excel 2003. The 2nd time I run the program
"found it" is displayed. The workbook has to be saved in order for the code
to run properly.

I would comment out the ON Error statement and see what is really happening.
You could be getting an error and that is why you not seeing the Found It.
If an error occurs teststr will be set to "".
 
Hi Joel, could it be because i have added the following
dim wkb as string

wkb = "Test.xls"

testStr = Dir(sPathDeskTop & "\" & wkb & ".lnk")

If i run the code, it inserts it again and then it see's it ??
 
Is the excel workbook named Test.xls? Did you also change the line below?
Set oShortcut = oWSH.CreateShortCut(sPathDeskTop & "\" & _
ActiveWorkbook.Name & ".lnk")

DIR will always return a "" if it doesn't find the shortcut.

I added a variable shortcutname to prevent problems. the file you are
testing and the file you are saving are two diffferent names

Sub testme02()
Dim oWSH As Object
Dim oShortcut As Object
Dim sPathDeskTop As String
Dim testStr As String

Set oWSH = CreateObject("WScript.Shell")
sPathDeskTop = oWSH.SpecialFolders("Desktop")

shortcutname = sPathDeskTop & "\" & wkb & ".lnk"
On Error Resume Next
testStr = Dir(shortcutname)
On Error GoTo 0

If testStr = "" Then
'not found
Set oShortcut = oWSH.CreateShortCut(shortcutname)
With oShortcut
.Description = "hi there"
.TargetPath = ActiveWorkbook.FullName
.IconLocation = "\\nv09002\tpdrive\TM-Recon\macro\scale.ico"
.Save
End With
Set oWSH = Nothing
Else
MsgBox "found it"
End If

End Sub
 
Hi Joel, I found the problem thanks... I am not a programmer but a dabbler.
I was not looking for the correct name.... But have learnt something, thanks
for the input & sorry if i wasted any time..

'------------------- Create a short cut on the desktop
----------------------------------------
Sub Auto_Open()
Dim oWSH As Object
Dim oShortcut As Object
Dim sPathDeskTop As String, wbk As String
Dim testStr As String
Application.IgnoreRemoteRequests = False ' Do not open other instances
of excel within this sessin
Application.WindowState = xlMinimized ' Run it hidden
Set oWSH = CreateObject("WScript.Shell")
sPathDeskTop = oWSH.SpecialFolders("Desktop")
wbk = "Find Abbreviation" '=== Chamged this here...
testStr = ""
testStr = Dir(sPathDeskTop & "\" & wbk & ".lnk")
If testStr = "" Then
'------------------ create Shortcut
----------------------------------------------
Set oShortcut = oWSH.CreateShortCut(sPathDeskTop & "\Find
Abbreviation.lnk")'=== this was my problem !!!
With oShortcut
.Description = "Click here to find an abbreviation"
.TargetPath =
"\\nv09002\tpdrive\Projects\General\100_Abbreviations\Abbreviations.xls"
.IconLocation =
"\\nv09002\tpdrive\Projects\General\100_Abbreviations\Find.ico"
.Save
End With
'--------------- Msg to tell user about the shortcut
-------------------------------
Application.WindowState = xlMaximized ' Open it up
My_InfoBox
Else
Application.WindowState = xlMaximized ' Open it up
My_InfoBox1
End If
End Sub
 
to prevent problems you should use a variable so the two statement are know
to use the same string

shortcutname = sPathDeskTop & "\" & wbk & ".lnk"
testStr = Dir(shortcutname)
If testStr = "" Then
'------------------ create Shortcut
 
Back
Top