[Solved] Print to PDFCreator w/out changing default printer

Creating a macro - Writing a Script - Using the API

[Solved] Print to PDFCreator w/out changing default printer

Postby 9811%Deet » Wed Jul 22, 2015 10:10 pm

Hello, you fellas helped me with a macro that pulls sheets based on a cell's contents a while back, and this has been working very well since:
(https://forum.openoffice.org/en/forum/v ... 20&t=75413)

Code: Select all   Expand viewCollapse view
Dim PrintRangeTmpl(0) as New com.sun.star.table.CellRangeAddress
PrintRangeTmpl(0).Sheet=0
PrintRangeTmpl(0).StartColumn = 0
PrintRangeTmpl(0).EndColumn = 13
PrintRangeTmpl(0).StartRow = 0
PrintRangeTmpl(0).EndRow = 64 
oSheets = ThisComponent.Sheets
NumSheets = oSheets.Count

for i = 0 to NumSheets - 1
   oSheet = oSheets.getByIndex(i)
   oCell = oSheet.getCellrangeByName("A100")
   If oCell.String = "1" Then
      PrintRangeTmpl(0).Sheet = i
else
   oSheet.PrintAreas = Array()
   oSheet.isvisible = False
end If
next i
ThisComponent.Print(Array())



But now I'm interested in setting this code to print through a printer emulator called "PDFCreator". This will allow my macro to output the selected data to a PDF.

I've been able to change the printer to PDFCreator through commands like:
Code: Select all   Expand viewCollapse view
rem ----------------------------------------------------------------------
dim args9(0) as new com.sun.star.beans.PropertyValue
args9(0).Name = "Printer"
args9(0).Value = "PDFCreator"

dispatcher.executeDispatch(document, ".uno:Printer", "", 0, args9())

...and this works fine to print the PDF, but the problem I'm having is that it changes the default printer, so that other print jobs from the same workbook will then default to the PDFCreator too.

If this workbook was only being used on one printer, it would be a simple matter to change the printer back, but the default printer will be named differently at every location where this workbook will be used. I need some way to change the printer to PDFCreator for this one macro, but then return the printer to whatever system default is defined afterward.

Any help would be greatly appreciated!
Last edited by 9811%Deet on Thu Jul 23, 2015 3:55 pm, edited 1 time in total.
Openoffice 4.1.1 - Windows 7 Professional
9811%Deet
 
Posts: 7
Joined: Tue Feb 17, 2015 3:22 am

Re: Print to PDFCreator without changing default printer.

Postby Zizi64 » Wed Jul 22, 2015 11:09 pm

Why you want to use a virtual printer (in this case) for exporting a document to PDF format? You can control the PDF export function of the AOO by macros...
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.4; AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7985
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Postby 9811%Deet » Wed Jul 22, 2015 11:43 pm

The macro was originally written for a printer. Some of the users will be uploading the pdf to an online database. Others will be printing the documents for filling. In both cases, the formatting, page selection, etc... needs to be uniform.

I would assume we could use the existing macro along with PDFCreator to produce a uniform result. But the problems defining the default are messing things up.
Openoffice 4.1.1 - Windows 7 Professional
9811%Deet
 
Posts: 7
Joined: Tue Feb 17, 2015 3:22 am

Re: Print to PDFCreator without changing default printer.

Postby Zizi64 » Thu Jul 23, 2015 6:42 am

Maybe these topics will help you (please read all of posts, and see all of links and code snippets in these topics):

https://forum.openoffice.org/en/forum/v ... hp?t=12982
https://forum.openoffice.org/en/forum/v ... 20&t=56708
https://forum.openoffice.org/en/forum/v ... hp?t=41554
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.4; AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7985
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Postby Zizi64 » Thu Jul 23, 2015 7:13 am

Here is Andrew Pitonyak's code for printer selection from the other (older) FORUM:
http://www.oooforum.org/forum/viewtopic.phtml?t=3388

Code: Select all   Expand viewCollapse view
'******************************************************************
'Author: Andrew Pitonyak
'email:   andrew@pitonyak.org
Sub PrintCurrentDocument
  Dim mPrintopts1(), x as Variant
  'Dimensioned at 0, if you set any other properties, be certain to set this to a higher value....
  'Dim mPrintopts2(0) As New com.sun.star.beans.PropertyValue
  Dim oDocument As Object, oPrinter As Object

  oDocument = ThisComponent

  '***********************************
  'Do you want to choose a certain printer
Dim mPrinter(0) As New com.sun.star.beans.PropertyValue
mPrinter(0).Name="Name"
mPrinter(0).value="Other printer"     ' insert name of your printer
oDocument.Printer = mPrinter()

  '***********************************
  'To simply print the document do the following:
oDocument.Print(mPrintopts1())

  '***********************************
End Sub
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.4; AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7985
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Postby RoryOF » Thu Jul 23, 2015 7:13 am

In such a situation the methodology is simple: alter your macro to read the name of the installed printer and remember it. Now execute the body of your macro and before exiting restore the chosen printer.

You already have the code to set the printer to be PDFCreator; similar code can be used to set the original printer. You really only need to sort out how to read the name of the current printer, which a few minutes studying the API should sort out.
Apache OpenOffice 4.1.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 28981
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Print to PDFCreator without changing default printer.

Postby Zizi64 » Thu Jul 23, 2015 7:31 am

Or you can use the pdf-filter option (without usage of any virtual printer) with some similar code:

Code: Select all   Expand viewCollapse view
Sub Expert_PDF

   oSheet = ThisComponent.Sheets.getByName("Sheet1")
   REM Or you can choose other sheet
   ThisComponent.CurrentController.setActiveSheet(oSheet)
   
    MyExportArea = "$A1:F200"
    REM Or you can 'calculate' the string based on other conditions
   
   document = ThisComponent.CurrentController.Frame
   oRange = oSheet.getCellRangeByName(MyExportArea)
   ThisComponent.CurrentController.Select(oRange)


   PDF_URL = "File://... !String!" REM You can determine it from the URL of the actual document, or you can use a fixed one.
   

Dim args2(1) as new com.sun.star.beans.PropertyValue
Dim Arg(0) as new com.sun.star.beans.PropertyValue
   
   Arg(0).Name = "Selection"
   Arg(0).Value = oRange

   args2(0).Name = "FilterName"
   args2(0).Value = "calc_pdf_Export"
   args2(1).Name = "FilterData"
   args2(1).Value = Arg()


   ThisComponent.storeToURL(PDF_URL,args2())   

End Sub
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.4; AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7985
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Print to PDFCreator without changing default printer.

Postby 9811%Deet » Thu Jul 23, 2015 3:54 pm

RoryOF wrote:In such a situation the methodology is simple: alter your macro to read the name of the installed printer and remember it. Now execute the body of your macro and before exiting restore the chosen printer.

You already have the code to set the printer to be PDFCreator; similar code can be used to set the original printer. You really only need to sort out how to read the name of the current printer, which a few minutes studying the API should sort out.



That's all I needed.

Thank you!
Openoffice 4.1.1 - Windows 7 Professional
9811%Deet
 
Posts: 7
Joined: Tue Feb 17, 2015 3:22 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 4 guests