ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel to PDF (https://www.excelbanter.com/excel-programming/367578-excel-pdf.html)

I Maycotte[_15_]

Excel to PDF
 

Below is a modified code from a recorded macro whose purpose is to print
to a pdfdriver and then save. Manually this takes a long time when the
user deals with more than a handful cells. In my case, my range
consists of 200+ cells. Everything seems correct with one exception --
the file is created but no data is saved. That is to say that when I
open "My Documents" I see a properly named PDF file but of size 0KB
(should be roughly 4.5 KB). When I attempt to open it, I receive a
file read error. I am wondering if there is anything wrong with the
code or if it's a problem with my writer. I am hesitant to say that it
is the writer because if I do each file manually, they are created and
viewed without error. Thank you all who reply.



Code:
--------------------
Sub PrintToPDF()
'
'
Dim Products As Range
Dim Filename As String

Set Products = Worksheets("Summary").Range("Products")

For Each cell In Products
If IsEmpty(cell) Then Exit For

Filename = cell
cell.Copy

Worksheets("Historical").Paste Destination:=Worksheets("Historical").Cells(2, 1)

Application.CutCopyMode = False
Application.Calculate

Application.ActivePrinter = "Acrobat PDFWriter on LPT1:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Acrobat PDFWriter on LPT1:", PrintToFile:=True, PrToFileName:="C:\My Documents\" & Filename & ".pdf"

Next cell
End Sub

--------------------


-- Isaac


--
I Maycotte
------------------------------------------------------------------------
I Maycotte's Profile: http://www.excelforum.com/member.php...o&userid=35604
View this thread: http://www.excelforum.com/showthread...hreadid=562549


Tom Ogilvy

Excel to PDF
 
why not try changing

ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Acrobat PDFWriter on LPT1:", PrintToFile:=True, PrToFileName:="C:\My
Documents\" & Filename & ".pdf"

to

Worksheets("Historical").PrintOut Copies:=1, ActivePrinter:= _
"Acrobat PDFWriter on LPT1:", PrintToFile:=True, PrToFileName:="C:\My
Documents\" & Filename & ".pdf"

Also make sure if there is a printarea defined for the sheet Historical, it
is set to the correct area.

--
Regards,
Tom Ogilvy



"I Maycotte" wrote:


Below is a modified code from a recorded macro whose purpose is to print
to a pdfdriver and then save. Manually this takes a long time when the
user deals with more than a handful cells. In my case, my range
consists of 200+ cells. Everything seems correct with one exception --
the file is created but no data is saved. That is to say that when I
open "My Documents" I see a properly named PDF file but of size 0KB
(should be roughly 4.5 KB). When I attempt to open it, I receive a
file read error. I am wondering if there is anything wrong with the
code or if it's a problem with my writer. I am hesitant to say that it
is the writer because if I do each file manually, they are created and
viewed without error. Thank you all who reply.



Code:
--------------------
Sub PrintToPDF()
'
'
Dim Products As Range
Dim Filename As String

Set Products = Worksheets("Summary").Range("Products")

For Each cell In Products
If IsEmpty(cell) Then Exit For

Filename = cell
cell.Copy

Worksheets("Historical").Paste Destination:=Worksheets("Historical").Cells(2, 1)

Application.CutCopyMode = False
Application.Calculate

Application.ActivePrinter = "Acrobat PDFWriter on LPT1:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"Acrobat PDFWriter on LPT1:", PrintToFile:=True, PrToFileName:="C:\My Documents\" & Filename & ".pdf"

Next cell
End Sub

--------------------


-- Isaac


--
I Maycotte
------------------------------------------------------------------------
I Maycotte's Profile: http://www.excelforum.com/member.php...o&userid=35604
View this thread: http://www.excelforum.com/showthread...hreadid=562549



I Maycotte[_16_]

Excel to PDF
 

Thanks for the help, Tom. It works. Very much appreaciated.

-- Isaac Maycott

--
I Maycott
-----------------------------------------------------------------------
I Maycotte's Profile: http://www.excelforum.com/member.php...fo&userid=3560
View this thread: http://www.excelforum.com/showthread.php?threadid=56254



All times are GMT +1. The time now is 01:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com