Using Wscript.Shell in Word VBA?

E

Ed

I'm attempting to use Wscript.Shell through Word 2002 VBA to access a
shortcut's TargetPath. Actually, right now I'm just attempting to set an
object to Wscript.Shell!! I have a reference to the Windows Script Host
Object Model library (wshom.ocx). I'm using the following code just to try
and get a handle on using this, but it keep throwing "Error 424 - object
required" on the Set line. Can anyone give me a boost? Or an alternate
method?

Ed

Sub GetDesktop()

Dim WshShell
Set WshShell = Wscript.CreateObject("Wscript.Shell")
MsgBox "Your desktop is " & WshShell.SpecialFolders("Desktop")

End Sub
 
E

Ed

Never mind! I should have Googled the NG ~before~ posting!! As usual, the
experts have resolved this already, and I am behind the curve!
Ed
 
H

Helmut Weber

Hi Ed,
Set WshShell = Wscript.CreateObject("Wscript.Shell") ' ???

Sub GetDesktop()
Dim WshShell As Object
Set WshShell = CreateObject("Wscript.Shell")
MsgBox "Your desktop is " & WshShell.SpecialFolders("Desktop")
Set WshShell = Nothing
End Sub


Dim WshShell As Object <<<

is some kind of a compromise,
as I could find the right type in the intellisense.
Maybe there isn't one at all.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
E

Ed

Hi, Helmut. I was scanning lots of programming web sites to come up with
what I originally posted. I think I might have been trying to use something
other than VBA in my macro! Using some hints I found in a Word.VBA NG post,
I put together the following. It works for me in Windows XP with Word 2002.
Ed

Sub TryAgain2()

Dim strName As String
' Requires a reference to Windows Script Host Object Model
Dim objShell As IWshRuntimeLibrary.WshShell
Dim objLink As WshShortcut
' Requires a reference to Microsoft Scripting Runtime Library
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

' Get UserName
UName = Environ$("Username")
' Set folder path
MyPath = "C:\Documents and Settings\" & _
UName & "\Recent\"

' Get files
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(MyPath)
If Not objFolder Is Nothing Then
For Each objFile In objFolder.Files
With objFile
strName = objFile.Name

Set objShell = New IWshRuntimeLibrary.WshShell
Set objLink = objShell.CreateShortcut(MyPath & strName)
Debug.Print objLink.TargetPath

End With
Next objFile
End If

Set objLink = Nothing
Set objShell = Nothing
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing

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