0

While running my code to print web pages to PDF there seems to be an issue with the file pathing as the "Run-time error '52'; Bad File name or number " shows but I can't figure out what it is. I confirmed that path name and continue to get the same error. I tried to unlink my OneDrive but there seems to be no change. Also if anyone can help name the files specific things that would be very helpful too. (i.e. first PDF has name General, second L&L, etc.)

Any help is appreciated.

Sub Test2()
Dim i As Long
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object

On Error Resume Next
    Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
    If Err.Number <> 0 Then
        Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    End If
On Error GoTo 0

If Dir("C:\MyDownloads", vbDirectory) = Empty Then MkDir "C:\MyDownloads"

For i = 1 To 7
    MyFile = Cells(i, 1).Text
    TempFile = Right(MyFile, InStr(1, StrReverse(MyFile), "/") - 1)
    WHTTP.Open "GET", MyFile, False
    WHTTP.Send
    FileData = WHTTP.ResponseBody
    
    FileNum = FreeFile
    Open "C:\MyDownloads\" & TempFile For Binary Access Write As #FileNum
        Put #FileNum, 1, FileData
    Close #FileNum
Next
Set WHTTP = Nothing
MsgBox "Open the folder [ C:\MyDownloads ] for the downloaded file..."
End Sub

Edit

TempFile is setting incorrectly, assumingly because the URL contains slashes. The cell value is "https://psearch.kitsapgov.com/pdetails/Details?parcel=132501-1-030-1005&page=general" and TempFile is only showing as "Details?parcel=132501-1-030-1005&page=general"

  • 2
    You've mentioned "pathing error", " issue with the file pathing" and "same error". Neither of those is a valid description of the error or the place it originates from. – GSerg Aug 16 '21 at 22:04
  • Fixed to show the correct error message – WindChill324 Aug 16 '21 at 23:26
  • 1
    What is FreeFile in `FileNum = FreeFile`? Is that a built-in method? Seems to come out of nowhere. (I have used VBA but this is new to me.) – wazz Aug 16 '21 at 23:40
  • Assigns an unused file number to the file, yes it's a built-in method – WindChill324 Aug 16 '21 at 23:43
  • 2
    Please include what the value of `TempFile` is when you get the error. Also, double check to see if the file might already exist. – braX Aug 17 '21 at 00:10
  • @braX Edit shows TempFile value, no files exist of the given type – WindChill324 Aug 17 '21 at 00:29
  • 2
    The filename has invalid characters in it. (questions marks are not allowed either) https://stackoverflow.com/questions/50846340/remove-illegal-characters-while-saving-workbook-excel-vba – braX Aug 17 '21 at 00:30
  • @braX Is there a potential fix? Instead of naming the file the URL can I use an alternate cell as the name and use the URL for the search function? – WindChill324 Aug 17 '21 at 00:35
  • 2
    There are a lot of things you can do, depending... you can just use the `Replace` function on the string to change the `?` to some other character, if that helps... it's up to you how you want to handle it. – braX Aug 17 '21 at 00:46

0 Answers0