VBA处理压缩文件示例
(2018-05-23 18:03:37)
标签:
excelvbaword代码 |
分类: VBA专区 |
01.
Sub
Unzip_Ex()
02.
Dim
FSO
As
Object
03.
Dim
oApp
As
Object
04.
Dim
Fname
As
Variant
05.
Dim
FileNameFolder
As
Variant
06.
Dim
DefPath
As
String
07.
Dim
strDate
As
String
08.
09.
Fname
= Application.GetOpenFilename _
10.
(filefilter:=
"Zip
Files (*.zip), *.zip"
, _
11.
MultiSelect:=
False
)
12.
If
Fname =
False
Then
13.
'
do nothing
14.
Else
15.
'
新文件夹的根目录
16.
'
你也可以使用 DefPath = "C:\Users\HUP\test\"
17.
DefPath
= Application.DefaultFilePath
18.
If
Right(DefPath, 1) <>
"\"
Then
19.
DefPath
= DefPath &
"\"
20.
End
If
21.
22.
'
创建新的文件夹名称
23.
strDate
= Format(Now,
" dd-mm-yy
h-mm-ss"
)
24.
FileNameFolder
= DefPath &
"MyUnzipFolder "
&
strDate &
"\"
25.
26.
'
在DefPath下创建文件夹
27.
MkDir
FileNameFolder
28.
29.
'
将文件解压到新创建的文件夹下
30.
Set
oApp =
CreateObject(
"Shell.Application"
)
31.
32.
oApp.
Namespace
(FileNameFolder).CopyHere
oApp.
Namespace
(Fname).items
33.
34.
'
如果你只需要解压一个文件,可以使用:
35.
'oApp.Namespace(FileNameFolder).CopyHere
_
36.
'oApp.Namespace(Fname).items.Item("test.txt")
37.
38.
MsgBox
"You find the files here: "
&
FileNameFolder
39.
40.
On
Error
Resume
Next
41.
Set
FSO =
CreateObject(
"scripting.filesystemobject"
)
42.
FSO.deletefolder
Environ(
"Temp"
) &
"\Temporary Directory*"
,
True
43.
End
If
44.
End
Sub