Save a Writer .odt document from VBScript

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
Granchio_54
Posts: 4
Joined: Mon Oct 21, 2024 7:49 pm

Save a Writer .odt document from VBScript

Post by Granchio_54 »

Excuse me
How can I save a Writer .odt document from VBScript?
I need to open a Writer .odt document, enter some text, then save and close it.
I have code that does everything but does not save.
When I reopen the document, the text inside is unchanged.
Can anyone help me? Thanks
Openoffice 4.1.15
Windows 10
FJCC
Moderator
Posts: 9588
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Save a Writer .odt document from VBScript

Post by FJCC »

The method you are looking for is store(). In OpenOffice basic, you can use

Code: Select all

ThisComponent.store()
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.
Granchio_54
Posts: 4
Joined: Mon Oct 21, 2024 7:49 pm

Re: Save a Writer .odt document from VBScript

Post by Granchio_54 »

I tryed the .store() method (thanks to FJCC)
Unfortunately it doesn't work: It gives me an error.
Actually, I don't know how to make it work.
I have attached a simplification of the script I run (It can be inserted into any folder together with the document. JUST REMOVE THE .TXT EXTENTION).
(The full one does a lot more: Opens a Calc file, reads data from some cells based on a Date Range, then transfers that data into the .odt document which... should be saved but I can't.
I hope someone can help me save the modified document.
Attachments
ProgForTest.vbs.TXT
(1.83 KiB) Downloaded 111 times
OO_Writer_Document.odt
(9.92 KiB) Downloaded 137 times
Openoffice 4.1.15
Windows 10
FJCC
Moderator
Posts: 9588
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Save a Writer .odt document from VBScript

Post by FJCC »

The version of the macro below runs in OpenOffice Basic.
I changed the last argument of loadComponentFromURL because args was undefined. That argument needs to be an array and I set it to an empty array.
I changed the the line objDocument.Store() to objDocument.store(). That would not matter in OpenOffice Basic.

Code: Select all

Sub Main
Dim strFile

Dim strText
Dim strNewText

strPath = "C:/Users/fjcc/Desktop/"  'Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
strFile = "OO_Writer_Document.odt"

	Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
	Set objDesktop = objServiceManager.CreateInstance("com.sun.star.frame.Desktop")

REM ---- I had found some information about this "Dispatcher" and I tried to solve
'        but even with this I could not find the functions or methods to save the document
'	Set objDispatcher = objServiceManager.CreateInstance("com.sun.star.frame.DispatchHelper")


'load file
	Set objDocument = objDesktop.loadComponentFromURL("file:///" & strPath & strFile, "_default", 0, array())
	Set objText = objDocument.getText() ' Read the text contained in the document
	MsgBox objText.String	' Display the text
	objText.String = ""		' Clear the text in the document
	MsgBox "The 'OLD' text in the document is deleted" 

'	Set objCursor = objText.createTextCursor()
	strNewText =	"This is just a test" & vbCrlf & _
					"AABBCCDDEEFFGGHH" & Chr(13) & _
					"aabbccddeeffgghh" & vbCrlf & _
					"1122334455667788" & vbCrlf
	objText.String = strNewText		' Inserts the new text into the document
	Set objText = objDocument.getText() ' Read the NEW text contained in the document
	MsgBox	"The text in the document has changed" & Chr(13) & _
			"This is the 'NEW' Text:" & vbCrlf & vbCrlf & objText.String

	objDocument.store()	' Generates an error
'	objDesktop.Store()	' Generates an error

	objDocument.Close(True)
End Sub
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.
Granchio_54
Posts: 4
Joined: Mon Oct 21, 2024 7:49 pm

Re: Save a Writer .odt document from VBScript

Post by Granchio_54 »

VBScript is not Case Sensitive.
objDocument.Store() is the same as objDocument.store() or objDocument.STORE().
Openoffice 4.1.15
Windows 10
FJCC
Moderator
Posts: 9588
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Save a Writer .odt document from VBScript

Post by FJCC »

Since your code runs basically unchanged in OpenOffice, we know that there is nothing wrong with the API calls. What error are you seeing when you run the line objDocument.store()?
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.
Granchio_54
Posts: 4
Joined: Mon Oct 21, 2024 7:49 pm

Re: Save a Writer .odt document from VBScript

Post by Granchio_54 »

This is the error in a sort of AlertBox titeled "Windows Script Host"
Script: C:\Test\ProgForTest.ods
Riga: 46
Carattere 2
Errore: com.sun.star.task.ErrorCodeOfException:
Codice: 800A03E9
Origine: [automation bridge]
Openoffice 4.1.15
Windows 10
User avatar
floris v
Volunteer
Posts: 4593
Joined: Wed Nov 28, 2007 1:21 pm
Location: Netherlands

Re: Save a Writer .odt document from VBScript

Post by floris v »

You probably have to use \ instead of /.
LibreOffice 24.2.7.2 on Ubuntu Linux
If your problem has been solved or your question has been answered, please edit the first post in this thread and add [Solved] to the title bar.
Nederlandstalig forum
Locked