PDA

View Full Version : Solved: How to get value from registry?



akokin
09-12-2008, 12:16 AM
Hello.
There is the registry keys for recent files:
HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Common\General\RecentFiles
How to get the value this key?
Thank you very much.

akokin
09-12-2008, 02:26 AM
Dear collegues!
I found my solution (below), maybe it will helpful for somebody:
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

TonyJollans
09-13-2008, 12:38 AM
If you want to do it without instantiating a Shell object ..

bKey = System.PrivateProfileString("", _
"HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Common\General", _
"RecentFiles")

akokin
09-13-2008, 03:45 AM
Tony, thank you very much. I will use this method (it is better).

fumei
09-15-2008, 10:27 AM
Also, you can use the RecentFiles collection in Word itself.


Dim j As Long
Dim MyList As String
For j = 1 To Application.RecentFiles.Count - 1
MyList = MyList & Application.RecentFiles(j) _
& vbCrLf
Next

MsgBox MyList

akokin
09-15-2008, 10:36 AM
Hello. Thank you, but it inform me "run-time error 5152. Method 'Name' of object RecentFiles is failed"...

fumei
09-15-2008, 10:47 AM
I just copied the code from my post above and ran it. It worked perfectly.

What version of Word are you using?

fumei
09-15-2008, 10:48 AM
Sorry, you are using 2003, right? Office 11. Still, I thought 2003 still had RecentFiles as a legitimate collection.

akokin
09-15-2008, 10:50 AM
I use WinXP and Word 2003. I use this macro simple:
Sub rflist()
Dim j As Long
Dim MyList As String
For j = 1 To Application.RecentFiles.Count - 1
MyList = MyList & Application.RecentFiles(j) & vbCrLf
Next
MsgBox MyList
End Sub

fumei
09-15-2008, 11:27 AM
It works prefectly in 2002. I guess the RecentFiles collection was removed from 2003.

akokin
09-15-2008, 11:34 AM
here need use FileSystemObject

TonyJollans
09-15-2008, 01:27 PM
Gerry,

akokin is trying to read the registry to get the name of the folder where shortcuts to 'recent files' are kept, not trying to access Word's MRU list. Exactly what goes in this folder, I'm afraid, bemuses me.

For the record, the RecentFiles collection still exists in 2007.

fumei
09-16-2008, 08:51 AM
I understand.

But why would there be a ""run-time error 5152. Method 'Name' of object RecentFiles is failed"..." on the code?

TonyJollans
09-16-2008, 11:58 AM
I don't see where it is being used - or trying to be used - but RecentFiles (the collection) does not have a Name property (RecentFile (the object) does).

fumei
09-16-2008, 01:09 PM
"I don't see where it is being used - or trying to be used "

Hmmmm...


I use WinXP and Word 2003. I use this macro simple:



Sub rflist()
Dim j As Long
Dim MyList As String
For j = 1 To Application.RecentFiles.Count - 1
MyList = MyList & Application.RecentFiles(j) & vbCrLf
Next
MsgBox MyList
End Sub

akokin
09-17-2008, 04:42 AM
Hello.
Very strangely! But on one computer (in my office) this code worked. But at home - not! I wonder...

TonyJollans
09-21-2008, 12:33 AM
Sorry, Gerry, I was being a bit slow. In my defence Anton does quote the message wrongly but I still should have seen it.

Anton -- This is really wierd. I suspect some corruption in your Word Data key Settings value, and especially so if it only happens on one computer. Alternatively, it could, I suppose, be a language issue - is there anything that the missing files have in common?

akokin
09-22-2008, 04:36 AM
Tony, I understood why I got error. Into list of recent files was one document which was deleted earlier. And I have one question: how to define existence of the files from the list of recent files?
Thank you.

TonyJollans
09-22-2008, 06:28 AM
Dir(FileName) is as easy as anything - but if RecentFile.Name gives an error you won't be able to do that - or anything except trap the error.

akokin
09-22-2008, 06:38 AM
Thank you. Yes, I used this function. And I will use the operator "On errorresume next". I think that it is right. Otherwise it not way.