• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2774
  • Last Modified:

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
0
dgmoore1
Asked:
dgmoore1
  • 7
  • 6
  • 2
1 Solution
 
PatHartmanCommented:
Going forward, why not export both versions if that's what you need?

To fix up what you have, I wouldn't use Access, I would use Word since Word reads and writes both file types.  Since I don't work with Word VBA and am not familiar with the object model, I start by creating a macro.  So, turn on the macro recorder.  Open the html file.  Save as pdf.  Stop the macro recorder.  That will give you the heart of what you have to do.  Then fix the code to have a loop to read through the directory and do all the files.

If you wanted to run this from Access you could but if this is a one time action.  I would just hard code the path and run it in Word.
0
 
Joe Winograd, EE Fellow 2017, MVE 2016, MVE 2015DeveloperCommented:
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
0
 
dgmoore1Author Commented:
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!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dgmoore1Author Commented:
Alas, A-PDF HTML to PDF won't run on 64-bit windows. Back to the drawing board...
0
 
Joe Winograd, EE Fellow 2017, MVE 2016, MVE 2015DeveloperCommented:
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
0
 
PatHartmanCommented:
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

0
 
dgmoore1Author Commented:
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
0
 
PatHartmanCommented:
If the Access app is generating the files, why not just generate both formats at once?
0
 
dgmoore1Author Commented:
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.
0
 
PatHartmanCommented:
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.
0
 
dgmoore1Author Commented:
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.
0
 
PatHartmanCommented:
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?
0
 
dgmoore1Author Commented:
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
0
 
dgmoore1Author Commented:
This is a daily procedure that runs in Access, so I will control Word from Access through automation.
0
 
PatHartmanCommented:
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now