Ask Your Question
0

macro doesn't work if I add a second sheet in calc [closed]

asked 2019-04-10 00:16:05 +0200

giacomoleopardo gravatar image

updated 2019-04-16 15:18:50 +0200

Jim K gravatar image

I'm trying to make this macro working to clear all data fields in my calc file.

Sub Erase_Fields
    oSheet = ThisComponent.getSheets()
    oObj1 = oSheet.getByIndex(0)
    oForm = oObj1.Drawpage.Forms.getByName("Data")
    oField1 = oForm.getByName("data1")
    oField1.Text = " "
    oField1.Text = ""
    oField2 = oForm.getByName("data2")
    oField2.Text = " "
    oField2.Text = ""
    oField3 = oForm.getByName("data3")
    oField3.Text = " "
    oField3.Text = ""
end sub

It works (thanks to @Ratslinger here but it seems to work just with one sheet. If I add a second sheet, it gives me back an error. I need to clear only the date fields on the first sheet, but I need to add other sheets to the file.C:\fakepath\Date field multiple.ods

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by erAck
close date 2019-04-16 16:16:16.065843

1 Answer

Sort by » oldest newest most voted
1

answered 2019-04-10 01:02:55 +0200

Ratslinger gravatar image

updated 2019-04-10 01:19:51 +0200

Hello,

You are better off using the second method mentioned in the original linked answer. It will keep the data erased and will not refresh by simply changing sheets. Here is the code needed even when it is not the first sheet (uses sheet name):

Sub Erase_Fields
    oSheet = ThisComponent.getSheets()
    oObj1 = oSheet.getByName("Foglio1")
    oForm = oObj1.Drawpage.Forms.getByName("Data")
    DocCtl = ThisComponent.getCurrentController()
    oField = oForm.getByName("data1")
'Get VIEW'
    CtlView = DocCtl.GetControl(oField)
    CtlView.setEmpty()    
    oField = oForm.getByName("data2")
'Get VIEW'
    CtlView = DocCtl.GetControl(oField)
    CtlView.setEmpty()    
    oField = oForm.getByName("data3")
'Get VIEW'
    CtlView = DocCtl.GetControl(oField)
    CtlView.setEmpty()    
end sub
edit flag offensive delete link more

Comments

Thank you. This is the perfect, elegant solution. It works just fine!

giacomoleopardo gravatar imagegiacomoleopardo ( 2019-04-10 06:39:07 +0200 )edit

Do not add "[SOLVED]" to the title. Instead, mark the answer as accepted. See guidelines for asking.

Jim K gravatar imageJim K ( 2019-04-10 18:00:50 +0200 )edit

Done. Thanks for the tip

giacomoleopardo gravatar imagegiacomoleopardo ( 2019-04-13 00:19:20 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2019-04-10 00:16:05 +0200

Seen: 32 times

Last updated: 2 days ago