本文介绍了VBA:Zip文件输出重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我解压缩时,我正在尝试重命名文件的输出。我试过重命名为 test.xls ,但是它抛出一个对象变量未设置错误。除了扫描 .datecreated 文件夹中的所有文件之外,还有其他方法吗?如果有办法重命名解压缩文件,那将是最佳选择。

I'm trying to rename the output of my file when I unzip. I have tried renameing to test.xls But it throws a object variable not set error. Is there another way other then scanning .datecreated on all the files in the folder? If there is a way to rename the file on unzip that would be best.

Set oApp = CreateObject("Shell.Application")
    oApp.NameSpace("C:\Users\**\Downloads\TempFolder\test.xls").CopyHere oApp.NameSpace("C:\Users\**\Downloads\TempFolder\testzip.zip").Items
Set oApp = Nothing


推荐答案

CopyHere 方法仅对文件夹对象(在压缩时包括zip文件)有效,而对于常规文件。因此出现错误消息。

The CopyHere method is only valid for folder objects (this includes zip files when zipping), not for regular files. Hence the error message.

请参见或。

我将zip解压缩到保证为的文件夹中。

然后移动(单个)文件到您的实际目标文件夹,并在此过程中将其重命名。

I would extract the zip to a folder that is guaranteed to be empty.
Then move the (single) file to your actual target folder, renaming it in the process.

Const Zipfile = "C:\Users\**\Downloads\TempFolder\testzip.zip"
Const EmptyFolder = "C:\Users\**\Downloads\EmptyFolder"
Const TargetFolder = "C:\Users\**\Target"
Dim strFile As String

Set oApp = CreateObject("Shell.Application")
' Unzip into empty folder
oApp.NameSpace(EmptyFolder).CopyHere oApp.NameSpace(Zipfile).Items
Set oApp = Nothing

' Get first and only file
strFile = Dir(EmptyFolder & "\*.*")
' Move and rename
Name EmptyFolder & "\" & strFile As TargetFolder & "\test.xls"

这篇关于VBA:Zip文件输出重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 18:59
查看更多