Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Apr 2003
    Location
    Hampshire, United Kingdom
    Posts
    602
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting desktop background in VBA (Win XP / Office XP)

    I'd like to write a little utility in VBA which picks an image from my "My Pictures" folder at random, and makes it the desktop background image. Selecting a random image is no problem, but I've no idea how to set the background image in VBA. Can it be done?
    Waggers
    If at first you do succeed, you've probably missed something.

  2. #2
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Setting desktop background in VBA (Win XP / Office XP)

    This <!post=fresh thread,473796>fresh thread<!/post> may get you at least partway there.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Setting desktop background in VBA (Win XP / Office XP)

    Put this declaration at the top of a module:

    Declare Function SystemParametersInfo Lib "user32" _
    Alias "SystemParametersInfoA" ( _
    ByVal uAction As Long, _
    ByVal uParam As Long, _
    ByVal lpvParam As Any, _
    ByVal fuWinIni As Long) As Long

    Const SPI_SETDESKWALLPAPER = 20
    Const SPIF_UPDATEINIFILE = &H1

    Use like this:

    Dim strWallpaper As String
    Dim lngRet As Long
    ' Set strWallpaper to a file name
    strWallpaper = ...
    ' Set as desktop image
    lngRet = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, strWallpaper, SPIF_UPDATEINIFILE)
    If lngRet = 0 Then
    MsgBox "Something went wrong."
    End If

  4. #4
    5 Star Lounger
    Join Date
    Apr 2003
    Location
    Hampshire, United Kingdom
    Posts
    602
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Setting desktop background in VBA (Win XP / Of

    Thanks, <img src=/S/hansv.gif border=0 alt=HansV width=27 height=26> .
    Waggers
    If at first you do succeed, you&#39;ve probably missed something.

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Setting desktop background in VBA (Win XP / Of

    If you want the change to take effect immediately (i.e., if you want the desktop to "refresh" and display the change), you may find that you need to make the following 2 changes to Hans's code:

    1. Add this additional constant declaration:
    <pre>Public Const SPIF_SENDWININICHANGE = &H2</pre>

    2. Change the final SystemParametersInfo argument from SPIF_UPDATEINIFILE to:
    <pre>SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE</pre>

    Note that this method only works for .bmp files. If you find one that also works for .jpg's, please post it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •