How to open dialog with specified folder?

A

avkokin

Hello.
I can open dialog "FileOpen" (the code below). But how can I open this
dialogue with specified folder? Moreover! How can I open the specified
folder (e.g. "C:\Documents and Setting\Anton Kokin\Application Data
\Microsoft\Office\Recent") if name's user (e.g. "Anton Kokin") maybe
is different? If this code will execute on others computers?
My code:
Sub getSpecFolder()
Dim oD As Dialog
Set oD = Dialogs(wdDialogFileOpen)
oD.Display
End Sub

Thank you.
 
J

Jonathan West

avkokin said:
Hello.
I can open dialog "FileOpen" (the code below). But how can I open this
dialogue with specified folder? Moreover! How can I open the specified
folder (e.g. "C:\Documents and Setting\Anton Kokin\Application Data
\Microsoft\Office\Recent") if name's user (e.g. "Anton Kokin") maybe
is different? If this code will execute on others computers?
My code:
Sub getSpecFolder()
Dim oD As Dialog
Set oD = Dialogs(wdDialogFileOpen)
oD.Display
End Sub

Thank you.

Sub getSpecFolder()
With Dialogs(wdDialogFileOpen)
.Name = "C:\Documents and Setting\Anton Kokin\Application
Data\Microsoft\Office\Recent\"
if .Display = -1 Then
MsgBox .Name
End If
End With
End Sub

The path you pass to the .Name property must have a closing \ character.
 
A

avkokin

Hello Jonathan. Thank you very much.
And if name's user unknow previously then how be? Maybe is there any
way to define name of user of this computer?
 
A

avkokin

Hello Jonathan.
Thank you.
But if user unknow then how to open this folder ("Recent")? Maybe is
there any way to define name's user of this computer?
 
H

Helmut Weber

Hi Anton,

user = Environ$("Username") & "\"
plus
..Name = "C:\Dokumente und Einstellungen\" & User & "*.*"
 
H

Helmut Weber

Hi Anton,

Sub getSpecFolder()
Dim user As String
user = Environ$("Username")
With Dialogs(wdDialogFileOpen)
.name = "C:\Benutzer\" & user & "\Dokumente\" ' no
.name = "C:\Benutzer\HelmutWeber\Dokumente\" ' no
MsgBox .name
.Show
End With
End Sub

sorry, it looked so simple, yet it doesn't work,
for reasons I can't understand nor explain.

All other samples did work, like:

Sub getSpecFolderA()
With Dialogs(wdDialogFileOpen)
' .name = "C:\Test\Word1\" ' ok
' .name = "C:\Test\Excel\" ' ok
.name = "C:\Office2003\" ' ok
MsgBox .name
.Show
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jonathan West

Helmut Weber said:
Hi Anton,

Sub getSpecFolder()
Dim user As String
user = Environ$("Username")
With Dialogs(wdDialogFileOpen)
.name = "C:\Benutzer\" & user & "\Dokumente\" ' no
.name = "C:\Benutzer\HelmutWeber\Dokumente\" ' no
MsgBox .name
.Show
End With
End Sub

sorry, it looked so simple, yet it doesn't work,
for reasons I can't understand nor explain.

All other samples did work, like:

Sub getSpecFolderA()
With Dialogs(wdDialogFileOpen)
' .name = "C:\Test\Word1\" ' ok
' .name = "C:\Test\Excel\" ' ok
.name = "C:\Office2003\" ' ok
MsgBox .name
.Show
End With
End Sub


Actually, it might be better to use the FileDialog object, like this

Sub getSpecFolderA()

Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
.AllowMultiSelect = False
.InitialFileName = "C:\Benutzer\HelmutWeber\Dokumente\"

If .Show = -1 Then
MsgBox .SelectedItems(1)
End If
End With
End Sub
 
A

avkokin

Dear colleagues.
I very grateful for your answers but it don't work so as I wanted.
Sorry for my inaccurate explaining. I try to say otherwise.
I want to get access to the folder RECENT (through dialog FileOpen) by
with VBA.
For example: there is the method GetSpecialFolder which returns the
special folder specified, e.g. the "Temp" folder with temporary files.
So I think (I don't know) what exist some method which can access to
folder Recent (with recent files) for Office.
Question: how?
 
A

avkokin

It probably get through FSO. But I can't open the folder Recent. I
don't understand. Example:
Sub getSpecFolder()
Dim oFSO As FileSystemObject
Dim oDialog As Dialog

Set oFSO = New FileSystemObject
Set oDialog = Dialogs(wdDialogFileOpen)
If oDialog.Display Then
oFSO.GetAbsolutePathName (MyRecent)
End If
Set oFSO = Nothing
End Sub

Public Function MyRecent() As String
MyRecent = FolderPath(CSIDL_RECENT)
End Function
 
J

Jonathan West

avkokin said:
It probably get through FSO. But I can't open the folder Recent. I
don't understand. Example:
Sub getSpecFolder()
Dim oFSO As FileSystemObject
Dim oDialog As Dialog

Set oFSO = New FileSystemObject
Set oDialog = Dialogs(wdDialogFileOpen)
If oDialog.Display Then
oFSO.GetAbsolutePathName (MyRecent)
End If
Set oFSO = Nothing
End Sub

Public Function MyRecent() As String
MyRecent = FolderPath(CSIDL_RECENT)
End Function

Randy Birch has a code sample for obtaining the paths of many different
system folders in Windows.

http://vbnet.mvps.org/index.html?code/browse/shpathidlist.htm

From the code sample, you need to copy & paste the following into a fresh
module.

1. The declarations before the first subroutine
2. The GetSpecialFolderLocation function

Change the scope of the routine and the various CISDL constants from Private
to Public, and you're ready to roll. The following line of code will then
retrieve the path of the Recent Files folder

sRecentFiles = GetSpecialFolderLocation(CSIDL_RECENT)
 
A

avkokin

That is more one code. And I can get folder Recent but not for
Office's documents (for all). Please give me any ideas...
Sub GetDesktopPath()
'source: http://puremis.net/excel/code/035.shtml
Dim WSHShell As Object
Set WSHShell = CreateObject("Wscript.Shell")
MsgBox "Your desktop is " & WSHShell.SpecialFolders("Desktop")
Set WSHShell = Nothing
End Sub
 
A

avkokin

Dear Jonathan, thank you. But it code and form don't worked.
Otherwise, it don't show the folder "Recent" with path "C:\Documents
and Settings\kokin\Application Data\Microsoft\Office\ðÏÓÌÅÄÎÉÅ ÆÁÊÌÙ
\". It show path - C:\Documents and Settings\kokin\Recent\ that is
uncorrect for me.
 
J

Jonathan West

Dear Jonathan, thank you. But it code and form don't worked.
Otherwise, it don't show the folder "Recent" with path "C:\Documents
and Settings\kokin\Application Data\Microsoft\Office\ðÏÓÌÅÄÎÉÅ ÆÁÊÌÙ
\". It show path - C:\Documents and Settings\kokin\Recent\ that is
uncorrect for me.

I don't think that path is a standard Windows system path. The Application
Data path is a standard path, so you can use the function I provided too get
that, and then append the rest of the path to it.
 
A

avkokin

I used follow code:
Sub getSpecFolder()
Dim myPath As String
myPath = Environ$("appdata") & "\" & "\Microsoft\Office\Recent\" 'Set
the path.BUT name this folder maybe diffferents independet of
language. Question: How to get universal name for this folder?

With Dialogs(wdDialogFileOpen)
.Name = myPath
If .Display = -1 Then
MsgBox .Name
End If
End With
End Sub

BUT name this folder ("Recent") maybe diffferents independet of
language of OS.
Question: How to get universal name for this folder?
 
A

avkokin

Dear collegues!
I found my solution (below), maybe it will helpfull:
Sub getSpecFolder_new()
Dim myPath As String
Dim objWSH
Dim bKey As String
Set objWSH = CreateObject("WScript.Shell")
bKey = objWSH.RegRead("HKCU\Software\Microsoft\Office\11.0\Common
\General\RecentFiles")
myPath = Environ$("appdata") & "\" & "\Microsoft\Office\" & bKey 'Set
the path.
With Dialogs(wdDialogFileOpen)
.Name = myPath
If .Display = -1 Then
MsgBox .Name
End If
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top