ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Turn off read only attribute on shortcut (https://www.excelbanter.com/excel-programming/420261-turn-off-read-only-attribute-shortcut.html)

michelle

Turn off read only attribute on shortcut
 
I'm trying to delete a shortcut off the users desktop programmatically. Here
is the code I have for checking if the file exists and deleting it.
Unfortunately I can't delete it because its read only! Please help!

Function SeeIfShortcutExists()
Dim WSHShell As Object
Dim MyShortcut As Object
Dim DesktopPath As String
Dim blnFile As Boolean
Set WSHShell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
Set MyShortcut = WSHShell.CreateShortcut(DesktopPath & "\" &
"Consolidated - Approve" & ".lnk")

blnFile = Len(Dir(MyShortcut))
End With

MsgBox blnFile

If blnFile = True Then
Kill (DesktopPath & "\" & "Consolidated - Approve" & ".lnk")
End If


End Function


Dave Peterson

Turn off read only attribute on shortcut
 
Maybe...

Option Explicit
Function DeleteAndRecreateShortcut()

Dim WSHShell As Object
Dim MyShortCut As Object
Dim DesktopPath As String
Dim myFileName As String
Dim TestStr As String

Set WSHShell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
myFileName = DesktopPath & "\" & "Consolidated - Approve" & ".lnk"

TestStr = ""
On Error Resume Next
TestStr = Dir(myFileName)
On Error GoTo 0

If TestStr = "" Then
'doesn't exist
Else
'won't hurt if it's not readonly, either.
SetAttr pathname:=myFileName, attributes:=vbNormal
Kill pathname:=myFileName
End If

Set MyShortCut = WSHShell.CreateShortcut(myFileName)
With MyShortCut
.Description = "Consolidated" & vbCrLf & "Approved"
.TargetPath = ActiveWorkbook.FullName
'.IconLocation = "x:\somepathtoanicon\someicon.ico"
.Save
End With

'mark it readonly?
SetAttr pathname:=myFileName, attributes:=vbReadOnly

End Function

Michelle wrote:

I'm trying to delete a shortcut off the users desktop programmatically. Here
is the code I have for checking if the file exists and deleting it.
Unfortunately I can't delete it because its read only! Please help!

Function SeeIfShortcutExists()
Dim WSHShell As Object
Dim MyShortcut As Object
Dim DesktopPath As String
Dim blnFile As Boolean
Set WSHShell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
Set MyShortcut = WSHShell.CreateShortcut(DesktopPath & "\" &
"Consolidated - Approve" & ".lnk")

blnFile = Len(Dir(MyShortcut))
End With

MsgBox blnFile

If blnFile = True Then
Kill (DesktopPath & "\" & "Consolidated - Approve" & ".lnk")
End If

End Function


--

Dave Peterson


All times are GMT +1. The time now is 03:17 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com