Strange Macro problem

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
promy
Posts: 1
Joined: Fri Nov 15, 2013 10:45 am

Strange Macro problem

Post by promy »

I was changing a simple macro i was using in excel to openoffice but i get a strange behaviour:
The code that is giving an error is:

Code: Select all

Cells(ActiveCell.Row, 1).select
This trows an error: "BASIC runtimeerror Object Variable not defined".
Now for the strange part: if I Click the OK button and just add a space, new line or whatever in the code (at any point in the code), it doesn't give the error anymore...until you close and reopen the file

Does anyone know how to solve this? I just want to select the first cell of the current row.
Openoffice 4.01 on windows XP
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Strange Macro problem

Post by JohnV »

This isn't a strange problem because the Basic languages are completely different. Not sure what you are trying to do but this might do it.

Code: Select all

Sub Main
oDoc = ThisComponent
CS = oDoc.CurrentSelection 'the real world cursor location and it must not span cells
oSheet = CS.Spreadsheet
oCell = oSheet.getCellByPosition(0,CS.CellAddress.Row)
print oCell.String 'or Value or Formula
REM This does not change the real world cursor location but the following does if needed.
oDoc.CurrentController.Select(oCell) 
End Sub
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Strange Macro problem

Post by Charlie Young »

JohnV punched in while I was playing with this, but I have two different ways.

The more straightforward:

Code: Select all

Sub SelectFirstCellInRow()
	Dim oCell As Object
	
	oCell = ThisComponent.getCurrentController().getSelection()
	If oCell.supportsService("com.sun.star.sheet.SheetCell") Then
		ThisComponent.getCurrentController().select(oCell.getRows().getByIndex(0).getCellByPosition(0,0)
		ThisComponent.getCurrentController().select(ThisComponent.createInstance("com.sun.star.sheet.SheetCellRanges"))
	End If
End Sub
The fancy way (I like this one better):

Code: Select all

Sub SelectFirstCellInRow2()
	Dim oCell As Object
		
	oCell = ThisComponent.getCurrentController().getSelection()
	If oCell.supportsService("com.sun.star.sheet.SheetCell") Then
		ChangeViewData(oCell.CellAddress.Sheet,oCell.CellAddress.Row,0)
	End If
End Sub

Sub ChangeViewData(Sheet As Integer,r As Long,c As Integer) 
	
	Dim vData As String
	Dim v
	Dim s As Integer
	Dim vSheet
	Dim PlusSlash As String
			
	if not IsNull(ThisComponent.CurrentController) then
		s = ThisComponent.Sheets(Sheet).RangeAddress.Sheet	
		vData = ThisComponent.CurrentController.ViewData
		v = Split(vData,";")
		v(1) = Trim(Str(s))
		if InStr(v(s + 3),"/") > 0 then
			PlusSlash = "/"
		else
			PlusSlash = "+"
		endif
		vSheet = Split(v(s + 3),PlusSlash)
			
		vSheet(1) = Trim(Str(r))
		vSheet(0) = Trim(Str(c))
		
		v(s + 3) = Join(vSheet,PlusSlash)
		vData = Join(v,";")
		
		ThisComponent.CurrentController.restoreViewData(vData)
	endif
		
End Sub
Apache OpenOffice 4.1.1
Windows XP
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Strange Macro problem

Post by Villeroy »

Code: Select all

REM  *****  BASIC  *****


sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args10(0) as new com.sun.star.beans.PropertyValue
args10(0).Name = "Sel"
args10(0).Value = false

dispatcher.executeDispatch(document, ".uno:GoToStartOfRow", "", 0, args10())

end sub
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
Post Reply