Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating a macro in Excel 2007 to auto repeat rows, except the last page, and send to pdf instead of print.

Posted on 2013-01-10
5
Medium Priority
?
951 Views
Last Modified: 2013-02-04
I got this code from another EE answer. Playing with it, and not sure I understand the line marked with **.  

ALSO, the macro works, but not exactly what I'm looking for.  It does the auto repeat rows in Excel and allows me to change the auto repeat rows for the last page correctly.   BUT, I want to create a .pdf when I run the macro instead of sending out a print job.  

How can I tweak to make this happen.    Code follows:


Sub PrintTest()
'macro written by nutsch on 4/23/08, with lots of inspiration from http://www.rondebruin.nl/print.htm
Dim TotPages As Long
**TotPages = application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
 
With ActiveSheet
    .PageSetup.PrintTitleRows = "$1:$7"
    .PrintOut From:=1, To:=TotPages - 1
    .PageSetup.PrintTitleRows = "$1:$6"
    .PrintOut From:=TotPages, To:=TotPages
End With
 
End Sub
0
Comment
Question by:InfoChase
  • 4
5 Comments
 
LVL 31

Expert Comment

by:gowflow
ID: 38766258
Well here it is:
TotPages = Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
Will return in the variable TotPages the number of pages to be printed.

As far as creating a .pdf question is: on your pc how do you create a .pdf do you use pdf995 as printer and print to it and then it create the pdf or ... ? based on your answer will advise next step.
gowflow
0
 
LVL 31

Accepted Solution

by:
gowflow earned 1500 total points
ID: 38766837
The following code will create a pdf file as distiller from the activesheet in excel. You may change the printer name to the adobe printer you have on your pc.

Sub Test_dist_PDF()
Dim PSFileName As String
Dim PDFFileName As String
PSFileName = "c:\myPostScript.ps"
PDFFileName = "c:\myPDF.pdf"

' Print the Excel range to the postscript file
Dim MySheet As Worksheet
Set MySheet = ActiveSheet
MySheet.UsedRange.PrintOut copies:=1, preview:=False, ActivePrinter:="PDF995", printtofile:=True, collate:=True, PrToFileName:=PSFileName

' Convert the postscript file to .pdf
Dim myPDF As PdfDistiller
Set myPDF = New PdfDistiller
myPDF.FileToPDF PSFileName, PDFFileName, ""
myPDF.Create

End Sub

Open in new window



Let me know.
gowflow
0
 
LVL 31

Expert Comment

by:gowflow
ID: 38848685
Did you hv a chance to try the proposed solution ?
gowflow
0
 

Author Comment

by:InfoChase
ID: 38851853
The code worked, but not exactly what I needed. The situational need has passed, and I do not need to pursue this issue any further.  Thanks for the assist; the code, and the idea, may be useful for some other situation down the road.
0
 
LVL 31

Expert Comment

by:gowflow
ID: 38851985
Your welcome, let me put a samll comment here. You may not need the code now as you mentioned 'The situational need has passed' and it is understood however this does not remove the fact that it was a total working solution as it happens that I use this specific code in my software developped to clients. Furthermore you confirmed that the solution worked (but it was not exactly what you needed)

All this being said, do you feel that it is fair that you rated this question B ?

gowflow
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question