Create Shortcut with own icon (1 Viewer)

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
Dear friends,

Is it possible using vba each time when i open my pc automatically to create desktop shortcut with my own icon?

thanks again
 

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
I don’t think it’s possible with VBA...I’m quite sure an Office Application has to be open to invoke VBA. However, it’s cousin, VBS would do the trick.

Give this thread a look over. Bill Mosca’s code does exactly what you are looking for as well as a few other things.

https://www.access-programmers.co.uk/forums/showthread.php?t=207187&highlight=Mosca

Dear friend NauticalGent,

Can i use this code into a normal vba code or?

Thanks
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 10:33
Joined
Apr 27, 2015
Messages
6,281
Again, this is not VBA, it is VBS: Visual Basic Script and can be used outside of any Microsoft Office program which is what I think you are asking.
 

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
Again, this is not VBA, it is VBS: Visual Basic Script and can be used outside of any Microsoft Office program which is what I think you are asking.

Ok, understand but i cannot understand on how can i use the code outside the access application. Please note that i'm not expert my friend.....! Could you please explain me what exactly i must do in order to open my database and automatically to create the shortcut icon on desktop?

Moreover, note that i use my own icon so i want this to create as an my application icon on desktop
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:33
Joined
Sep 21, 2011
Messages
14,044
Why do you want to create he icon every time you open the DB.?

I only created the shortcut on the initial install of a DB to a computer.?
 

Lightwave

Ad astra
Local time
Today, 14:33
Joined
Sep 27, 2004
Messages
1,521
Not sure yes its possible to run a vba function from the desktop using a vbscript

I have something sorted so I can run compacts.

http://rounduptheusualsuspects.org/?p=1386

This is the vbscript which shows where you put your function name it doesn't entirely run it from the desktop though. It opens your database runs the function and then closes the database. Its nice though because you can automate the hell out of virtually anything

Code:
set oAccess = createobject("Access.Application")

oAccess.OpenCurrentDatabase "C:Path\TargetDatabasethatcontainsthefunctionyourwanttoRun!.mdb"
oAccess.visible = true
wscript.sleep 1000
oAccess.Run "NameOfFunctionYouWantToRun"
oAccess.closecurrentdatabase

set oAccess = nothing
 
Last edited:

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
Why do you want to create he icon every time you open the DB.?

I only created the shortcut on the initial install of a DB to a computer.?

Because sometimes the user can delete the shortcut icon....But, i think the ideal would be first the code to look if the icon is on the screen and if not then to create it
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:33
Joined
Feb 28, 2001
Messages
26,999
I am sensing a case of "the cart before the horse."

If there is no shortcut, then your user will have to copy and open the front end directly. Which actually is what should be done anyway. The only time you would want a shortcut of any sort would be ...

(a) if you were sharing the front end (not recommended) or

(b) if the whole database was shared and monolithic (not split, which is ALSO not recommended) or

(c) the shortcut is to a batch file that you are using to download & launch the latest version of the front end to the local system (but again, it is better to just copy the batch file to the local system and THEN launch it locally.)

WHY would you need a shortcut? (As opposed to having everyone have a copy of the front end or batch-job distributor?) I'm not saying what you are doing is wrong, but some points just don't add up quite right.
 

msbad199

New member
Local time
Today, 10:33
Joined
May 29, 2018
Messages
5
Try this:

