Solved

VB6 - Save excel file on Desktop issue

Posted on 2014-10-19
7
448 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 45

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 45

Expert Comment

by:Martin Liss
ID: 40390374
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 45

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 45

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
This is about my first experience with programming Arduino.
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 …
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now