Excel 2010 VBA - XMLHTTP

时间:2014-05-29 16:24:34

标签: excel-vba xmlhttprequest vba excel

这些代码行的目的究竟是什么:

XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"

我尝试使用.Open"GET".responseText语句运行XMLHTTP代码,并且没有上述两行,并且没有注意到输出的任何差异。我错过了什么或两条线确保一些重要的参数?我使用的是Excel 2010 VBA。欣赏任何洞察力......

3 个答案:

答案 0 :(得分:0)

Content-Type entity-header字段指示发送给收件人的实体主体的媒体类型,或者在HEAD方法的情况下,指示已发送的媒体类型请求是GET。如果没有上传文件内容,则不会在请求标头中为任何目的服务。

User-Agent 请求标头字段包含有关发起请求的用户代理的信息。这是出于统计目的,跟踪协议违规以及自动识别用户代理以便定制响应以避免特定的用户代理限制。在许多情况下,跳过此标题不会产生任何差异,因为所提供的网页将与浏览器兼容,并且不适合用户代理。

更多细节可以在超文本传输​​协议(HTTP / 1.1)中找到:语义和内容 RFC 7231 section 3.1.1.5 for Content-TypeRFC 7231 section 5.5.3 for User-Agent

答案 1 :(得分:0)

Sub ListAllFiles()
    Dim fs As FileSearch, ws As Worksheet, i As Long
    Set fs = Application.FileSearch
    With fs
        .SearchSubFolders = False ' set to true if you want sub-folders included
        .FileType = msoFileTypeAllFiles 'can modify to just Excel files eg with msoFileTypeExcelWorkbooks
        .LookIn = "C:\Users\user\Downloads\" 'modify this to where you want to serach
        If .Execute > 0 Then
            Set ws = Worksheets.Add
            For i = 1 To .FoundFiles.Count
                ws.Cells(i, 1) = .FoundFiles(i)
            Next
        Else
            MsgBox "No files found"
        End If
    End With
End Sub

Sub LoopThroughFiles()
    Dim MyObj As Object, MySource As Object, file As Variant
    Set MySource = MyObj.GetFolder("C:\Users\user\Downloads\")
    For Each file In MySource.Files
        If InStr(file.Name, "test") > 0 Then
            MsgBox "found"
            Exit Sub
        End If
        Next file
    End Sub



    Sub ListFiles()

'Set a reference to Microsoft Scripting Runtime by using
'Tools > References in the Visual Basic Editor (Alt+F11)

'Declare the variables
        Dim objFSO As Scripting.FileSystemObject
        Dim objTopFolder As Scripting.Folder
        Dim strTopFolderName As String

'Insert the headers for Columns A through F
        Range("A1").Value = "File Name"
        Range("B1").Value = "File Size"
        Range("C1").Value = "File Type"
        Range("D1").Value = "Date Created"
        Range("E1").Value = "Date Last Accessed"
        Range("F1").Value = "Date Last Modified"

'Assign the top folder to a variable
        strTopFolderName = "C:\Users\user\Downloads"

'Create an instance of the FileSystemObject
        Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get the top folder
        Set objTopFolder = objFSO.GetFolder(strTopFolderName)

'Call the RecursiveFolder routine
        Call RecursiveFolder(objTopFolder, True)

'Change the width of the columns to achieve the best fit
        Columns.AutoFit

    End Sub

    Sub RecursiveFolder(objFolder As Scripting.Folder, IncludeSubFolders As Boolean)

'Declare the variables
        Dim objFile As Scripting.file
        Dim objSubFolder As Scripting.Folder
        Dim NextRow As Long

'Find the next available row
        NextRow = Cells(Rows.Count, "A").End(xlUp).Row + 1

'Loop through each file in the folder
        For Each objFile In objFolder.Files
            Cells(NextRow, "A").Value = objFile.Name
            Cells(NextRow, "B").Value = objFile.Size
            Cells(NextRow, "C").Value = objFile.Type
            Cells(NextRow, "D").Value = objFile.DateCreated
            Cells(NextRow, "E").Value = objFile.DateLastAccessed
            Cells(NextRow, "F").Value = objFile.DateLastModified
            NextRow = NextRow + 1
            Next objFile

'Loop through files in the subfolders
            If IncludeSubFolders Then
                For Each objSubFolder In objFolder.SubFolders
                    Call RecursiveFolder(objSubFolder, True)
                    Next objSubFolder
                End If

            End Sub


            Sub Example3()

                Dim varDirectory As Variant
                Dim flag As Boolean
                Dim i As Integer
                Dim strDirectory As String

                strDirectory = "C:\Users\user\Downloads\"
                i = 1
                flag = True
                varDirectory = Dir(strDirectory, vbNormal)

                While flag = True
                    If varDirectory = "" Then
                        flag = False
                    Else
                        Cells(i + 1, 1) = varDirectory
                        Cells(i + 1, 2) = strDirectory + varDirectory
'returns the next file or directory in the path
                        varDirectory = Dir
                        i = i + 1
                    End If
                Wend


            End Sub


            Sub Example1()
                Dim objFSO As Object
                Dim objFolder As Object
                Dim objFile As Object
                Dim i As Integer

'Create an instance of the FileSystemObject
                Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
                Set objFolder = objFSO.GetFolder("C:\Users\user\Downloads\")
                i = 1
'loops through each file in the directory and prints their names and path
                For Each objFile In objFolder.Files
'print file name
                    Cells(i + 1, 1) = objFile.Name
'print file path
                    Cells(i + 1, 2) = objFile.Path
                    i = i + 1
                    Next objFile
                End Sub

答案 2 :(得分:0)

Sub Quik_FileOpen(ByRef FileLocation As String)
    Workbooks.Open FileName:=FileLocation, Notify:=False
End Sub

Public Function FileIsOpen(ThisFileName As String)
    Dim TestWorkbook As Workbook
    Set TestWorkbook = Nothing
    On Error Resume Next
    Set TestWorkbook = Workbooks((ThisFileName))
    On Error GoTo 0

    If TestWorkbook Is Nothing Then
        FileIsOpen = "N"
    Else
        FileIsOpen = "Y"
    End If
End Function