Solved

VB6 - Save excel file on Desktop issue

Posted on 2014-10-19
7
602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn different types of Android Layout and some basics of an Android App.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress

724 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