Code:
  [FONT=Calibri]Public Sub CreateDesktopShortcut(strShortcutTitle As String, _[/FONT]
  [FONT=Calibri]                                 Optional strTargetPath As String = "")[/FONT]
  [FONT=Calibri]On Error Resume Next[/FONT]
  [FONT=Calibri] [/FONT]
  [FONT=Calibri]    Dim oShell As IWshShell_Class[/FONT]
  [FONT=Calibri]    Dim oShortcut As IWshShortcut_Class[/FONT]
  [FONT=Calibri]    Dim vItem As Variant[/FONT]
  [FONT=Calibri]    Dim vType As Variant[/FONT]
  [FONT=Calibri]        [/FONT]
  [FONT=Calibri]    Set oShell = New IWshShell_Class[/FONT]
  [FONT=Calibri]    [/FONT]
  [FONT=Calibri]    If strTargetPath = "" Then[/FONT]
  [FONT=Calibri]        strTargetPath = CurrentDb.Name[/FONT]
  [FONT=Calibri]    End If[/FONT]
  [FONT=Calibri]        [/FONT]
  [FONT=Calibri]    For Each vItem In oShell.SpecialFolders[/FONT]
  [FONT=Calibri]        If Mid(vItem, Len(vItem) - 6, 7) = "Desktop" And _[/FONT]
  [FONT=Calibri]           InStr(1, vItem, "All Users") = 0 And _[/FONT]
  [FONT=Calibri]           InStr(1, UCase(vItem), "ADMINISTRATOR") = 0 Then[/FONT]
  [FONT=Calibri]                Set oShortcut = oShell.CreateShortcut(vItem & "\" & strShortcutTitle & ".lnk")[/FONT]
  [FONT=Calibri]                oShortcut.TargetPath = strTargetPath[/FONT]
  [FONT=Calibri]                oShortcut.Save[/FONT]
  [FONT=Calibri]        End If[/FONT]
  [FONT=Calibri]    Next[/FONT]
  [FONT=Calibri]    [/FONT]
  [FONT=Calibri]End Sub[/FONT]


You will need to reference the Windows Script Host Object Model as well for this to work.
 

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
Try this:

Code:
  [FONT=Calibri]Public Sub CreateDesktopShortcut(strShortcutTitle As String, _[/FONT]
  [FONT=Calibri]                                 Optional strTargetPath As String = "")[/FONT]
  [FONT=Calibri]On Error Resume Next[/FONT]
  [FONT=Calibri] [/FONT]
  [FONT=Calibri]    Dim oShell As IWshShell_Class[/FONT]
  [FONT=Calibri]    Dim oShortcut As IWshShortcut_Class[/FONT]
  [FONT=Calibri]    Dim vItem As Variant[/FONT]
  [FONT=Calibri]    Dim vType As Variant[/FONT]
  [FONT=Calibri]        [/FONT]
  [FONT=Calibri]    Set oShell = New IWshShell_Class[/FONT]
  [FONT=Calibri]    [/FONT]
  [FONT=Calibri]    If strTargetPath = "" Then[/FONT]
  [FONT=Calibri]        strTargetPath = CurrentDb.Name[/FONT]
  [FONT=Calibri]    End If[/FONT]
  [FONT=Calibri]        [/FONT]
  [FONT=Calibri]    For Each vItem In oShell.SpecialFolders[/FONT]
  [FONT=Calibri]        If Mid(vItem, Len(vItem) - 6, 7) = "Desktop" And _[/FONT]
  [FONT=Calibri]           InStr(1, vItem, "All Users") = 0 And _[/FONT]
  [FONT=Calibri]           InStr(1, UCase(vItem), "ADMINISTRATOR") = 0 Then[/FONT]
  [FONT=Calibri]                Set oShortcut = oShell.CreateShortcut(vItem & "\" & strShortcutTitle & ".lnk")[/FONT]
  [FONT=Calibri]                oShortcut.TargetPath = strTargetPath[/FONT]
  [FONT=Calibri]                oShortcut.Save[/FONT]
  [FONT=Calibri]        End If[/FONT]
  [FONT=Calibri]    Next[/FONT]
  [FONT=Calibri]    [/FONT]
  [FONT=Calibri]End Sub[/FONT]


You will need to reference the Windows Script Host Object Model as well for this to work.

Thank you very much,

How can i call this Function?
 

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
Good morning to everyone,

With your help I have done the following:

1. I have read Link http://rounduptheusualsuspects.org/?p=1386 which Lightwave sent me and finally I managed to create a routine into my base which does exactly what is described in Link, (to do Compact & Repair regarding the example). I have succeeded to create a vbs file in which I put the example code into them.
Following attached is the code:

Firstly create the following function in your target MS Access database.

Public Function CompactDatabase()
Dim vStatusBar As Variant
DoCmd.SetWarnings False

If FileLen(CurrentDb.Name) > 2000000 Then
Application.SetOption ("Auto Compact"), 1
Application.SetOption "Show Status Bar", True
vStatusBar = SysCmd(acSysCmdSetStatus, "The application will be compacted on close during compaction please do not interrupt")
Else
Application.SetOption ("Auto Compact"), 0
End If

End Function

Next open up notepad paste in the following code save it as a txt file and then in explorer edit the suffix to vbs. Then simply double click on the file within explorer to run it. You should see the access database you have identified in the VB script open and then immediately close at which point it carries out the compact. The eagle eyed will have spotted that the above function doesn’t actually perform a compact. All it does is set the compact on close option to True within Access settings. The VB Script then instructs the database to close and Access compacts the database on exit.


set oAccess = createobject("Access.Application")

oAccess.OpenCurrentDatabase "C:path\TargetDatabaseyouwantCompacted.mdb"
oAccess.visible = true
wscript.sleep 1000
oAccess.Run "CompactDatabase"
oAccess.closecurrentdatabase

set oAccess = nothing[/I]


2. After the above example, I change the code which msbad199 sent me in order to create Shortcut on Desktop. Already i test the code using a command button direct into my database in order to check if the shortcut has been created. The code is working into the following problem.

The code creates the shortcut on desktop but giving different name of the file (File Name=1.accdb). So, the first issue is that and i thing its simple to fix it...

3. Then I have created the Vbs file based on the above example, converting it to do what I want. (create shortcut on desktop). I use the following code into the vbs file:

set oAccess = createobject("Access.Application")

oAccess.OpenCurrentDatabase "C:\CheckIT\Test.accdb"
oAccess.visible = true
wscript.sleep 1000
oAccess.Run "Call CreateDesktopShortcut"
oAccess.closecurrentdatabase

set oAccess = nothing

Please note that:

1. The vbs file is in the same folder with my database. (C:\CheckIt\Test.accdb)
2. If I run the vbs file I'm getting the attached error in the photo but i saw the database to open normally without to create the shortcut on desktop.


Any help...?
 

Attachments

  • Capture.JPG
    Capture.JPG
    21.4 KB · Views: 63

isladogs

MVP / VIP
Local time
Today, 14:33
Joined
Jan 14, 2017
Messages
18,186
Coming back to the original question, the code provided by msbad199 works but doesn't create an icon of your choice.

This code is taken from https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/xsy6k3ys(v=vs.84) and does add an icon - either a built in one for an application or a specified file of your choice

Code:
Sub CreateDesktopShortcutWithIcon()
    ' Based on code from https://msdn.microsoft.com/en-us/library/xsy6k3ys(v=vs.84).aspx
    Dim WshShell As Object
    Dim strDesktop As String
    Dim oShellLink As Object
    
    Set WshShell = CreateObject("WScript.Shell")
    strDesktop = WshShell.SpecialFolders("Desktop")
   
    Set oShellLink = WshShell.CreateShortcut(strDesktop & "\TestShortcut.lnk") 'modify as appropriate
    
    'modify each of the values as appropriate
    With oShellLink
         .TargetPath = "C:\Users\Public\YourFileName.accdb"
         .WindowStyle = 1
        '.Hotkey = "Ctrl+Alt+f" 'optional
         '.IconLocation = "notepad.exe, 0"  ' for embedded icon
         .IconLocation = "C:\__tmp\MIME\foo.ico"  ' for specified .ico file
         .Description = "My shortcut created by VBA" 'optional
         .WorkingDirectory = strDesktop
         '.Arguments = "C:\myFile.txt"  'optional
         .Save
    End With
End Sub
 

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
Coming back to the original question, the code provided by msbad199 works but doesn't create an icon of your choice.

This code is taken from https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/xsy6k3ys(v=vs.84) and does add an icon - either a built in one for an application or a specified file of your choice

Code:
Sub CreateDesktopShortcutWithIcon()
    ' Based on code from https://msdn.microsoft.com/en-us/library/xsy6k3ys(v=vs.84).aspx
    Dim WshShell As Object
    Dim strDesktop As String
    Dim oShellLink As Object
    
    Set WshShell = CreateObject("WScript.Shell")
    strDesktop = WshShell.SpecialFolders("Desktop")
   
    Set oShellLink = WshShell.CreateShortcut(strDesktop & "\TestShortcut.lnk") 'modify as appropriate
    
    'modify each of the values as appropriate
    With oShellLink
         .TargetPath = "C:\Users\Public\YourFileName.accdb"
         .WindowStyle = 1
        '.Hotkey = "Ctrl+Alt+f" 'optional
         '.IconLocation = "notepad.exe, 0"  ' for embedded icon
         .IconLocation = "C:\__tmp\MIME\foo.ico"  ' for specified .ico file
         .Description = "My shortcut created by VBA" 'optional
         .WorkingDirectory = strDesktop
         '.Arguments = "C:\myFile.txt"  'optional
         .Save
    End With
End Sub


Ok Isladocs, using your code everything working fine. The vbs file now can creates the shortcut on the desktop screen with my own icon style. So far so good till here.!

Second step is: 1) I want each time when the Pc open using code to check if my shortcut exist on screen or not. I case where the user delete the shortcut must create again automatically next time when open the database or when open the PC..

and 2) Is it possible to run the vbs file using the autoexec macro?

