gpt4 book ai didi

excel - 打开 ZipFile,查找特定文件类型并保存文件名

转载 作者:行者123 更新时间:2023-12-04 23:56:37 24 4
gpt4 key购买 nike

所以我在这里发布了一个问题:

VBA - Find Specific Sub Folders by Name Identifiers

这个问题非常广泛,但我面临着需要帮助识别和解决的具体问题。现在,我设法在原始帖子中解决了这些问题,但是,仍有很大一部分问题没有得到解答,我只想在能够发布完整结果时关闭问题。

目前,我仍然需要做的是最后 4 个步骤:

  • 打开压缩文件
  • 查找 .png 扩展名
  • 获取 .png 文件的名称
  • 将姓名放在excel的单元格中

  • 我面临的问题是正确打开 zip 文件。我在这方面经历了很多帖子,但似乎没有什么对我有用。

    我最接近完成任务的是我在这里找到的:

    https://www.ozgrid.com/forum/forum/help-forums/excel-general/109333-how-to-count-number-of-items-in-zip-file-with-vba-2007

    我想,如果至少我能够输入 zip 文件,我就可以从那里开始工作。但是,唉,我仍然坚持只是试图打开文件。

    这是我拥有的代码(使用上面的链接):
    Sub CountZipContents()

    Dim zCount As Double, CountContents As Double
    Dim sh As Object, fld As Object, n As Object
    Dim FSO As Object

    CountContents = 0
    zCount = 0

    x = "C:\Users\UserName\Desktop\Today\MyFolder\"

    Set FSO = CreateObject("Scripting.FileSystemObject")

    If FSO.FolderExists(x) Then

    For Each FileInFolder In FSO.GetFolder(x).Files

    If Right(FileInFolder.Name, 4) = ".png" Then

    CountContents = CountContents + 1

    ElseIf Right(FileInFolder.Name, 4) = ".Zip" Then

    Set sh = CreateObject("Shell.Application")
    Set ZipFile = sh.Namespace(CVar(x & "\" & FileInFolder.Name))

    Debug.Print FileInFolder.Name

    For Each fileInZip In ZipFile.Items

    If LCase(fileInZip) Like LCase("*.png") Then

    CountContents = CountContents + 1

    End If

    Next

    End If

    Next FileInFolder

    End If

    Set sh = Nothing

    End Sub

    我得到的问题是在这一行:
    For Each fileInZip In ZipFile.Items

    错误信息:

    Object variable or With block not set



    每当我尝试使用 Shell ,如下所示:
    Dim oShell As New Shell

    我收到此错误:

    User-defined type not defined



    使用以下内容:

    友情链接 https://msdn.microsoft.com/en-us/library/windows/desktop/bb776890(v=vs.85).aspx
    Dim oApp As Object

    Set oApp = CreateObject("WScript.Shell")

    'get a shell object
    Set oApp = CreateObject("Shell.Application")

    If oApp.Namespace(ZipFile).Items.count > 0 Then

    我收到此错误:

    Object doesn't support this property or method



    在这条线上:
    If oApp.Namespace(ZipFile).Items.count > 0 Then

    对我尝试过的链接的引用:

    https://wellsr.com/vba/2015/tutorials/open-and-close-file-with-VBA-Shell/
    http://www.vbaexpress.com/forum/showthread.php?38616-quot-shell-quot-not-work-in-Excel
    Excel VBA - read .txt from .zip files

    我只是不明白为什么这一步要花这么多时间才能完成。

    最佳答案

    您的主要问题是一个非常简单的问题:您的路径 "C:\Users\UserName\Desktop\Today\MyFolder\"已经包含一个尾随反斜杠,并且当您设置 ZipFile -变量,您在路径和文件名之间添加另一个。这将导致 shell -命令失败和ZipFilenothing .

    代码有一些小问题。我建议使用 GetExtensionName您的 FileSystemObject 获取扩展名并将其转换为小写,以便捕获所有文件,无论它们是否为 .PNG , .png.Png

       For Each FileInFolder In FSO.GetFolder(x).Files
    Dim fileExt As String
    fileExt = LCase(FSO.GetExtensionName(FileInFolder.Name))

    If fileExt = "png" Then
    CountContents = CountContents + 1
    Debug.Print "unzipped " & FileInFolder.Name
    ElseIf fileExt = "zip" Then

    Dim ZipFileName As String, ZipFile, fileInZip
    Set sh = CreateObject("Shell.Application")
    ZipFileName = x & FileInFolder.Name
    Set ZipFile = sh.Namespace(CVar(ZipFileName))

    For Each fileInZip In ZipFile.Items
    If LCase(FSO.GetExtensionName(fileInZip)) = "png" Then
    CountContents = CountContents + 1
    Debug.Print "zipped in " & FileInFolder.Name & ": " & fileInZip
    End If
    Next
    End If
    Next FileInFolder

    另外强烈建议使用 Option Explicit并定义所有变量。并将命令拆分成更小的部分。这只需花费您几秒钟的时间输入额外的行,但在调试代码时会有所帮助:
    ' Instead of
    ' Set ZipFile = sh.Namespace(CVar(x & "\" & FileInFolder.Name))
    ' write
    Dim fName as string
    fName = x & "\" & FileInFolder.Name; ' Now you can check fName and see the problem.
    Set ZipFile = sh.Namespace(CVar(fName))

    关于excel - 打开 ZipFile,查找特定文件类型并保存文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55260788/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com