OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

vbs OLE

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
kryptos66
General User
General User


Joined: 31 Oct 2007
Posts: 20

PostPosted: Fri Sep 05, 2008 1:01 pm    Post subject: vbs OLE Reply with quote

How would I proceed with the following vbs code if I wanted to alter the margins in the document before I began to write to it?
With thanks.. CB
Code:

Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")

Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")

Set objDesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop")

Dim args()
Set objDocument= objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)

Set objText= objDocument.getText

Set objCursor= objText.createTextCursor
Back to top
View user's profile Send private message
vitcaro
OOo Advocate
OOo Advocate


Joined: 20 Feb 2007
Posts: 256
Location: Italy

PostPosted: Sun Sep 07, 2008 3:17 am    Post subject: Reply with quote

Here is an example to fix page margin with VBS:
Code:

Set oServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
Set oDesktop= oServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oComponents = oDesktop.getComponents()
Set oEnum = oComponents.createEnumeration
On Error Resume Next
Do While oEnum.hasMoreElements
    Set ThisComponent = oEnum.nextElement
    if ThisComponent.supportsService("com.sun.star.text.TextDocument") Then Exit Do
Loop
Set oDoc = ThisComponent
Dim PrinterDescriptor(0)
Set PrinterDescriptor(0) = aProperty("Name", "Adobe PDF")
oDoc.setPrinter PrinterDescriptor
'Properties of com.sun.star.text.PagePrintSettings
'PageRows     short => Number of rows in which document pages should appear on the output page.
'PageColumns  short => Number of columns in which document pages should appear on the output page.
'LeftMargin   long => Left margin on the output page.
'RightMargin  long => Right margin on the output page.
'TopMargin    long => Top margin on the output page.
'BottomMargin long => Bottom margin on the output page.
'HoriMargin   long => Margin between the columns on the output page.
'VertMargin   long => Margin between the rows on the output page.
'IsLandscape  boolean => Determines if the output page is in landscape format.
Dim PagePrintSettings(7)
Set PagePrintSettings(0) = aProperty("PageRows", 2)
Set PagePrintSettings(1) = aProperty("PageColumns", 2)
Set PagePrintSettings(2) = aProperty("LeftMargin", 100)
Set PagePrintSettings(3) = aProperty("RightMargin", 100)
Set PagePrintSettings(4) = aProperty("TopMargin", 100)
Set PagePrintSettings(5) = aProperty("BottomMargin", 100)
Set PagePrintSettings(6) = aProperty("VertMargin", 100)
Set PagePrintSettings(7) = aProperty("IsLandscape", False)
oDoc.setPagePrintSettings PagePrintSettings
Dim PrintOptions(0)
Set PrintOptions(0) = aProperty("Pages", "4-7")
oDoc.printPages PrintOptions

Function aProperty(cName,cValue)
Set aProperty = oServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
aProperty.Name = cName
aProperty.Value = cValue
End Function
Back to top
View user's profile Send private message
kryptos66
General User
General User


Joined: 31 Oct 2007
Posts: 20

PostPosted: Tue Sep 09, 2008 1:29 pm    Post subject: Reply with quote

Thanks for this reply. However it doesn't seem to do what I want, and I'm baffled as to why printer settings would at all be included. It's the margins as they appear on the screen which interest me. I want my vbs file to start Open Office with a new Writer document loaded and then to manipulate the margins of the page, just as if I was using the Menu bar, choosing Format then Page then Left Margin etc. This is a macro I found at http://udk.openoffice.org/common/man/tutorial/writerdemo.html

Code:
'The service manager is always the starting point
'If there is no office running then an office is started up
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")

'Create the CoreReflection service that is later used to create structs
Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")

'Create the Desktop
Set objDesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop")

'Open a new empty writer document
Dim args()
Set objDocument= objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)

'Create a text object
Set objText= objDocument.getText

'Create a cursor object
Set objCursor= objText.createTextCursor

'Inserting some Text
objText.insertString objCursor, "The first line in the newly created text document." & vbLf, false

'Inserting a second line
objText.insertString objCursor, "Now we're in the second line", false

