Link to home
Start Free TrialLog in
Avatar of dgmoore1
dgmoore1Flag for United States of America

asked on

MSAccess: Convert html to pdf using vba

I have an Access app that generates a bunch of html files. I need to create pdf copies of the files, and I see online that there are countless potential approaches. Can somebody suggest an efficient way to iterate through a folder in VBA and convert each html file to vba:

For Each F in Folder.Files
If F.Name Like "*.html" Then
Filename = Left(F.Name, InStr(F.Name, ".")-1)
  {Convert to PDF} Filename.pdf
End If
Next F
?
I'd prefer to avoid approaches that open the files in a browser and then print using a PDF driver.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I can't help you with VBA code, but if you're willing to try a commercial product, here are two possibilities:

(1) HTML2PDF Pilot:
http://www.snapfiles.com/get/html2pdfpilot.html

It has a GUI interface that can process individual files or an entire folder. It also has command line support for use in batch files and/or programs/scripts.

(2) A-PDF HTML to PDF:
http://www.a-pdf.com/html-to-pdf/

This also has both GUI and command line interfaces, as well as a Watched Folder feature.

They both offer 30-day trials. If you're OK with this approach, give them a spin and see if one works for you. Neither is free, but both are reasonably priced ($30 and $39, respectively). Regards, Joe
Avatar of dgmoore1

ASKER

Pat - thanks for the suggestion, but it's too complex and involves too many moving parts.

Joe, from the vendor's documentation it doesn't look like (1) will work, but (2) looks promising. I'm going to d/l a trial copy and see if I can shell to a cmd window and call the HTML2PDF executable. If that works, it could be the solution I'm looking for. I'll get back here after my test and let you know how it goes. Thanks for the tip!
Alas, A-PDF HTML to PDF won't run on 64-bit windows. Back to the drawing board...
I don't know what it is that HTML2PDF Pilot can't do for you, but maybe you need the HTML2PDF-X Pilot library instead:
http://www.colorpilot.com/html2pdfaddon.html

Re A-PDF HTML-to-PDF not working on a 64-bit OS, that really surprised me. But I tried it here on W7/64-bit and you are absolutely correct...the installer won't even run! I even tried it with a few compatibility mode settings and it still wouldn't install. I filled in a contact form and sent an email, too, asking when they're going to have a version that works on 64-bit. I'll let you know if and when I hear back from them (I like their products, but not their support, so I'm not hopeful). In the meantime, do you have a 32-bit system that it can run on? Or maybe a 32-bit VM? Regards, Joe
thanks for the suggestion, but it's too complex and involves too many moving parts.
What part is too hard?
1. Add a second OutputTo following the first.  That will save the file as both types.
2. Word actually does what you need to do.  It can open a file of type A and save as type B.  Access doesn't do that.
3. The Macro recorder will write the specific open and save code.
4.  You never said if this was a one-time only effort.

This is the code generated by the macro recorder.  You need to replace the RecentFiles(1).Open with a loop to read each file in the directory.  Use FSO.  The code will be the same in Word as it is in Access.

Sub Macro1()
'
' Macro1 Macro
'
'
    RecentFiles(1).Open
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\TempPat\This is a test.pdf", ExportFormat:=wdExportFormatPDF, _
        OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
        wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
        IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
        wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
        True, UseISO19005_1:=False
End Sub

Open in new window

This is to be part of a daily process that will run on any of several workstations and generate about 75 pdf's. I am aware of the Word capability but was leery about using Word because of past hassles with integrating Word and Access, but it looks like it may work for what I need. I'll be testing it today.

Thanks
If the Access app is generating the files, why not just generate both formats at once?
I probably will - that's not settled yet. I just received the requirement yesterday afternoon and haven't thought it completely through yet. This conversion needs to fit in with a lot of other procedures, so I need to determine where it fits best.
If you generate both formats, all you need to worry about is converting what you have.  You don't need a procedure that you run daily or whatever.
The process runs daily because the underlying data changes daily. We do a scheduled daily update after midnight and push the files to a Sharepoint portal. The html versions of the files are for viewing on the portal, and the pdf's are there for users who need to download printable versions.
What am I missing here?  If you create both file types from Access at the same time (one output statement followed by another), why do you need to run the process we are talking about beyond the once to create copies of some existing files?
My apologies if my replies have not been as clear as they might have been. My process generates a group of html pages in one procedure, and a group of gifs in a subsequent procedure. The html pages display the gifs, and if I convert the html to pdf before it's gifs have been generated the pdf will be wrong. If I generate all of the html and all of the gifs, then iterate through the output folder, the html pages will display the proper gifs and the pdf's will be accurate. I can't generate the gifs before the html because the html files rely on data generated while the gifs are being produced. In any case, I'm accepting your approach as the solution as it appears that it will do the trick. Thanks
This is a daily procedure that runs in Access, so I will control Word from Access through automation.
Thank you.  Now I understand the problem.  You can control the conversion loop from Access and then pass into word the name of one file at a time.