title: VBA Error Requesting Header
body: <p>I'm getting an error requesting the header.</p>
  
  <blockquote>
    <p>Error: Run-time error '2147012746 (80072f76)':</p>
    
    <p>The requested header was not found</p>
  </blockquote>
  
  <p>Debugging it refers to:</p>
  
  <blockquote>
    <p>PDFdownloadURL = .getResponseHeader("Location")</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>Dim baseURL As String, searchResultsURL As String, pdfURL As String, PDFdownloadURL As String
  Dim httpReq As Object
  Dim HTMLdoc As Object
  Dim PDFlink As Object
  Dim cookie As String
  Dim downloadFolder As String, localFile As String
  Dim fileNum As Integer, fileBytes() As Byte
  
  'Folder in which the downloaded file will be saved
  
  downloadFolder = ThisWorkbook.Path
  If Right(downloadFolder, 1) &lt;&gt; "\" Then downloadFolder = downloadFolder &amp; "\"
  
  baseURL = "http://recorder.maricopa.gov/recdocdata/"
  searchResultsURL = baseURL &amp; "GetRecDataDetail.aspx?rec=19930074944&amp;suf=&amp;bdt=1/1/1947&amp;edt=11/18/2016&amp;nm=&amp;doc1=&amp;doc2=&amp;doc3=&amp;doc4=&amp;doc5="
  
  Set httpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
  
  With httpReq
  
      'Send GET to request search results page
  
      .Open "GET", searchResultsURL, False
      .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"
      .Send
      cookie = .getResponseHeader("Set-Cookie")
  
      'Put response in HTMLDocument for parsing
      Set HTMLdoc = CreateObject("HTMLfile")
      HTMLdoc.body.innerHTML = .responseText
  
      'Get PDF URL from pages link
      '&lt; a id="ctl00_ContentPlaceHolder1_lnkPages" title="Click to view unofficial document"
      ' href="unofficialpdfdocs.aspx?rec=19930074944&amp;pg=1&amp;cls=RecorderDocuments&amp;suf=" target="_blank"&gt;11&lt; /a&gt;
  
      Set PDFlink = HTMLdoc.getElementById("ctl00_ContentPlaceHolder1_lnkPages")
      pdfURL = Replace(PDFlink.href, "about:", baseURL)
  'Send GET request to the PDF URL with automatic http redirects disabled.  This returns a http 302 status (Found) with the Location header containing the URL of the PDF file
  
  .Open "GET", pdfURL, False
  .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"
  .setRequestHeader "Referer", searchResultsURL
  .setRequestHeader "Set-Cookie", cookie
  .Option(WinHttpRequestOption_EnableRedirects) = False
  .Send
  PDFdownloadURL = .getResponseHeader("Location")
  
  'Send GET to request the PDF file download
  
  .Open "GET", PDFdownloadURL, False
  .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/46.0"
  .setRequestHeader "Referer", pdfURL
  .Send
  
  'Save response bytes in the local file
  
  If .Status = 200 Then
      localFile = downloadFolder &amp; Mid(PDFdownloadURL, InStrRev(PDFdownloadURL, "/") + 1)
      If Dir(localFile) &lt;&gt; "" Then Kill localFile
      fileBytes = .responseBody
      fileNum = FreeFile
      Open localFile For Binary Access Write As #fileNum
      Put #fileNum, 1, fileBytes
      Close #fileNum
      MsgBox "Downloaded " &amp; localFile
  Else
      MsgBox "Download failed " &amp; .statusText
  End If
  
  End With
  End Sub
  </code></pre>
  
  <p><a href="http://recorder.maricopa.gov/recdocdata/GetRecDataDetail.aspx?rec=19930074944&amp;suf=" rel="nofollow noreferrer">Link to Webpage:</a></p>
  
  <p>Any help would be greatly appreciated. Thanks!</p>