How to copy sheets to another workbook using pure libreoffice basic code?

It is simple to copy all the sheets to other workbook with VBA code:

    Sub CopyWorkbook()
    Dim sh as Worksheet,  wb as workbook
    Set wb = workbooks("Target workbook")
    For Each sh in workbooks("source workbook").Worksheets
       sh.Copy After:=wb.Sheets(wb.sheets.count) 
    Next sh
    End Sub

How can accomplish the same job with pure libreoffice basic code?

Cross posted on StackOverflow.

If you cross post, as a courtesy please let us know that you have done so, otherwise it leads to several discussions and a waste of time because several identical answers may be posted by different users.

https://wiki.documentfoundation.org/Macros/Basic/Calc/Sheets

Screenshot From 2025-02-02 08-25-54

Ah yes - user has a habit of removing their own posts when they find a solution.

Copy all worksheets in source file “/tmp/sample.ods” into target file “/tmp/new.ods”:

Sub Main
    Dim FileProperties(0) As New com.sun.star.beans.PropertyValue
    FileProperties(0).Name = "Hidden"
    FileProperties(0).Value = true

    Url_new = "private:factory/scalc"
    set Target = StarDesktop.loadComponentFromURL(Url_new, "_blank", 0,FileProperties)


    Path_source = "/tmp/sample.ods"
    Url_source = ConvertToUrl(Path_source)
    Source = StarDesktop.loadComponentFromURL(Url_source, "_blank", 0, FileProperties)

    position = 0
    For Each sheet In Source.Sheets        
        Target.sheets.importSheet(Source, sheet.Name, position)
        position = position +1
    Next

    Path_target = "/tmp/new.ods"
    Url_target = ConvertToUrl(Path_target)
    Target.storeAsURL(Url_target,FileProperties)
End Sub

Help someone make it more shorter.

shorter :wink:

cp /tmp/sample.ods /tmp/new.ods

1 Like

With pure libreoffice basic ,instead of bash.

Copying all the sheets is the equivalent of copying the file, so why not just do that? A quick search with your favourite search engine would find FileCopy Statement.

This is not a solution to your problem; please delete it and re-post as a comment.