[Solved] Macro to close a document

Discuss the spreadsheet application
Post Reply
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

[Solved] Macro to close a document

Post by MATT.B »

I have been searching for a simple code for closing the active calc document in basic
only thing I have been able to find is doc.close (true) but it doesn't mention how to define doc
I have set up a long code for open office when I open a document it loads a macro that changes sections of the document and then saves it under a new file name after that I want it to close the active window/document, this way I can open all 400 files at once oo will change the cells to the new locations, set the file name, save as ods and pdf , then close before it will open the next document, with the way the stack works the macro must complete before open office will attempt to open the next file in queue. right now it works oo opens the file moves the cells and saves. but doesn't close so I run out of memory before all files are converted to the new format.

to explain this better I updated one of my documents to a new format so another program I created will get data from it the problem is the old documents aren't formatted correctly so I created a macro to fix the format and save so all calc documents (of this type)are formatted the same
Last edited by MATT.B on Sun Nov 01, 2015 7:12 am, edited 1 time in total.
OpenOffice 4.1.1 ON WINDOWS 7 64
User avatar
RoryOF
Moderator
Posts: 35267
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: MACRO TO CLOSE DOCUMENT

Post by RoryOF »

This thread might help
viewtopic.php?f=20&t=73209
Apache OpenOffice 4.1.16 on Xubuntu 26.04.1 LTS
FJCC
Moderator
Posts: 9653
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: MACRO TO CLOSE DOCUMENT

Post by FJCC »

Code: Select all

ThisComponent.close(True)
should work but you must have created a reference to the document when you opened it. Do you have a line of code like

Code: Select all

NewDoc = StarDesktop.loadComponentFromURL()
That NewDoc variable will have the close() method.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

Code: Select all

NewDoc = StarDesktop.loadComponentFromURL()
That NewDoc variable will have the close() method.
no I do not have a line like that the documents are being opened by me using windows explorer
is there a way to add that it for the way I am opening the documents?
OpenOffice 4.1.1 ON WINDOWS 7 64
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

so I added ThisComponent.close(True) to my code and it seems to be working fine thank you don't know why I didn't think of that figured it wasn't that simple
OpenOffice 4.1.1 ON WINDOWS 7 64
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

yeah that doesn't work as expected now I see why the variable needs to be set
OpenOffice 4.1.1 ON WINDOWS 7 64
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

any ideas on how to do this with the current open document if it helps the variable myfilename is the document filename and variable mypath is the path the doc is saved in
example;
myfilename = example.ods
mypath = c:\users\main\documents\ods\
and
url = mypath + myfilename in url format
the problems is the vars. aren't set till after the doc is opened
OpenOffice 4.1.1 ON WINDOWS 7 64
FJCC
Moderator
Posts: 9653
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: MACRO TO CLOSE DOCUMENT

Post by FJCC »

Please show your actual code. Using ThisComponent should work but being able to test real code often reveals details I had not considered.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

edit post
changed code.ods

the way this is set up on my system is the macros are stored in person macros on on the doc its self
when view is created
code.ods
(59.06 KiB) Downloaded 504 times
OpenOffice 4.1.1 ON WINDOWS 7 64
FJCC
Moderator
Posts: 9653
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: MACRO TO CLOSE DOCUMENT

Post by FJCC »

I don't find the word close anywhere in your code. Where is it that you want to put that?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

oh crap lol sorry i changed a lot of the code so please as a new sub and ill just call to it when i need it
the close doc code
newcode.txt
(9.57 KiB) Downloaded 481 times
OpenOffice 4.1.1 ON WINDOWS 7 64
FJCC
Moderator
Posts: 9653
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: MACRO TO CLOSE DOCUMENT

Post by FJCC »

I stored this code in My Macros

Code: Select all

Sub SaveAs1 
On Error GoTo UNKNOWNERROR
if fnWhichComponent(thisComponent) <> "Spreadsheet" then
  exit sub
end if
Dim check as string
dim document as object
dim dispatcher as object
Dim MyFilename as string
dim MyPath as string
Dim url as string
dim url1 as string
DIM PAGE AS STRING
document = ThisComponent.CurrentController.Frame
oDoc = ThisComponent
oView = oDoc.getCurrentController()
oSheet = odoc.getSheets().getByIndex(0)
oCell = oSheet.getCellRangeByName("ZZ2")
CHECK = oCell.getstring
If check = 5615 then
	oCell = oSheet.getCellRangeByName("U2")
	myFilename = oCell.getstring
	IF MyFilename = "  " then
		ret& = Msgbox ("Infromation is missing, you will be prompted to save!" , 10 + 1, "INFORMATION MISSING")
		EXIT SUB
		end if
	oCell = oSheet.getCellRangeByName("U4")
	MyPath = "C:\Users\fjcc\Desktop\"     'FJCC
	'MyPath = oCell.getstring
	url = ConvertToUrl(MyPath & MyFilename & ".ods") 
	url1= ConvertToUrl(MyPath & MyFilename & ".pdf") 
	'ret& = Msgbox ("Files saved to folder " & MyPath &  " with a filenames of " & MyFilename & " .ods and " & MyFilename & " .pdf", 10 + 1, "Information")
	oDoc.StoreAsUrl(url,Array()) 
