Namespace()上的VBA错误.CopyHere ...和... Namespace()。items

时间:2016-07-27 14:41:50

标签: excel vba excel-vba namespaces

我试图从其他帖子修改VBA脚本(26486871)。

该脚本将下载Zip文件,提取文本文件并将数据导入Excel。

我不了解VBA所以我会一次一个地处理每个功能。

  1. 使用随机名称创建临时目录................................完成
  2. 从公共服务器下载Zip文件....................................... ........完成
  3. 提取文本文件(20MB,制表符分隔).................................... ..........错误
  4. 将数据导入打开的工作表(覆盖现有数据)......尚未
  5. 在提取部分,我在以下行收到运行时错误:

    objOApp.Namespace(FileNameToUnzip).CopyHere objOApp.Namespace(varFileNameFolder).items, 256
    

    "运行时错误' 91:对象变量或未设置块变量。"

    当我在调试模式下将光标悬停在变量上时,目录和文件名是正确的。 我不确定未设置的是什么。我感谢任何帮助。

    Option Explicit
    'Main Procedure
    Sub DownloadExtractAndImport()
    
    Dim url As String
    Dim targetFolder As String, targetFileZip As String, targetFileTXT As String
    
    Dim wkbAll As Workbook
    Dim wkbTemp As Workbook
    Dim sDelimiter As String
    Dim newSheet As Worksheet
    
    url = "http://www.example.com/data.zip"
    targetFolder = Environ("TEMP") & "\" & RandomString(6) & "\"
    MkDir targetFolder
    targetFileZip = targetFolder & "data.zip"
    targetFileTXT = targetFolder & "data.txt"
    
    '1 download file
    DownloadFile url, targetFileZip
    
    '2 extract contents
    Call UnZip(targetFileZip, targetFolder)
    
    
    End Sub
    
    Private Sub DownloadFile(myURL As String, target As String)
    
    Dim WinHttpReq As Object
    Dim oStream As Object
    Set WinHttpReq = CreateObject("Msxml2.ServerXMLHTTP")
    WinHttpReq.Open "GET", myURL, False
    WinHttpReq.send
    
    myURL = WinHttpReq.responseBody
    If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    oStream.SaveToFile target, 1 ' 1 = no overwrite, 2 = overwrite
    oStream.Close
    End If
    
    End Sub
    
    Private Function RandomString(cb As Integer) As String
    
    Randomize
    Dim rgch As String
    rgch = "abcdefghijklmnopqrstuvwxyz"
    rgch = rgch & UCase(rgch) & "0123456789"
    
    Dim i As Long
    For i = 1 To cb
    RandomString = RandomString & Mid$(rgch, Int(Rnd() * Len(rgch) + 1), 1)
    Next
    
    End Function
    
    Private Function UnZip(PathToUnzipFileTo As Variant, FileNameToUnzip As Variant)
    
    Dim objOApp As Object
    Dim varFileNameFolder As Variant
    varFileNameFolder = PathToUnzipFileTo
    Set objOApp = CreateObject("Shell.Application")
    objOApp.Namespace(FileNameToUnzip).CopyHere objOApp.Namespace(varFileNameFolder).items, 256
    
    End Function
    

2 个答案:

答案 0 :(得分:0)

Comintem是对的,您应该使用添加的代码编辑旧问题,而不是发布几乎相同的新问题。也许保留这个问题并删除旧问题。

要回答您的问题,您似乎将错误顺序的参数传递给UnZip函数。尝试将该行更改为:

Call UnZip(targetFolder, targetFileZip)

<强>更新

在创建对象并在一行中调用其属性/方法时,很难诊断问题。根据您的问题的性质来判断,您的VBA知识似乎并不是特别庞大,并且您正试图通过将各种Web代码捆绑在一起来构建可行的解决方案。判断这种方法不是我的立场,但如果采用这种方法,我的建议是一次创建一个对象并一次调用一个方法。这样可以更容易诊断代码。

我尝试重写代码的元素,以向您展示如何完成此操作。这可能有点矫枉过正,但至少它会帮助您确定任何问题的准确位置。显然,将文件夹名称更改为您自己的名称。

Dim mainFolder As String
Dim zipFolder As String
Dim destinationFolder As String
Dim oShell As Object
Dim oMainFolder As Object
Dim oDestinatioFolder As Object
Dim oZipFolder As Object
Dim oZipItems As Object

'Define the folder names
mainFolder = "C:\Users\User\Downloads\SO\" 'change to your own folder name
zipFolder = "sqlite-shell-win32-x86-3071700.zip" 'an old sqlite download = change to your name
destinationFolder = Left(zipFolder, Len(zipFolder) - 4) 'name of zip folder minus the '.zip'

'Create the new destination folder
MkDir mainFolder & destinationFolder

'Acquire the folder items
'create the shell object
Set oShell = CreateObject("Shell.Application")
'create the main folder object as Folder3 item
Set oMainFolder = oShell.Namespace(CVar(mainFolder)) 'argument must be a variant
'create the destination folder object as Folder3 item
Set oDestinatioFolder = oMainFolder.Items.Item(CVar(destinationFolder & "\")).GetFolder
'create the zip folder object as Folder3
Set oZipFolder = oMainFolder.Items.Item(CVar(zipFolder)).GetFolder

'Extract the zip folder items and write to desination folder
oDestinatioFolder.CopyHere oZipFolder.Items, 256

答案 1 :(得分:0)

Dim mainFolder As String
Dim zipFolder As String
Dim destinationFolder As String
Dim oShell As Object
Dim oMainFolder As Object
Dim oDestinatioFolder As Object
Dim oZipFolder As Object
Dim oZipItems As Object

替换为

Dim mainFolder As Variant
Dim zipFolder As Variant
Dim destinationFolder As Variant
Dim oShell As Object
Dim oMainFolder As Object
Dim oDestinatioFolder As Object
Dim oZipFolder As Object
Dim oZipItems As Object