Focus problem

M

Michele

I really don't know anything about programming... I am
trying to create code to open windows explorer from within
Word and then move to and ultimately delete a folder. So
far I have the following:

Public Sub delfolder()

Dim winFolder As String

winFolder = Environ("WINDIR")

Shell winFolder & "\explorer.exe", vbNormalFocus

SendKeys "Combined"

End Sub

The code opens Explorer as I wanted but every time it
types the "combined" into my Word document. Why is the
focus not staying on Windows Explorer? Please help!
 
M

Mark Tangard

Michele,

SendKeys is notoriously unreliable. In this case, though,
assuming it's going to work, you want the SendKeys line
*before* the Shell line.

But could this be done entirely in VBA? You can use the
VBA RmDir statement to delete a folder and Kill to delete
a file.
 
D

DeeJee

Public Sub delfolder()
Dim winFolder As String
winFolder = Environ("WINDIR")
Shell winFolder & "\explorer.exe", vbNormalFocus
SendKeys "Combined"
End Sub
The code opens Explorer as I wanted but every time it
types the "combined" into my Word document. Why is the
focus not staying on Windows Explorer? Please help!

Don't know why you want to send a string to explorer,
because explorer can't receive a string that way.
And why open explorer to remove a Dir?
If you want to remove C:\MyDir
use in VBAcode: (look also in help of VBA for ChDir, Kill...)
ChDir "C:\MyDir" 'Go to the Dir to remove
Kill "*.*" 'But there may not be any open file in the Dir!
ChDir "C:\"
RmDir "C:\MyDir" 'Dir to remove has to be empty first!
 
S

Steve Lang

FWIW,
If the directory you want to delete has multiple subdirectories using the
Kill and RmDir is a pain since you have to go through all the sub dirs to
kill the files, then use RmDir to remove the directories. I found a much
faster method that deletes the folder and all it contains in one shot. You
need to have Microsoft Scripting Runtime (scrrun.dll) referenced in your
project first:
Function Foo(strKillFolder as String)

Dim FSO as FileSystemObject

Set FSO = CreateObject("Scripting.FileSystemObject")

FSO.DeleteFolder strKillFolder, True

Set FSO = Nothing

End Function

Steve
 
D

DeeJee

I found a much faster method that deletes the folder and all it contains in
one shot.

Be carefull with FSO, because on most systems it will not work
because the administrator disabled the scripting runtime!
 
S

Steve Lang

But when you are the admin - insert appropriate maniacial laughter here -
the FSO makes life good!

S
 

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