'Create instance of a text table with 4 columns and 4 rows
Set objTable= objDocument.createInstance( "com.sun.star.text.TextTable")
objTable.initialize 4, 4

'Insert the table
objText.insertTextContent objCursor, objTable, false

'Get first row
Set objRows= objTable.getRows
Set objRow= objRows.getByIndex( 0)

'Set the table background color
objTable.setPropertyValue "BackTransparent", false
objTable.setPropertyValue "BackColor", 13421823

'Set a different background color for the first row
objRow.setPropertyValue "BackTransparent", false
objRow.setPropertyValue "BackColor", 6710932

'Fill the first table row
insertIntoCell "A1","FirstColumn", objTable
insertIntoCell "B1","SecondColumn", objTable
insertIntoCell "C1","ThirdColumn", objTable
insertIntoCell "D1","SUM", objTable

objTable.getCellByName("A2").setValue 22.5
objTable.getCellByName("B2").setValue 5615.3
objTable.getCellByName("C2").setValue -2315.7
objTable.getCellByName("D2").setFormula"sum "

objTable.getCellByName("A3").setValue 21.5
objTable.getCellByName("B3").setValue 615.3
objTable.getCellByName("C3").setValue -315.7
objTable.getCellByName("D3").setFormula "sum "

objTable.getCellByName("A4").setValue 121.5
objTable.getCellByName("B4").setValue -615.3
objTable.getCellByName("C4").setValue 415.7
objTable.getCellByName("D4").setFormula "sum "

'Change the CharColor and add a Shadow
objCursor.setPropertyValue "CharColor", 255
objCursor.setPropertyValue "CharShadowed", true

'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objText.insertControlCharacter objCursor, 0 , false

'Inserting colored Text.
objText.insertString objCursor, " This is a colored Text - blue with shadow" & vbLf, false

'Create a paragraph break ( ControlCharacter::PARAGRAPH_BREAK).
objText.insertControlCharacter objCursor, 0, false

'Create a TextFrame.
Set objTextFrame= objDocument.createInstance("com.sun.star.text.TextFrame")

'Create a Size struct.
Set objSize= createStruct("com.sun.star.awt.Size")
objSize.Width= 15000
objSize.Height= 400
objTextFrame.setSize( objSize)

' TextContentAnchorType.AS_CHARACTER = 1
objTextFrame.setPropertyValue "AnchorType", 1

'insert the frame
objText.insertTextContent objCursor, objTextFrame, false

'Get the text object of the frame
Set objFrameText= objTextFrame.getText

'Create a cursor object
Set objFrameTextCursor= objFrameText.createTextCursor

'Inserting some Text
objFrameText.insertString objFrameTextCursor, "The first line in the newly created text frame.", _
false
objFrameText.insertString objFrameTextCursor, _
vbLf & "With this second line the height of the frame raises.", false

'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objFrameText.insertControlCharacter objCursor, 0 , false

'Change the CharColor and add a Shadow
objCursor.setPropertyValue "CharColor", 65536
objCursor.setPropertyValue "CharShadowed", false

'Insert another string
objText.insertString objCursor, " That's all for now !!", false

On Error Resume Next
  If Err Then
  MsgBox "An error occurred"
End If


Sub insertIntoCell( strCellName, strText, objTable)
   Set objCellText= objTable.getCellByName( strCellName)
   Set objCellCursor= objCellText.createTextCursor
   objCellCursor.setPropertyValue "CharColor",16777215
   objCellText.insertString objCellCursor, strText, false
End Sub

Function createStruct( strTypeName)
   Set classSize= objCoreReflection.forName( strTypeName)
   Dim aStruct
   classSize.createObject aStruct
   Set createStruct= aStruct
End Function



This opens a document ,writes some text, and creates and colors a table, but there is no clue about how to customise margins. I'm trying to change from WordPerfect (sadly being forced to do so) In WordPerfect;

Code:
Set objWP = CreateObject("WordPerfect.PerfectScript.8")
objWP.MarginLeft "4c"