dim args12(1) as new com.sun.star.beans.PropertyValue
dim Arg(0) as new com.sun.star.beans.PropertyValue
oCell = oSheet.getCellRangeByName("W47")
	page = oCell.getstring
IF PAGE = 1 THEN
	Arg(0).Name = "PageRange"
	Arg(0).Value = "1"
	args12(1).Name = "FilterData"
	args12(1).Value = Arg()
	goto pdf
	END IF
IF PAGE = 2 THEN
	Arg(0).Name = "PageRange"
	Arg(0).Value = "1,2"
	args12(1).Name = "FilterData"
	args12(1).Value = Arg()
	goto pdf
	END IF
IF PAGE = 3 THEN
	Arg(0).Name = "PageRange"
	Arg(0).Value = "1,2,3"
	args12(1).Name = "FilterData"
	args12(1).Value = Arg()
	goto pdf	
	END IF
IF PAGE = 4 THEN
	Arg(0).Name = "PageRange"
	Arg(0).Value = "1,2,3,4"
	args12(1).Name = "FilterData"
	args12(1).Value = Arg()
	goto pdf
	END IF
IF PAGE = 5 THEN
	Arg(0).Name = "PageRange"
	Arg(0).Value = "1,2,3,4,5"
	args12(1).Name = "FilterData"
	args12(1).Value = Arg()
	goto pdf	
	END IF			
IF PAGE = 6 THEN
	Arg(0).Name = "PageRange"
	Arg(0).Value = "1,2,3,4,5,6"
	args12(1).Name = "FilterData"
	args12(1).Value = Arg()
	goto pdf
	END IF
PDF:	
args12(0).Name = "FilterName"
args12(0).Value = "calc_pdf_Export"
oDoc.storeToURL(url1,args12())
oDoc.close(TRUE) 'FJCC
EXIT SUB
end if
UNKNOWNERROR:
ret& = Msgbox ("Unknown error, Auto save macro is disabled, and you will be prompted to save", 10 + 1, "ERROR")
End Sub

function fnWhichComponent(oDoc) as string
if HasUnoInterfaces(oDoc, "com.sun.star.lang.XServiceInfo") then 
   if thisComponent.supportsService ("com.sun.star.text.GenericTextDocument") then
      fnWhichComponent = "Text"
   elseif thisComponent.supportsService("com.sun.star.sheet.SpreadsheetDocument") then
      fnWhichComponent = "Spreadsheet"
   elseif thisComponent.supportsService("com.sun.star.presentation.PresentationDocument") then
      fnWhichComponent = "Presentation"
   elseif thisComponent.supportsService("com.sun.star.drawing.GenericDrawingDocument") then
      fnWhichComponent = "Drawing"
   else
      fnWhichComponent = "Oops current document something else"
   end if
else
   fnWhichComponent = "Not a document"
end if
End function
The only edits from what you posted are a change to the MyPath variable so it points to my desktop and the addition of the line oDoc.close(TRUE) near the end of the sub. Both edits are marked with 'FJCC. With your original .ods file open and a file name in the cell U2, I ran the macro and it saved ods and PDF versions of the file to my desktop and then closed the file. Is that what you want?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

Yeah exactly what I did but it would close all windows not just the active one and that's where I was having issues I needed only the active window to close.
it would close the background window while it was still saving causing files to get corupted
OpenOffice 4.1.1 ON WINDOWS 7 64
FJCC
Moderator
Posts: 9653
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: MACRO TO CLOSE DOCUMENT

Post by FJCC »

That sounds more like a program crash than the close() process, which only affects the document that calls it. I can run the macro with several documents open with no problem.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

FJCC wrote:That sounds more like a program crash than the close() process, which only affects the document that calls it. I can run the macro with several documents open with no problem.
I think the crash is caused by my saveas1 macro is ran at event document is going to be closed I removed the close function from saveas1 and added it to prep after saveas1 is called
also I have startup running at event view created
I want it at loading finished but because the macro is ran before the view is created it send back an error
I think at this time I just need to scrap the auto close it doesn't wait for the macros to be executed on close causing crashes that lead to my macro library being deleted
but I do thank you for all your help and if I could add rep I would
OpenOffice 4.1.1 ON WINDOWS 7 64
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: MACRO TO CLOSE DOCUMENT

Post by MATT.B »

the biggest issue I have with writing macros is I cant seem to find where the code lookup is at ,in excel you can bring up an object browser that allows you to see all the possible code sections it also tells you who the parent is for that code section. in excel I could open the browser and search for close and it would show me all the possible parents for that code snippet. ie Windows.close / workbooks("example.xls").close or workbook before close ie. sub main workbook before close (cancel as Boolean)
OpenOffice 4.1.1 ON WINDOWS 7 64
MATT.B
Posts: 165
Joined: Tue Nov 18, 2014 2:16 am

Re: [solved]Macro to close a document

Post by MATT.B »

thiscomponent.close(true) works for currently displayed document but if you want to close a document that's in the background you have to do something like
doc1 = thiscomponent
when that document is in focus
and then later do
doc1.close(true)
will close the document even if its in the background
OpenOffice 4.1.1 ON WINDOWS 7 64
Post Reply