| View previous topic :: View next topic |
| Author |
Message |
mneumann General User


Joined: 22 Sep 2003 Posts: 40 Location: Austria
|
Posted: Thu Dec 08, 2005 10:18 am Post subject: Replacing Image in OO 2.0 does not work anymore |
|
|
I had a nice macro that was running fine for OO 1.1.5, but in OO 2.0 Writer it will not work anymore. The purpose of this macro is to take an image file in a Writer document and replace it with a new version of the image. Does anybody know what is the problem? Or another method to do that? Here is the code:
| Code: |
Function LoadGraphicIntoDocument( oDoc As Object, cUrl As String, cInternalName As String ) As String
' Get the BitmapTable from this drawing document.
' It is a service that maintains a list of bitmaps that are internal
' to the document.
Dim oBitmaps as Object
Dim cNewURL as String
oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )
If oBitmaps.hasByName (cInternalName) Then
oBitmaps.replaceByName (cInternalName, cUrl)
Else
' Add an external graphic to the BitmapTable of this document.
oBitmaps.insertByName( cInternalName, cUrl )
End If
' Now ask for it back.
' What we get back is an different Url that points to a graphic
' which is inside this document, and remains with the document.
cNewUrl = oBitmaps.getByName( cInternalName )
LoadGraphicIntoDocument = cNewUrl
End Function
|
|
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Thu Dec 08, 2005 1:38 pm Post subject: |
|
|
That particular method, which was written by Danny B, never really inserted an image so that it is viewable.
| Code: | Sub Main
Dim sURL$
Dim s$
Dim oBitmaps
REM Insert a reference to the external file
sURL = "file:///C:/form1.gif"
REM Insert a reference to an internally contained graphic.
oBitmaps = ThisComponent.createInstance( "com.sun.star.drawing.BitmapTable" )
s = LoadGraphicIntoDocument(ThisComponent, sURL, "DBGif2")
InsertGraphicObjectShape(ThisComponent, s)
End Sub
Sub InsertGraphicObjectShape(oDoc, sURL$)
REM Author: Andrew Pitonyak
Dim oSize As New com.sun.star.awt.Size
Dim oPos As New com.sun.star.awt.Point
Dim oGraph
REM First, create a graphic object shape
oGraph = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
REM Size and place the graphic object.
oSize.width=6000
oSize.height=8000
oGraph.setSize(oSize)
oPos.X = 2540
oPos.Y = 2540
oGraph.setposition(oPos)
REM Assuming a text document, add it to the single draw page.
oDoc.getDrawpage().add(oGraph)
REM Set URL to the graphic.
oGraph.GraphicURL = sURL
End Sub
Function LoadGraphicIntoDocument( oDoc As Object, cUrl As String, cInternalName As String ) As String
' Get the BitmapTable from this drawing document.
' It is a service that maintains a list of bitmaps that are internal
' to the document.
Dim oBitmaps as Object
Dim cNewURL as String
oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )
If oBitmaps.hasByName (cInternalName) Then
oBitmaps.replaceByName (cInternalName, cUrl)
Else
' Add an external graphic to the BitmapTable of this document.
oBitmaps.insertByName( cInternalName, cUrl )
End If
' Now ask for it back.
' What we get back is an different Url that points to a graphic
' which is inside this document, and remains with the document.
cNewUrl = oBitmaps.getByName( cInternalName )
LoadGraphicIntoDocument = cNewUrl
End Function |
I have another method by Frank S that will also work in my free macro document. _________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
mneumann General User


Joined: 22 Sep 2003 Posts: 40 Location: Austria
|
Posted: Fri Dec 09, 2005 11:04 am Post subject: |
|
|
| pitonyak wrote: | That particular method, which was written by Danny B, never really inserted an image so that it is viewable.
|
Well, I do not need to insert a graphic, I just need to replace it. This worked very fine until OO 1.1.5, but not anymore in OO 2.0. It throws a runtime error when asking back the name with the getbyname function. And it seems to do that only once it was running the insertbyName section. The replacebyName does not throw a runtime error, but it does not seem to do anything either. I just wonder what has changed in OO 2.0 in regard to that matter. |
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Fri Dec 09, 2005 12:29 pm Post subject: |
|
|
Take a look here:
http://api.openoffice.org/docs/common/ref/com/sun/star/drawing/BitmapTable.html
| Quote: | | Note: You can add new entries for later use, but you cannot remove entries that are used inside the document. |
That said, replace by name appears to be broken. Can you file a bug report on this? Here is a hack that perhaps you can take the time to clean-up, it works for me.
| Code: | Dim sURL$
Dim s$
Dim oBitmaps
Dim cNewURL as String
Dim cInternalName As String
Dim cUrl$
Dim i%
cUrl$ = "file:///C:/d1.gif"
cInternalName = "DBGif2"
oBitmaps = ThisComponent.createInstance( "com.sun.star.drawing.BitmapTable" )
Inspect(oBitmaps)
If NOT oBitmaps.hasByName("DUMMY") Then
oBitmaps.insertByName( "DUMMY", "file:///C:/x.gif" )
End If
If oBitmaps.hasByName (cInternalName) Then
sURL = oBitmaps.getByName( cInternalName )
s = s & oBitmaps.getByName( cInternalName ) & CHR$(10)
Dim oDP : oDP = ThisComponent.getDrawpage()
Dim oObj
For i = 0 To oDP.getcount() - 1
oObj = oDP.getByIndex(i)
If oObj.supportsService("com.sun.star.drawing.GraphicObjectShape") then
If oObj.GraphicURL = sURL Then
oObj.GraphicURL = oBitmaps.getByName("DUMMY")
End If
End If
Next
'Exit Sub
oBitmaps.removeByName(cInternalName)
oBitmaps.insertByName( cInternalName, cUrl )
'oBitmaps.replaceByName (cInternalName, cUrl)
Print "See the other image"
sURL = oBitmaps.getByName( "DUMMY" )
For i = 0 To oDP.getcount() - 1
oObj = oDP.getByIndex(i)
If oObj.supportsService("com.sun.star.drawing.GraphicObjectShape") then
If oObj.GraphicURL = sURL Then
oObj.GraphicURL = oBitmaps.getByName( cInternalName )
End If
End If
Next
'oBitmaps.insertByName( cInternalName, cUrl )
Else
' Add an external graphic to the BitmapTable of this document.
oBitmaps.insertByName( cInternalName, cUrl )
End If
s = s & oBitmaps.getByName( cInternalName ) & CHR$(10)
MsgBox s
|
_________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
|
|
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
|