Writer Macro ThisComponent empty

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
slciec
Posts: 9
Joined: Mon Feb 13, 2017 4:18 pm

Writer Macro ThisComponent empty

Post by slciec »

I have the following macro that gives me a BASIC runtime error on ThisComponent.getPrinter() . Property or method not found: getPrinter.
If I save the macro as part of the document it work fine.

Code: Select all

Sub PrintD1
Dim oDoc 
oDoc = ThisComponent.getPrinter()
oDoc(0).Value = "\\DC1.illinoiseyecenter.com\D1"
ThisComponent.setPrinter(oDoc)
ThisComponent.StyleFamilies.getByName("PageStyles").getByName("Default").PrinterPaperTray = " Tray 3"
Dim oOpts(0) As New com.sun.star.beans.PropertyValue
oOpts(0).Name = "Wait"
oOpts(0).Value = False
ThisComponent.Print(oOpts())

End Sub
OpenOffice 4.1.3 Windows 7
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Writer Macro ThisComponent empty

Post by RoryOF »

When you get the BASIC error, have you a document open?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
slciec
Posts: 9
Joined: Mon Feb 13, 2017 4:18 pm

Re: Writer Macro ThisComponent empty

Post by slciec »

Yes I do. So in the macro Organizer I currently have it installed under My Macros. If I create the macro under the document Router.odt->Standard it works.
There must be something I am not calling to get the attachment to the current document.
I read thru the Accessing the UNO API information and put in "MsgBox ThisComponent.Dbg_SupportedInterfaces" it returns with Dbg_SupportedInterface not available. (TypeClass is not TypeClass_INTERFACE)
OpenOffice 4.1.3 Windows 7
slciec
Posts: 9
Joined: Mon Feb 13, 2017 4:18 pm

Re: Writer Macro ThisComponent empty

Post by slciec »

So I figured out the error that is going on. Apparently when I am debugging the macro it does not recognize the document if it is under my macros.
If I just run the macro it works. I will just debug the macro as part of the document.
Thanks for looking into it.
OpenOffice 4.1.3 Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Writer Macro ThisComponent empty

Post by Villeroy »

When the code is stored in the global scope ("My Documents" or "OpenOffice Macros"), ThisComponent refers to the latest active document, not counting the Basic IDE but counting any help window.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
UnklDonald418
Volunteer
Posts: 1549
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Writer Macro ThisComponent empty

Post by UnklDonald418 »

Macros like yours that reference ThisComponent multiple times can cause problems especially during debugging.
That situation can easily be avoided if you begin your macro as Andrew Pitonyak recommends with

Code: Select all

oDoc = ThisComponent
Then you use oDoc throughout your macro without any chance of it changing references.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
slciec
Posts: 9
Joined: Mon Feb 13, 2017 4:18 pm

Re: Writer Macro ThisComponent empty

Post by slciec »

I made that change in the code, during the testing I figured out what the main issue is.
When trying to set the paper tray it tells me the Tray is basically invalid. Even though the tray is valid for the printer i set it to in the previous statement it for some odd reason thinks I am hooked up to my "Default" printer which does not have a "Tray 3". Is there anyway to force the document to check the printer i have changed it to?

Code: Select all

Dim oDoc 
Dim oTray 
Dim oPrinter
oDoc = ThisComponent
'Set Printer
oPrinter = oDoc.getPrinter()
oPrinter(0).Value = "\\DC1.illinoiseyecenter.com\D1"
oDoc.setPrinter(oPrinter)

'Set Output Tray
oTray = ThisComponent.StyleFamilies.getByName("PageStyles").getByName("Default").PrinterPaperTray
oTray = "Tray 3"
oDoc.StyleFamilies.getByName("PageStyles").getByName("Default").PrinterPaperTray = oTray

Dim oOpts(0) As New com.sun.star.beans.PropertyValue
oOpts(0).Name = "Wait"
oOpts(0).Value = False
'ThisComponent.Print(oOpts())
OpenOffice 4.1.3 Windows 7
Post Reply