I have recently written some code for the company I work at in VBA for Excel. The purpose of the code is to run through a list of entered part numbers on Excel, search their respective URL, and then save that webpage as a PDF to the selected folder.
I wrote this code using some inspiration and code from other projects I have seen and also had to add some of my own.
My problem is this:
The code I put here works flawlessly on my computer. I have test ran it many times with many different numbers and it works as intended every time. That being said, I sent this to my adviser via email for him to use because I was making this Excel project for him.
When he opened it on the day I sent it, it worked perfectly for him as it had for me. The next day he tried to run it and started getting automation errors I had never seen like
run-time error '430'
and a few other similar errors.
Does anyone have any idea why the program would work one day and not the next and/or if there is a solution to it?
I'm mainly confused why it would suddenly stop working for him (it does still run perfectly on my computer). Also, for the code below it does follow a real link I just change website parts to "url" for the sake of company privacy.
Although, I don't think that matters anyways because I believe the code is following the first link properly and then has errors when it has to loop through more than one part number.
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function
Sub DownloadItemPDF()
Dim strPDFLink As String
Dim strPDFFile As String
Dim doc, hcol, text As Variant
Dim itemNum As Long
Dim count As Integer
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
MsgBox "Select Folder to Save .pdf to"
Dim FolderName As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
On Error Resume Next
strDir = .SelectedItems(1)
Err.clear
On Error GoTo 0
End With
ActiveSheet.Range("H3").Select
For count = 1 To 15
If Not Selection.Value = "" Then
itemNum = Selection.Value
strPDFLink = "url" & itemNum & "url"
strPDFFile = strDir & "/Drawing2D_" & itemNum & "_" & Format(Now, "yyyy.mm.dd") & ".pdf"
IE.Navigate strPDFLink
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
Application.Wait (Now + #12:00:02 AM#)
Result = DownloadFile(strPDFLink, strPDFFile)
End If
Selection.Offset(1, 0).Select
Next count
End Sub