was all it took to do this simple little operation. Incredible huh?
Back to top
View user's profile Send private message
vitcaro
OOo Advocate
OOo Advocate


Joined: 20 Feb 2007
Posts: 256
Location: Italy

PostPosted: Tue Sep 09, 2008 10:00 pm    Post subject: Reply with quote

Here are two functions for page formatting, which are from AndrewMacro.pdf:
Code:

Sub Margins( )
Dim oStyleFamilies, oFamilies, oPageStyles, oStyle
Dim oViewCursor, oPageStyleName
Dim fromleft%, fromtop%, fromright%, frombottom%
Dim oDoc
oDoc = ThisComponent
REM You don't need the view cursor, you can use any TextCursor
oViewCursor = oDoc.CurrentController.getViewCursor()
oPageStyleName = oViewCursor.PageStyleName
oPageStyles = oDoc.StyleFamilies.getByName("PageStyles")
oStyle = oPageStyles.getByName(oPageStyleName)
REM fromleft, fromtop, fromright, frombottom = whatever you want
oStyle.LeftMargin = fromleft
oStyle.TopMargin = fromtop
oStyle.RightMargin = fromright
oStyle.BottomMargin = frombottom
End Sub

Sub SetThePageStyle()
Dim oStyle
Dim sPageStyleName$
Dim oDoc
Dim s$
Dim oVC
oDoc = ThisComponent
REM You don't need the view cursor, you can use any TextCursor
oVC = oDoc.getCurrentController().getViewCursor()
sPageStyleName = oVC.PageStyleName
DIM oPageStyles
oPageStyles = oDoc.StyleFamilies.getByName("PageStyles")
oStyle = oPageStyles.getByName(sPageStyleName)
REM Is this is Letter, then set to A4
If oStyle.Width = 27940 Then
Print "Setting to size A4"
oStyle.Width = 21000
oStyle.Height = 29700
Else
Print "Setting to size Letter"
oStyle.Width = 21590
oStyle.Height = 27940
End If
REM Note that the Width and Height properties are the same as the
REM values stored in the Size property. Seems silly, I know...
REM Setting the width or height sets both...
s="Width = " & CStr(oStyle.Width / 2540) & " inches" & CHR$(10)
s=s&"Height = "&CStr(oStyle.Height / 2540)&" inches" & CHR$(10)
s=s&"Width = "&CStr(oStyle.Size.Width / 2540) & " inches"&CHR$(10)
s=s&"Height = "&CStr(oStyle.Size.Height / 2540)&" inches" & CHR$(10)
MsgBox s, 0, "Page Style " & sPageStyleName
End Sub
Back to top
View user's profile Send private message
kryptos66
General User
General User


Joined: 31 Oct 2007
Posts: 20

PostPosted: Thu Sep 11, 2008 1:26 am    Post subject: Reply with quote

THank you so much - that's been a great help - now I feel I'm beginning to get somewhere. Translating a little I'm now able to open OO, start a new text document. alter margins, create a table and and place some text in the table cells, all from a standalone vbs file:
Code:

Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
Set objDesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop")

Dim args()
Set objDocument= objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)

Set oViewCursor = objDocument.CurrentController.getViewCursor()
oPageStyleName = oViewCursor.PageStyleName
Set oPageStyles = objDocument.StyleFamilies.getByName("PageStyles")
Set oStyle = oPageStyles.getByName(oPageStyleName)

oStyle.LeftMargin = 500
oStyle.TopMargin = 500
oStyle.RightMargin = 500
oStyle.BottomMargin = 500

Set objText= objDocument.getText
Set objCursor= objText.createTextCursor

objText.insertControlCharacter objCursor, 0 , false
objText.insertControlCharacter objCursor, 0 , false

Set objTable= objDocument.createInstance( "com.sun.star.text.TextTable")
objTable.initialize 32, 9

objText.insertTextContent objCursor, objTable, false

For I = 1 To 31
    objTable.getCellByName("A"&I+1).setValue I 
Next

And that's probably as far as I'll get this week, but I'll be back for more hinfs on how to format the table, define row and column sizes, place the marker at a particular point etc.
Many thanks again!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group