Page 1 of 1
[Solved] Inserting boxes and shapes
Posted: Sun Jun 10, 2018 10:01 pm
by Cogito ergo sum
Hi, does anyone have a simple Basic macro for inserting a text box in a writer document and tilting it to an angle?
Also one for inserting a geometric figure e.g. a star?
many thanks in anticipation
Re: inserting boxes and shapes
Posted: Sun Jun 10, 2018 10:18 pm
by Villeroy
There is no simple macro for this.
Re: inserting boxes and shapes
Posted: Mon Jun 11, 2018 7:37 am
by Zizi64
Draw and adjust it by the Draw application, and put it into the Gallery. Then you can use it by a "drag&Drop" from the Gallery in all of applications.
Re: inserting boxes and shapes
Posted: Mon Jun 11, 2018 7:54 am
by Cogito ergo sum
No simple macro? A complicated one then perhaps?
Re: inserting boxes and shapes
Posted: Mon Jun 11, 2018 8:47 am
by Zizi64
A complicated one then perhaps?
You will write it.
Please explain: Why you need it?
You can use a template instead (if you often need same shape in same position of the document), or the gallery with your own shapes.
Re: inserting boxes and shapes
Posted: Mon Jun 11, 2018 10:10 am
by Cogito ergo sum
A template is of no interest to me because I want to write the macro for an unknown number of users on a network who subsequently would be required to copy the template to their own gallery to use it. Also I would simply like to know how to do this programatically if it can be done as part of a learning process. And finally a macro would open this possibility of a user customising the use of it to some extent - e.g. the text content of a text box. So yeah I'll have to write it myself from trial and error - I had just hoped tofind a quick shortcut here.
Re: inserting boxes and shapes
Posted: Mon Jun 11, 2018 10:24 am
by RoryOF
A little bit of searching often throws up helpful OO macros.
I found these
[Solved] Insert Image, Position and Resize via Macro in Calc
[Solved] Add Image using macro
[Solved] Inserting and positioning image
and many others
I also recommend Andrew Pitonyak's works on macros.
Re: inserting boxes and shapes
Posted: Mon Jun 11, 2018 10:26 am
by Villeroy
Don't you have a common templates folder on the network?
Re: Inserting boxes and shapes
Posted: Mon Jun 11, 2018 2:23 pm
by Sébastien C
If I may, some examples...
Insert a text box in
Writer and tilting it to an angle is not a big deal...
Code: Select all
Sub textBoxTilting45degrees()
Dim myForm As Object
Dim myDimensionsForm As New com.sun.star.awt.Size
Dim myPositionForm As New com.sun.star.awt.Point
myForm = thisComponent.createInstance("com.sun.star.drawing.TextShape")
myDimensionsForm.Width = 7500 ' 75 mm.
myDimensionsForm.Height = 2530 ' 25,3 mm.
myPositionForm.X = 5000 ' 50 mm.
myPositionForm.Y = 10000 ' 100 mm.
With myForm
.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE
.AnchorPosition.X = 0
.AnchorPosition.Y = 0
.Size = myDimensionsForm
.Position = myPositionForm
.rotateAngle = 4500 ' 45 degrees
End With
thisComponent.drawPage.add(myForm)
myForm.String = "Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!! Hello World !!!"
End Sub
A common star is a
custom shape, and unfortunately, it is so bad documented.
BUT, in
Draw, we can convert it in a
polygon shape.
And paste the star in Writer, just for studying it points...
After that, drawing a
polygon shape is not a big deal too...
Code: Select all
Sub aNewStarIsBorn()
Dim myForm As Object
Dim myDimensionsForm As New com.sun.star.awt.Size
Dim myPositionForm As New com.sun.star.awt.Point
Dim thePoints(9) As New com.sun.star.awt.Point
myForm = thisComponent.createInstance("com.sun.star.drawing.PolyPolygonShape")
thisComponent.drawPage.add(myForm)
thePoints(0).X = 10869
thePoints(0).Y = 1512
thePoints(1).X = 10592
thePoints(1).Y = 2421
thePoints(2).X = 9681
thePoints(2).Y = 2421
thePoints(3).X = 10420
thePoints(3).Y = 2987
thePoints(4).X = 10143
thePoints(4).Y = 3889
thePoints(5).X = 10869
thePoints(5).Y = 3336
thePoints(6).X = 11596
thePoints(6).Y = 3889
thePoints(7).X = 11318
thePoints(7).Y = 2987
thePoints(8).X = 12058
thePoints(8).Y = 2421
thePoints(9).X = 11147
thePoints(9).Y = 2421
myForm.PolyPolygon = Array( thePoints() )
myDimensionsForm.Width = 2000 ' 20 mm.
myDimensionsForm.Height = 2000 ' 20 mm.
myPositionForm.X = 1000 ' 10 mm.
myPositionForm.Y = 1000 ' 10 mm.
With myForm
.AnchorPosition.X = 0
.AnchorPosition.Y = 0
.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PAGE
.LineWidth = 80
.Size = myDimensionsForm
.Position = myPositionForm
.rotateAngle = 4500 ' 45 degrees
End With
End Sub
A good way, I think, for beginning to learn code...
Re: Inserting boxes and shapes
Posted: Mon Jun 11, 2018 11:45 pm
by Cogito ergo sum
OK many thanks everyone for your ideas and to Sébastian for the code which set me on a useful path. Here's as far as I've got with Sébastians code and some googling and a lot of trial and error:
Code: Select all
Dim args(),aStruct
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
Set oDsk= objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oDoc= oDsk.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
Set objSize = createStruct("com.sun.star.awt.Size")
Set objText= oDoc.getText
Set objCursor= objText.createTextCursor
Set objTextFrame= oDoc.createInstance("com.sun.star.drawing.TextShape")
objSize.Width = 7500
objSize.Height = 2530
objTextFrame.setSize( objSize)
objText.insertTextContent objCursor, objTextFrame, false
objTextFrame.String = "Hello World !!! Hello World !!! Hello World !!! Hello World !!!"
objTextFrame.rotateAngle = 4500
objTextFrame.AnchorType = AS_CHARACTER
objTextFrame.IsFollowingTextFlow = False
objTextFrame.CharHeight="14"
objTextFrame.CharFontName="Trebuchet MS"
objTextFrame.CharColor="1500"
objTextFrame.HoriOrient = 0
objTextFrame.VertOrient = 0
objTextFrame.VertOrientPosition = 3500
objTextFrame.HoriOrientPosition = 2000
Function createStruct( strTypeName)
Set classSize= objCoreReflection.forName( strTypeName)
classSize.createObject aStruct
Set createStruct= aStruct
End Function
I wanted a standalone macro and this Works impeccably as a VB file. I just lack the technique for making the borders of the box visible for it to be complete. (It turned out to be not too complicated)
Re: Inserting boxes and shapes
Posted: Tue Jun 12, 2018 12:40 am
by Sébastien C
Cogito ergo sum wrote:I just lack the technique for making the borders of the box visible for it to be complete. (It turned out to be not too complicated)
Code: Select all
objTextFrame.LineStyle = com.sun.star.drawing.LineStyle.SOLID
objTextFrame.LineWidth = 100
objTextFrame.LineColor = rgb(255, 0, 0)
It is maybe time to discover Xray or MRI...
Imagine, just for the line, discover: “
LineCap”, “
LineColor”, “
LineDash”, “
LineDashName”, “
LineEnd”, “
LineEndCenter”, “
LineEndName”, “
LineEndWidth”, “
LineJoint”, “
LineStart”, “
LineStartCenter”, “
LineStartName”, “
LineStartWidth”, “
LineStyle”, “
LineTransparence”, “
LineWidth”...
And for the margin: “
LeftMargin”, “
RightMargin”, “
TopMargin”, “
BottomMargin”, “
ParaLeftMargin”, “
ParaRightMargin”, “
ParaTopMargin”, “
ParaBottomMargin”.
A dream isn’t it ?
Re: Inserting boxes and shapes
Posted: Tue Jun 12, 2018 10:38 am
by Cogito ergo sum
Well, thank you once Again Sébastian - although objTextFrame.LineStyle = com.sun.star.drawing.LineStyle.SOLID throws up an error, but if I change it quite simply to objTextFrame.LineStyle = 1 then it works fine. Like a dream as you say but the dream becomes a nightmare again if for example I try the same thing with LineDash in order to try and get a dotted border. I now have:
Code: Select all
Dim args(),aStruct
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
Set oDsk= objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oDoc= oDsk.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
Set objSize = createStruct("com.sun.star.awt.Size")
Set objText= oDoc.getText
Set objCursor= objText.createTextCursor
Set objTextFrame= oDoc.createInstance("com.sun.star.drawing.TextShape")
objSize.Width = 7500
objSize.Height = 2530
objText.insertTextContent objCursor, objTextFrame, false
objTextFrame.setSize(objSize)
objTextFrame.String = VbLf & "Hello World !!! Hello World !!! Hello World !!! Hello World !!!"
objTextFrame.rotateAngle = 4500
objTextFrame.paraleftmargin = 300
objTextFrame.topmargin = 700
objTextFrame.LineStyle = 1
objTextFrame.LineWidth = 10
objTextFrame.LineColor = rgb(255, 0, 0)
objTextFrame.AnchorType = AS_CHARACTER
objTextFrame.IsFollowingTextFlow = False
objTextFrame.CharHeight="14"
objTextFrame.CharFontName="Trebuchet MS"
objTextFrame.CharColor="1500"
objTextFrame.HoriOrient = 0
objTextFrame.VertOrient = 0
objTextFrame.VertOrientPosition = 3500
objTextFrame.HoriOrientPosition = 2000
Function createStruct( strTypeName)
Set classSize= objCoreReflection.forName( strTypeName)
classSize.createObject aStruct
Set createStruct= aStruct
End Function
and as can be seen I put a VbLf & before the text because although paraleftmargin works paratopmargin doesn't although it doesn't throw up an error. It could be an idea to try Xray, but does it do more than simply list parameters, properties and methods? What I seem to be lacking is the basic understanding of why some things work and others don't when it would seem logical that when the one works the other would too. And yes I have tried to read Pitonyaks stuff - it's excellent but over my head a lot of the time, unfortunately.
Re: Inserting boxes and shapes
Posted: Tue Jun 12, 2018 7:47 pm
by Lupp
There are only three LineStyle constants: NONE, SOLID, and DASH.
To use them you need to give the complete qualified names:
Code: Select all
com.sun.star.drawing.LineStyle.NONE
com.sun.star.drawing.LineStyle.SOLID
com.sun.star.drawing.LineStyle.DASH
A lot to type for 0, 1, and 2.