|
本帖最后由 lpzxhjp 于 2012-6-12 10:37 编辑
shaowu459 发表于 2012-6-10 22:09
你打开一个工作簿,然后在代码那里一下一下的按F8,并且随时查看代码的效果,比如说另存了文件,比如说启 ...
麻烦老师看看,谢谢!我的代码:
- Sub Winrar()
- Application.DisplayAlerts = False
- Dim TheRarexe As String 'WINRAR程序的位置
- Dim TheSource As String '压缩前的源文件
- Dim TheTarget As String '压缩好的文件
- Dim TheFileString As String
- Dim TheResult As Long
- If InStr(ActiveWorkbook.Name, "EH(lpzxhjp)") > 0 Then
- With ActiveWorkbook
- TheRarexe = "D:\Program Files\WinRAR\WinRAR" '系统WINRAR路径
- '.SaveCopyAs "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & "-" & .Name '另存为C盘根目录下,作为压缩用的临时文件
- .SaveCopyAs "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & Mid(.Name, 18, InStrRev(.Name, ".xl") - 1) '另存为C盘根目录下,作为压缩用的临时文件
- TheSource = "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & Mid(.Name, 18, InStrRev(.Name, ".xl") - 1) '临时文件作为压缩源文件
- TheTarget = "C:\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & Mid(.Name, 18, InStrRev(.Name, ".xl") - 18) & ".rar" '保存到目录
- TheFileString = TheRarexe & " a " & TheTarget & " " & TheSource
- TheResult = Shell(TheFileString, vbHide) '压缩
- Application.Wait Now + TimeValue("00:00:02")
- Kill TheSource '删除临时文件
- .SaveAs "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & Mid(.Name, 18, 99) '另存为C盘根目录下,作为压缩用的临时文件
- End With
- Else
- With ActiveWorkbook
- TheRarexe = "D:\Program Files\WinRAR\WinRAR" '系统WINRAR路径
- .SaveCopyAs "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & "-" & .Name '另存为C盘根目录下,作为压缩用的临时文件
- TheSource = "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & "-" & .Name '临时文件作为压缩源文件
- TheTarget = "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & "-" & Mid(.Name, 1, InStrRev(.Name, ".xl") - 1) & ".rar" '保存到目录
- TheFileString = TheRarexe & " a " & TheTarget & " " & TheSource
- TheResult = Shell(TheFileString, vbHide) '压缩
- Application.Wait Now + TimeValue("00:00:02")
- Kill TheSource '删除临时文件
- '.SaveAs "C:\Documents and Settings\Administrator\桌面\Excelhome\EH(lpzxhjp)-" & Format(Date, "yyyy.mm.dd") & "-" & .Name '另存为C盘根目录下,作为压缩用的临时文件
- End With
- End If
- Application.DisplayAlerts = True
- End Sub
复制代码
|
-
|