简体   繁体   中英

Excel 2010 VBA - XMLHTTP

what exactly is the purpose of these lines of codes:

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"

I have tried running the XMLHTTP code with just the .Open"GET" and .responseText statements with and w/o the above two lines and didn't notice any difference in the outputs. Am I missing something or do the two lines ensure some significant parameters? Am using Excel 2010 VBA. Appreciate any insight...

The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the case of the HEAD method, the media type that would have been sent had the request been a GET. If no file content is uploaded, this does not server any purpose in the request header.

The User-Agent request-header field contains information about the user agent originating the request. This is for statistical purposes, the tracing of protocol violations, and automated recognition of user agents for the sake of tailoring responses to avoid particular user agent limitations. In many cases, skipping this header will not cause any difference as the web pages served will be cross browser compatible and are not tailored to fit the user agent.

More details can be found in the Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content RFC 7231 section 3.1.1.5 for Content-Type and RFC 7231 section 5.5.3 for User-Agent

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM