cursor at text in Writer doc (via com-bridge)

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

cursor at text in Writer doc (via com-bridge)

Post by cornouws »

Hi *,

I work in a Writer document, started from Lotus notes, and work in it via a com-bridge.
Now there is a certain string (#XXXX#) where I want to place a cursor to insert a picture at that position (replacing the string as well).
I know how to replace the string with a string.
Also many other things I've found out, but this one ... ?

Any help appreciated,
Cor
The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
cornouws
Posts: 44
Joined: Mon Jan 14, 2008 10:43 pm

Re: cursor at text in Writer doc (via com-bridge)

Post by cornouws »

Looks as if the problem is. that the string I want to search, is in a table, which makes creating of the cursor different.
But only tomorrow I can check with Lotus Script via Com-bridge and will know for sure.

Anyhow, in OOo Basic this works.
And to show it:

Code: Select all

'-------------------------------------------------------------------------
Sub PlacePictureAtBMInCell
	Dim sGraphicFullName$, sBookmarkName$
	Dim lHeight&, lWidth&
	
	sBookmarkName = "InsertPicture"
	sGraphicFullName = "xxx\TestPlaatje1.jpg"
	lHeight = 2000 : 	lWidth = 2000
	PlaceSignatureGraphic(ThisComponent, sGraphicFullName, sBookmarkName, lHeight, lWidth)
	
End Sub

'-------------------------------------------------------------------------
Private Function PlaceSignatureGraphic (oDoc as Object, sGraphicFullName$, sBookmarkName$, lHeight&, lWidth&) as Integer
'-------------------------------------------------------------------------
	Dim nResult%, i%
	Dim oDrawPage, oGraph
	Dim sURL$,  cUrl$, sCellName$
	Dim oSize
	Dim oText, oCursor, oBookmark, oAnchor, oTable
	Dim bInTable as Boolean
	Dim oDescriptor, oFoundAll, oFound
	Dim bTestWithBookMark as Boolean
	
On Error GoTo ErrorHandler

' choose to use bookmark or a string to find the picture's place
' of course can be argument of the function
	bTestWithBookMark = FALSE
 	
' find the place to insert the picture
	If Not bTestWithBookMark Then
		oDescriptor = ThisComponent.createSearchDescriptor()
		oDescriptor.SearchString = "#InsertPicture#"
		oFoundAll = ThisComponent.findAll(oDescriptor)
		
		' mind! if it is OK, there only will be one instance
		For i = 0 to oFoundAll.getCount()-1
			' mind! IIRC, via Com-bridge in Lotis Script one cannot acces the content of the returned array oFoundAll()
			oFound = oFoundAll.getByIndex(i)
			oTable = oFound.TextTable
			If Not isEmpty(oTable) Then bInTable = TRUE
		Next	
	Else
		oBookmark = oDoc.Bookmarks.getByName(sBookmarkName)
		oAnchor = oBookmark.getAnchor
		oTable = oBookmark.Anchor.TextTable
		If Not isEmpty(oTable) Then bInTable = TRUE
	End If
	
' prepare the cursor
	If bInTable Then
		If Not bTestWithBookMark Then
			sCellName = oFound.Cell.CellName
		Else
			sCellName = ThisComponent.Bookmarks.getByName(sBookmarkName).getAnchor.getStart.Cell.CellName
		End If
		oText = oTable.getCellByName(sCellName).getText()
 		oCursor = oText.createTextCursor() 
		If Not bTestWithBookMark Then
			oCursor.gotoRange (oFound, FALSE)
		Else
			oCursor.gotoRange (oAnchor, FALSE)
		End If
	Else
		oText = oDoc.getText()
		oCursor = oText.createTextCursor()
		oCursor.gotoRange (oAnchor, FALSE)
	End If
		
' load picture in Document via separate function
	sURL =  ConvertToURL(PreserveEndSlash( sGraphicFullName )
	cUrl = LoadGraphicIntoDocument( oDoc, sURL, sGraphicFullName)	
 	oGraph = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
	oSize = createUnoStruct( "com.sun.star.awt.Size" )
	oSize.Width = lWidth
	oSize.Height = lHeight
	With oGraph
		.GraphicURL = cUrl
		.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER '.AT_PARAGRAPH
		.size = oSize
	End With
	
' remove string found/in bookmark
 	oCursor.setString ("")
	
'now insert the image into the text document
	oText.insertTextContent( oCursor, oGraph, False )
	
 	PlaceSignatureGraphic  = 1
Exit Function
ErrorHandler:
	PlaceSignatureGraphic  = -1
	msgBox "Error in PlaceSignatureGraphic :" & CHR(13) & CHR(13) &_
		 Error() & CHR(13) & "on row: " & Erl
End Function

'-------------------------------------------------------------------------
' based on function found in a forum or in Andrew's macro document
Private Function LoadGraphicIntoDocument( oDoc As Object, cUrl$, cInternalName$) As String
'-------------------------------------------------------------------------
' Given a URL to an external graphic resource,
'  load that graphic permanently into this drawing document,
'  and return a new URL to the internal resource.
' The new URL can be used in place of the old URL.
	Dim oBitmaps
	Dim cNewUrl

	oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )
	If oBitmaps.hasByName(cInternalName) Then
		' Bitmap already present
		cNewUrl = oBitmaps.getByName( cInternalName )	
	Else
		' load bitmap
		oBitmaps.insertByName( cInternalName, cUrl )
	End If
   
   ' Get the Url that points to the graphic inside this document
	cNewUrl = oBitmaps.getByName( cInternalName )
   
	LoadGraphicIntoDocument = cNewUrl
End Function

The most recent LibreOffice and OpenOffice.org on Ubuntu - sometimes on Windows XP
Post Reply