How to create save-as filename from data within the document in writer

I am starting to use LibreOffice, and I have a problem. I can’t run my macros as I could in MS Word.

I got an error when I am trying to use my code, that works without any problems in MS Word.

My code:

Sub SKABELON_MEMORADIUM()
    Dim Stry As Range, TOC As TableOfContents
    Dim StrPath As String, StrName As String, StrName1 As String

    'Set Default save path
    StrPath = "C:\Users\" & Environ("UserName") & "\OneDrive\6. MEMORADIUM\"
    With ActiveDocument
      Application.ScreenUpdating = False
      For Each Stry In .StoryRanges
        Stry.Fields.Update 'UPDATE FIELDS IN ALL STORY RANGES
      Next
      For Each TOC In .TablesOfContents
        TOC.Update     'UPDATE TABLES OF CONTENTS
      Next
      Application.ScreenUpdating = True
      'Check whether the filename contains 'MEMORADIUM - '
      'If not, that's what we need to save it with
      If InStr(.Name, "MEMORADIUM - ") = 0 Then
        'Get the contents of the EMNE content control so we can add it to the filename
        StrName = .SelectContentControlsByTitle("EMNE")(1).Range.Text
        StrName1 = .SelectContentControlsByTitle("ID")(1).Range.Text
        'Display the FileSaveAs dialogue
        With Dialogs(wdDialogFileSaveAs)
          .Name = StrPath & "ID" & " " & StrName1 & " " & "MEMORADIUM - " & StrName
          .Format = wdFormatXMLDocument
          .Show
        End With
      Else
        .Save
      End If
    End With

    Dim oStory As Object
    Dim oToc As Object

    'EXIT IF NO DOCUMENT IS OPEN
    If Documents.Count = 0 Then Exit Sub
    Application.ScreenUpdating = False

    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update 'UPDATE FIELDS IN ALL STORIES
    Next oStory

    For Each oToc In ActiveDocument.TablesOfContents
        oToc.Update 'UPDATE TABLE OF CONTENTS
    Next oToc

    Application.ScreenUpdating = True
End Sub

@SteffenBuchhave: Please do not post as community wiki. See guidelines for asking.

LibreOffice Basic and the UNO API are very different from VBA with MS Office.

I have no idea what ActiveDocument.StoryRanges does in MS Office, although this discussion mentions it: Word/VBA > LibreOffice Macro Conversion (View topic) • Apache OpenOffice Community Forum

You need to study how to write LibreOffice macros and then translate the code. One place to start is http://www.pitonyak.org/oo.php.

If you have a lot of macros, I would recommend taking the time to learn Python-UNO rather than Basic, but that’s up to you. Either way, post a specific question if you have a doubt regarding how to write something for LibreOffice.

Don’t simply ask, “How do I translate <VBA code xxx> into LibreOffice API code.” Instead, ask something like “How do I get the macro to do www? I tried xxx based on LibreOffice documentation yyy but it gave an error zzz.”

Did you tread this thread? It contains some links. The code you can find this way will show you how to use the uno API in the context. To get able to modify the target in the direction of selecting more specific elements from the content to build the file name may require some studies, though.