PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Do File Open and default to the MyDocuments dialog box

 
 
Steven
Guest
Posts: n/a
 
      19th Mar 2008
How do I create a macro to do a File Open and default to the 'My Documents'
dialog box?

Thank you,

Steven
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      19th Mar 2008
One way:

Option Explicit
Sub testme()

Dim myDocumentsPath As String
Dim wsh As Object
Dim myFileName As Variant
Dim CurPath As String
Dim wkbk As Workbook

'save the existing current directory
CurPath = CurDir

Set wsh = CreateObject("WScript.Shell")
myDocumentsPath = wsh.SpecialFolders.Item("mydocuments")

'change to the one you want
ChDrive myDocumentsPath
ChDir myDocumentsPath

myFileName = Application.GetOpenFilename(filefilter:="Excel Files, *.xls")

'change back to the old directory
ChDrive CurPath
ChDir CurPath

If myFileName = False Then
Exit Sub
End If

'do what you want--open the file???
set wkbk = workbooks.open(filename:=myfilename)

End Sub

Steven wrote:
>
> How do I create a macro to do a File Open and default to the 'My Documents'
> dialog box?
>
> Thank you,
>
> Steven


--

Dave Peterson
 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      19th Mar 2008
You can use the code at http://www.cpearson.com/Excel/SpecialFolders.aspx to
get the name of the current user's My Document folder (actual name and
location varies by locale and operating system version). One you have that,
you can use ChDrive and ChDir to set the default directory to that folder
and then call GetOpenFileName.

Dim MyDocsFolderName As String
Dim SaveDir As String
Dim FName As Variant

SaveDir = CurDir
' GetSpecialFolder at http://www.cpearson.com/Excel/SpecialFolders.aspx
MyDocsFolderName = GetSpecialFolder(CSIDL_PERSONAL)
ChDrive MyDocsFolderName
ChDir MyDocsFolderName
FName = Application.GetOpenFilename()
ChDrive SaveDir
ChDir SaveDir


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)





"Steven" <(E-Mail Removed)> wrote in message
news:A0370F2F-2C5B-44AD-B2C3-(E-Mail Removed)...
> How do I create a macro to do a File Open and default to the 'My
> Documents'
> dialog box?
>
> Thank you,
>
> Steven


 
Reply With Quote
 
Steven
Guest
Posts: n/a
 
      19th Mar 2008
Thank you Dave. One thing. I put a button on the formatting bar that runs a
macro in a file that only holds macros. At the end of this marco I have a
command to close the macro file w/o saving. But, if I hit the Cancel button
on the Open File Dialog box the macro command to close the macro file is not
processes. How do I still close the macro file even if the Cancel button is
clicked on the Open File Dialog box.

Thank you,

Steven

"Dave Peterson" wrote:

> One way:
>
> Option Explicit
> Sub testme()
>
> Dim myDocumentsPath As String
> Dim wsh As Object
> Dim myFileName As Variant
> Dim CurPath As String
> Dim wkbk As Workbook
>
> 'save the existing current directory
> CurPath = CurDir
>
> Set wsh = CreateObject("WScript.Shell")
> myDocumentsPath = wsh.SpecialFolders.Item("mydocuments")
>
> 'change to the one you want
> ChDrive myDocumentsPath
> ChDir myDocumentsPath
>
> myFileName = Application.GetOpenFilename(filefilter:="Excel Files, *.xls")
>
> 'change back to the old directory
> ChDrive CurPath
> ChDir CurPath
>
> If myFileName = False Then
> Exit Sub
> End If
>
> 'do what you want--open the file???
> set wkbk = workbooks.open(filename:=myfilename)
>
> End Sub
>
> Steven wrote:
> >
> > How do I create a macro to do a File Open and default to the 'My Documents'
> > dialog box?
> >
> > Thank you,
> >
> > Steven

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      19th Mar 2008
This portion:

If myFileName = False Then
Exit Sub
End If

'do what you want--open the file???
set wkbk = workbooks.open(filename:=myfilename)

End Sub

needs to be replaced with:

If myFileName = False Then
'do nothing
else
'do what you want--open the file???
set wkbk = workbooks.open(filename:=myfilename)
end if

thisworkbook.close savechanges:=false

End Sub

Steven wrote:
>
> Thank you Dave. One thing. I put a button on the formatting bar that runs a
> macro in a file that only holds macros. At the end of this marco I have a
> command to close the macro file w/o saving. But, if I hit the Cancel button
> on the Open File Dialog box the macro command to close the macro file is not
> processes. How do I still close the macro file even if the Cancel button is
> clicked on the Open File Dialog box.
>
> Thank you,
>
> Steven
>
> "Dave Peterson" wrote:
>
> > One way:
> >
> > Option Explicit
> > Sub testme()
> >
> > Dim myDocumentsPath As String
> > Dim wsh As Object
> > Dim myFileName As Variant
> > Dim CurPath As String
> > Dim wkbk As Workbook
> >
> > 'save the existing current directory
> > CurPath = CurDir
> >
> > Set wsh = CreateObject("WScript.Shell")
> > myDocumentsPath = wsh.SpecialFolders.Item("mydocuments")
> >
> > 'change to the one you want
> > ChDrive myDocumentsPath
> > ChDir myDocumentsPath
> >
> > myFileName = Application.GetOpenFilename(filefilter:="Excel Files, *.xls")
> >
> > 'change back to the old directory
> > ChDrive CurPath
> > ChDir CurPath
> >
> > If myFileName = False Then
> > Exit Sub
> > End If
> >
> > 'do what you want--open the file???
> > set wkbk = workbooks.open(filename:=myfilename)
> >
> > End Sub
> >
> > Steven wrote:
> > >
> > > How do I create a macro to do a File Open and default to the 'My Documents'
> > > dialog box?
> > >
> > > Thank you,
> > >
> > > Steven

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File open dialog default to details Paul Windows Vista General Discussion 2 19th Nov 2008 11:52 PM
File open dialog default path hinesgg Microsoft Word Document Management 1 2nd Apr 2008 12:36 AM
file open dialog default path av Microsoft Word Document Management 0 2nd Apr 2008 12:32 AM
File Open dialog box default size Alan Roy Windows Vista File Management 1 6th Feb 2007 04:38 PM
Default to Details on File Open Dialog Don Windows XP General 2 9th May 2006 04:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:06 AM.