Solved

VB6 - Save excel file on Desktop issue

Posted on 2014-10-19
7
496 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 46

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 46

Expert Comment

by:Martin Liss
ID: 40390374
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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 46

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 46

Expert Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 195
Java Loop 6 60
Cygwin - GNU GPL License 1 24
How to parse the string and split the sub directory names in asp.net 7 41
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

772 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