Ask Your Question

is it possible to save and reopen a file from a macro

asked 2019-09-20 21:03:32 +0200

newbie-02 gravatar image

hi all,

regarding the problem that libreoffice becomes very slow after saving a file with plenty of comments / notes in it, i'm working with autosave 'off', and would like to have a macro that:

  • takes the actual filename,
  • checks whether it ends in a timestamp formatted as: YYYY-MM-DD_HH-MM
  • if yes replaces that by the actual timestamp,
  • if not appends the actual timestamp,
  • saves the file with that new name,
  • closes it,
  • and reopens it,

a friend of mine who tried was blocked by the fact that on closing the file the macro was interrupted, also when not! applied to the file but to 'my macros' of the program,

the close and reopen is! neccessary, it's the only way i know to keep calc 'responisve' after a save or autosave of large files with plenty comments.

any help appreciated,



edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2019-11-17 15:39:55 +0200

newbie-02 gravatar image

updated 2019-11-17 17:48:45 +0200

Ratslinger gravatar image

yes it is,

take something from the following code - it does additional things, like first saving a timestamped copy - but in the end it closes the file and reopens it.

was useful while struggeling with calc becoming slow after saving of files with plenty comments, that behaviour is - nearly - off in ver. (it came back with subsequent versions), but the macro functionality may be useful for other users for other purposes ...

sub schliessenoeffnen '--------------------------br-06.09.2019

Dim oDoc, oForm, oblatt, oDatei as Object 

' remember file
odoc = thiscomponent 
speicherOrt = ConvertFromUrl(odoc.getLocation) 

' strip if timestamped, apply new timestamp, save
extens = Right(speicherort,4) 
dateilen = len(speicherort)-4 
dateibase = left(speicherort, len(speicherort)-4)
tail = right(dateibase,17)
tlen = Len(Tail)
  pos1 = instr(tail,"_") 
  pos2 = instr(tail,"-") 
  if pos1 > 0 and pos2 > 0 then 
    if pos1+ 5 <> pos2 then 
      notimestamp = true 
      if Mid(tail, pos1+ 11,  1) <> "_" or Mid(tail, pos1+ 8, 1) <> "-" or Mid(tail, pos1+ 14, 1) <> "-" then 
        notimestamp = true 
        dateibase = left(dateibase,len(Dateibase)-tlen) 'strip old timestamp
      end if 
    end if 
      dateibase = dateibase & "_" & Format(Now(),"YYYY-MM-DD") & "_" & format(time(),"HH-MM" 'add new timestamp 
    dateibase = dateibase & "_" & Format(Now(),"YYYY-MM-DD") & "_" & format(time(),"HH-MM" 
  end if 

' save file
fname = dateibase & extens 
surl = converttourl(fname) 
dim dummy() 

' Formular schliessen 
oDoc = thisComponent.currentController.frame 
oDispatch = createUnoService("") 
oDispatch.executeDispatch(oDoc, ".uno:CloseDoc", "", 0, Array()) 

' Formular öffnen 
DIM sDatei, sPfad as string 
oDoc = ThisComponent 
sDatei = fname 

sUrl_1 = ConvertToURL(sDatei) 'Umwandeln URL 
sPfad =  "scalc.exe" 'Pfad der zu verwendenden Applikation 
Shell(sPfad + " " + sDatei,3) 'nun öffnen der Datei mit dem angegebenen Programm 

end sub 'schliessenoeffnen

sorry - i tried! to insert as code - it didn't work,

sorry - parts are in german,

beware - i did some prettyfying before insert - thus may be buggy,



P.S. is there a way to insert 'code' as a block? i couldn't find, had to do it line by line, otherwise it (expletive deleted) the formatting ... painful

edit flag offensive delete link more

answered 2019-11-17 19:33:12 +0200

A basic macro, it saves and reloads the file. need to adapt to your need.

Sub SaveRecharge
dim document, dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("")
dispatcher.executeDispatch(document, ".uno:Save", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:Reload", "", 0, Array())
end sub
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-09-20 21:03:32 +0200

Seen: 133 times

Last updated: Nov 17 '19