Solved

VB6 - Save excel file on Desktop issue

Posted on 2014-10-19
7
557 Views
Last Modified: 2014-10-19
Hi all

I have a problem for some users to save an excel file with the VB6 app.

Some users are under XP, Window7 and Window8.

So the Desktop path is not the same.

How can i adjust below code to always save in on the desktop, no matter the OS version?

Thanks again

xlObject.ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\all users\Desktop\New locations "

Open in new window

0
Comment
Question by:Wilder1626
  • 4
  • 3
7 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40390265
Take a look at this article.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 40390359
Hi MartinLiss

I see a 500 - Internal server error. when i click on the link
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40390374
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:Wilder1626
ID: 40390543
Ok, I'm about to try this.

Private Const CSIDL_DESKTOP = &H0
Private Const MAX_PATH = 255
Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long

Public Function GetDesktopFolderPath(ByVal pHwnd As Long) As String
    Dim lReturn As Long
    Dim lPidl   As Long
    Dim lPath   As Long
    Dim sPath   As String

    sPath = Space$(MAX_PATH)
    lReturn = SHGetSpecialFolderLocation(pHwnd, CSIDL_DESKTOP, lPidl)  ' Get lPidl for Id...
    If lReturn = 0 Then                                    ' If success is 0
        lReturn = SHGetPathFromIDList(lPidl, sPath)        '   Get Path from Item Id List
        If lReturn = 1 Then                                '   If success is 1
            sPath = Left(sPath, InStr(sPath, vbNullChar) - 1)             '     Fix path string
            GetDesktopFolderPath = sPath                    '     Return Favorite path
        End If
    End If
End Function

Open in new window


and then, to save, i will do:

Dim strPath As String

strPath = GetDesktopFolderPath(Me.Hwnd)
xlObject.ActiveWorkbook.SaveAs Filename:= strPath  & "\New locations"

Open in new window

0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40390619
Try this instead.

Function SpecialFolderPath() As String
     
    Dim objWSHShell As Object
    Dim strSpecialFolderPath
     
     'On Error GoTo ErrorHandler
     ' Create a shell object
    Set objWSHShell = CreateObject("WScript.Shell")
     ' Find out the path to the passed special folder,
     '  just change the "Desktop" for one of the other options
    SpecialFolderPath = objWSHShell.SpecialFolders("Desktop")
     ' Clean up
    Set objWSHShell = Nothing
    Exit Function
ErrorHandler:
     
    'MsgBox "Error finding " & strSpecialFolder, vbCritical + vbOKOnly, "Error"
End Function
Sub x()
Dim strPath As String

strPath = SpecialFolderPath
ActiveWorkbook.SaveAs Filename:=strPath & "\MyWB.xlsm", FileFormat:=57
End Sub

Open in new window

0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 40390738
Oh yes.

This is perfect

Many thanks for your help
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40390742
You're welcome.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 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