Could you help me Understand this simple macro code to copy values?

So basically, my boss would like a spreadsheet automated. I’m already familiar with several other programming languages and I did use Visual Basic 6.0 way, way, way back in the day, so this doesn’t look too unfamiliar. However, when I tried to record a macro, I developed two possible issues.

1.) The code did not work (I modified it slightly)
2.) The note behind record macros says “may be limited”

sub copy
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 args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$H$10:$H$12"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "CopyToHere"
args2(0).Name = "$J$10:$J$12"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())

end sub

When I executed this, from the recording, I copied the values from H10:H12 to J10:J12. However, by default, there was no mention of of the destination in the macro. While this asks for copy confirmation, it does not seem to copy anything.

Can anyone tell me any of:
1.) Where does the com.sun.star sequence come from?
2.) What does the 0 in parenthesis mean behind Args1?

I added all the lines that involve the term “args2”, but it didn’t help move the cursor or anything.

Thanks a lot!

Try to download this book and read chapters 5.23.1 Copy Spreadsheet Cells With The Clipboard and 5.23.2 Copy Spreadsheet Cells Without The Clipboard

Featured fix.

To paste, just use the upper left cell of the area.

sub copy
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 args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$H$10:$H$12"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue

rem xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
args2(0).Name = "ToPoint"
args2(0).Value = "$j$10"
rem xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args2())
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
end sub

ATTENTION: If you would like to give more details to your question, use edit in question or add a comment below. Thank you.

If the answer met your need, please click on the ball Descrição da imagem to the left of the answer, to finish the question.

1 Like