Html в pdf vba

Contact US

*Tek-Tips’s functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Using VBA to convert an HTML file into PDF

Using VBA to convert an HTML file into PDF

Using VBA to convert an HTML file into PDF

I wrote a macro which downloads some web pages to my HD; now I want to convert each of these pages into a PDF file, and finally to concatenate all PDFs into a single one.
Is this accomplishable in VBA? I only find code snippets to save Excel/Access/Word files into PDF!

RE: Using VBA to convert an HTML file into PDF

Well, it could be as simple as printing to a PDF.

But as for concatonating them, how are you saving the files? as HTML files, text files, or what? If as HTML, then I’m not sure right off hand that VBA will be the best tool for concatonating, though I’d be shocked if it’s impossible.

Читайте также:  Code editors in javascript

So. since Word can handle html files, you could try:
1. Open files in word
2. Print to PDF (If you don’t have Adobe Acrobat, you can install soemthing like CutePDF to print to PDF for free. Well, Office 2007/2010 may handle this natively as well, I do not recall. I know that Adobe tried to sue Microsoft a few years ago over Microsoft building something of that nature directly into Office apps without requiring a separate product such as Acrobat. (I don’t remember any details on that, just remember reading a mention or two online)

«But thanks be to God, which giveth us the victory through our Lord Jesus Christ.» 1 Corinthians 15:57

RE: Using VBA to convert an HTML file into PDF

>Office 2007/2010 may handle this natively as well

>Adobe tried to sue Microsoft a few years ago

Back in mid-2006, yes. Given that PDF was supposed to be an open standard, this all seemed a bit odd at the time. They also threatened to sue them shortly afterwards over XPS format.

Microsoft thus pulled PDF and XPS exporting into a seperately downloadable addin for Office 2007. SP2 then added it back in natively. Office 2010 has it native from the get go.

RE: Using VBA to convert an HTML file into PDF

«But thanks be to God, which giveth us the victory through our Lord Jesus Christ.» 1 Corinthians 15:57

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Copyright © 1998-2023 engineering.com, Inc. All rights reserved.
Unauthorized reproduction or linking forbidden without expressed written permission. Registration on or use of this site constitutes acceptance of our Privacy Policy.

Источник

Сохранение вэб страниц в PDF

Приветствую комрады есть макрос, который сохранял вэб страницы в PDF файл. После апдейта IE и Excela перестал работать. Помогите пожалуйста разобраться в чем соль..

Добавлено через 9 минут
Как результат предлагает сохранить в html

Добавлено через 10 часов 40 минут
Собственно сам код :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
Dim PDFFolder As String Dim LastRow As Long Dim arrSpecialChar() As String Dim dblSpCharFound As Double Dim PDFPath As String Dim i As Long Dim j As Integer 'An array with special characters that cannot be used for naming a file. arrSpecialChar() = Split("\ / : * ? " & Chr$(34) & " < >|", " ") 'Find the last row. With shMain .Activate LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row End With 'Check if the PDF's folder exists. PDFFolder = shMain.Range("B4").Value If FolderExists(PDFFolder) = False Or PDFFolder = "" Then MsgBox "The PDF folder's path is incorect!", vbCritical, "Wrong path" shMain.Range("B4").Select Exit Sub End If 'Check if there is at least one URL. If LastRow < 8 Then MsgBox "You did't enter a URL!", vbCritical, "No URL" Exit Sub End If 'Add the backslash if not exists. If Right(PDFFolder, 1) <> "\" Then PDFFolder = PDFFolder & "\" End If ' 'Set the default printer to Adobe PDF (for Adobe Professional). 'Convert the URLs to PDFs. For i = 8 To LastRow On Error Resume Next PDFPath = Cells(i, 4).Value ' 'Check if the PDF name contains a special/illegal character. For j = LBound(arrSpecialChar) To UBound(arrSpecialChar) dblSpCharFound = WorksheetFunction.Find(arrSpecialChar(j), PDFPath) If dblSpCharFound > 0 Then PDFPath = WorksheetFunction.Substitute(PDFPath, arrSpecialChar(j), "-") End If Next j PDFPath = PDFFolder & PDFPath On Error GoTo 0 'Save the PDF files to the selected folder. Call WebpageToPDF(Cells(i, 3).Value, PDFPath & ".pdf") Next i 'Inform the user that macro finished. MsgBox LastRow - 7 & " invoices were successfully saved as PDFs!", vbInformation, "Done" End Sub Sub WebpageToPDF(pageURL As String, PDFPath As String) 'Creates a new web browser object, opens a selected URL and then prints 'the web page as PDF using Adobe Professional. 'The macro needs a reference to Windows Script Host Object Model Library, as well 'as to the Microsoft Internet Controls Library in order to work. 'From VBA editor go to Tools -> References -> add the two references. 'Or you can find them at C:\Windows\system32\wshom.ocx and C:\Windows\system32\ieframe.dll. Dim WebBrowser As InternetExplorer Dim StartTime As Date Dim intRet As Long Dim Report As Variant 'Create new web browser object, make it visible, 'maximize the window and navigate to the desired url. Set WebBrowser = New InternetExplorer WebBrowser.Visible = True ShowWindow WebBrowser.hwnd, SW_MAXIMIZE WebBrowser.Navigate (pageURL) 'Wait until the web page is fully loaded. Do DoEvents Loop Until WebBrowser.ReadyState = READYSTATE_COMPLETE 'Check if the internet explorer window exists. StartTime = Now() Do Until Now() > StartTime + TimeValue("00:00:05") intRet = 0 DoEvents 'IEFrame is the class name for internet explorer. intRet = FindWindow("IEFrame", vbNullString) If intRet <> 0 Then Exit Do Loop Const OLECMDID_SAVEAS = 4 Const OLECMDEXECOPT_DODEFAULT = 0 Const OLECMDEXECOPT_PROMPTUSER = 2 'If the IE window exists, print the web page as PDF. WebBrowser.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_PROMPTUSER WebBrowser.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_PROMPTUSER If intRet <> 0 Then WebBrowser.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_PROMPTUSER ShowWindow WebBrowser.hwnd, SW_MAXIMIZE Application.Wait (Now + TimeValue("00:00:05")) End If If MsgBox("Invoice saved! Do you wish to proceed?", vbYesNo + vbQuestion) = vbNo Then Exit Sub End If 'Release the web browser object. WebBrowser.Quit Set WebBrowser = Nothing End Sub

Источник

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)
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.

Avatar of undefined

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.

GET A PERSONALIZED SOLUTION

I can’t help you with VBA code, but if you’re willing to try a commercial product, here are two possibilities:

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.

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

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!

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn’t do my job half as well as I do without it!

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

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 

Источник

Оцените статью