Thanks in advanced all of you
 

isladogs

MVP / VIP
Local time
Today, 14:33
Joined
Jan 14, 2017
Messages
18,186
Ok Isladocs, using your code everything working fine. The vbs file now can creates the shortcut on the desktop screen with my own icon style. So far so good till here.!

Second step is: 1) I want each time when the Pc open using code to check if my shortcut exist on screen or not. I case where the user delete the shortcut must create again automatically next time when open the database or when open the PC..

and 2) Is it possible to run the vbs file using the autoexec macro?

Thanks in advanced all of you

1. If the shortcut already exists, it is overwritten ... so I'd just ignore that issue
2. To run from your autoexec macro, change the code from a Sub to a Function. Then use RunCode followed by the function name

BUT I still don't understand why you don't just create your shortcut using the vbs file when Windows starts.
Why open a database just so you can run this code?
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 14:33
Joined
Sep 21, 2011
Messages
14,044
I'd just be telling the user to stop deleting the shortcut. :D
 

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
1. If the shortcut already exists, it is overwritten ... so I'd just ignore that issue
2. To run from your autoexec macro, change the code from a Sub to a Function. Then use RunCode followed by the function name

BUT I still don't understand why you don't just create your shortcut using the vbs file when Windows starts.
Why open a database just so you can run this code?

BUT I still don't understand why you don't just create your shortcut using the vbs file when Windows starts.

How can i set the windows to run this vba script file....i don't know...
 

isladogs

MVP / VIP
Local time
Today, 14:33
Joined
Jan 14, 2017
Messages
18,186
Windows can't run a vba file.as already stated, vba only runs within Office applications or similar.
It can run a vbs file e.g as a scheduled task

Anyway have you now got it working using autoexec in Access?
 

gstylianou

Registered User.
Local time
Today, 16:33
Joined
Dec 16, 2013
Messages
357
Windows can't run a vba file.as already stated, vba only runs within Office applications or similar.
It can run a vbs file e.g as a scheduled task

Anyway have you now got it working using autoexec in Access?

Yes....i did and test that.. everything OK.

Thanks for all your help...
 

Users who are viewing this thread

Top Bottom