[Solved] How to copy Sheet with Event macro via VBA code

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
t-tymczas
Posts: 9
Joined: Sat Jan 03, 2015 2:34 pm

[Solved] How to copy Sheet with Event macro via VBA code

Post by t-tymczas »

Hi.
My problem is about copying sheet with attached event macros.
I have a Calc document. One of the sheets has attached Event Macro (macro that runs every time when any cell value is changer by user). Another macro is used for copying the previous one. But the copy does not have Event macro attached anymore.
I need a VBA code that copies sheet with attached event macro, or Vba code to reassign event macto to new sheet(new copy).
Thanks for any help.

PS. Event macro is attached to specyfic sheet not globaly in whole document.
Last edited by Hagar Delest on Sat Jan 03, 2015 11:20 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 4.2, Windows 8.1
F3K Total
Volunteer
Posts: 1048
Joined: Fri Dec 16, 2011 8:20 pm

Re: How to copy Sheet with Event macro via vba code

Post by F3K Total »

Hello,
try this:

Code: Select all

sub S_Copy_Sheet_With_Events
    oSheets = ThisComponent.sheets
    osheet = thiscomponent.currentController.activesheet
    nSheet = osheet.Rangeaddress.Sheet
    sName = osheet.Name
    sNewName = inputbox("Insert Name of copied sheet","Copy this sheet",sName)
    oSheets.copyByName( sName, sNewName, nSheet + 1)
    onewsheet = oSheets(nSheet + 1)
    aSheetEvents = osheet.Events
    sEventNames = aSheetEvents.ElementNames
    aNewSheetEvents = onewsheet.Events
    for i = 0 to ubound(aSheetEvents.ElementNames)
        aEvent = aSheetEvents.getbyname(sEventNames(i))
        aNewSheetEvents.replaceByName(sEventNames(i),aEvent)
    next i
end sub
R
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 11 AOO, LO | Linux Mint AOO, LO
t-tymczas
Posts: 9
Joined: Sat Jan 03, 2015 2:34 pm

Re: How to copy Sheet with Event macro via vba code

Post by t-tymczas »

Yes. That's It.
Worked like magic :))
Thanks a lot.
LibreOffice 4.2, Windows 8.1
Post Reply