need to use VBA inside Access to save a webpage as a PDF, specifying path and file name, without user intervention

I need to use VBA inside Access to save a webpage as a PDF, using a specific path and file name.  The code below works just fine to select the records, compose the URL and the path/filename, open the webpage, and open a printer dialog box.  

I need to modify the .ExecWB command (currently  (   .ExecWB 6, 2, 2, 0   )  to use the variable strPathAndFileName to save the file without opening a dialog box and requiring the user to navigate to the desired location and type a file name & extension.

The rest of the code works fine to close IE and move to the next record.




strSQL = "Select Log, AppraisalDistrictPropertyID from PropertiesT where ClientID < 1442 and AppraisalDistrictPropertyID is not null"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
   rs.MoveFirst
   
'check for empty recordset
If Not rs.RecordCount > 0 Then
   MsgBox "No records met the criteria for PrintWebPageToPDF.", vbOKOnly, strAppNa
   Exit Sub
End If

Do While rs.EOF = False
'loop through records in rs
'Debug.Print strURL & rs.Fields("AppraisalDistrictPropertyID") & " will be dropped in " & strOutputFolder & rs.Fields("Log") & "."
'confirm that folder exists for Log entry\
CheckPath strOutputFolder & rs.Fields("Log")
   
'open IE to first record
'ExecWB to print webpage to PDF
Set IE = CreateObject("internetexplorer.application")
With IE
   .Visible = True
   .Navigate strURL & rs.Fields("AppraisalDistrictPropertyID")
   Do Until IE.ReadyState = 4 'READYSTATE_COMPLETE
      DoEvents
   Loop

   strPathAndFileName = strOutputFolder & rs.Fields("Log") & "\a.pdf"

   .ExecWB 6, 2, 2, 0

   .Quit
End With
Set IE = Nothing

'rename PDF to H:\Property Tax Consulting\Annual Files\2018\<<Log>>\a.pdf
'update counter display
'loop to next record

rs.MoveNext
Loop



'recordset is closed and emptied:
rs.Close
Set rs = Nothing

Open in new window

LVL 6
Paul Cook-GilesAccess/VBA/SQL Database DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
While not the best way to do things, I would use SendKeys and just act like a user.

 I think you won't get closer to a solution then that.

Jim.
0
Paul Cook-GilesAccess/VBA/SQL Database DeveloperAuthor Commented:
Jim, I'm not married to the use of ExecWB;  I'm open to any process that will let me save a webpage as a pdf, so long as I can define the path/filename.
0
F. X. FlinnSenior Consultant / FounderCommented:
I've had some success with this approach, which may not be right for you:

DoCmd.RunSavedImportExport "Export-NY Internal Report"

There are some limitations to this method and I don't believe it does what you want, since you are looking for a webpage printer that gets called via Access VBA.

You might be better off with a screen-scraper. I used one years ago to pull electric power generation data off UK websites and stuff the results into a SQL table. I think it may have been called iMacros.
0
Paul Cook-GilesAccess/VBA/SQL Database DeveloperAuthor Commented:
I found an approach that works:  Using ExecWB to select and copy all the text/graphics/whatever on a webpage, then opening Word, making the layout Landscape, pasting the contents of the clipboard, exporting the document as a PDF, and then closing Word without saving the document.



Public Sub SaveWebPageAsPDF(strURL As String, strPathAndFileName As String)
Dim IE As Object, objWord As Word.Application, doc As Word.Document

Set IE = CreateObject("InternetExplorer.Application")
With IE
   .Navigate strURL
   Do While .ReadyState <> 4
      DoEvents
   Loop
   .ExecWB 17, 2
   .ExecWB 12, 2
   .Quit
End With


Set objWord = CreateObject("Word.Application")
With objWord
   Set doc = .Documents.Add
   doc.PageSetup.Orientation = wdOrientLandscape
   .Selection.Paste
   .ActiveDocument.ExportAsFixedFormat OutputFileName:=strPathAndFileName, ExportFormat:=wdExportFormatPDF, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument
   .Quit SaveChanges:=wdDoNotSaveChanges
End With


End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul Cook-GilesAccess/VBA/SQL Database DeveloperAuthor Commented:
I was able to find a solution on my own.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.