Link to home
Create AccountLog in
Avatar of PRSEXPERT
PRSEXPERTFlag for United States of America

asked on

Trying to change an XML file automatically with VBScript to change defaults in OpenOffice

I am trying to get this script to work, and I am now asking for assistance.

It will replace the value with value_a, but it will not tag correctly.

Please assist.

Thanks

I have attached the xml file and the vbscript is:


Dim XmlDoc, value_a, value_b, value_c
value_a = "<value>MS Office 97<"&"/value>"
value_c = value_a

Set XmlDoc = CreateObject("Msxml2.DOMDocument.4.0")
XmlDoc.async = False
If XmlDoc.load("writer.xml") Then
    XmlDoc.setProperty "SelectionLanguage", "XPath"
    XmlDoc.setProperty "SelectionNamespaces","xmlns:oor='" & XmlDoc.documentElement.namespaceURI & "'"
    Set value_b = XmlDoc.selectsinglenode("oor:data/oor:component-data[attribute::oor:name='Setup']/node[attribute::oor:name='Office']/node[attribute::oor:name='Factories']/node[attribute::oor:name='com.sun.star.text.TextDocument']/prop[attribute::oor:name='ooSetupFactoryDefaultFilter']")
        If Not value_b Is Nothing Then
            WScript.Echo "Found value " & value_b.text
            value_b.text = value_c
            strResult = XmlDoc.save("file2.xml")
        Else
            WScript.Echo "value element not found."
        End If
    Else
        WScript.Echo "Error parsing XML: " & XmlDoc.parseError.reason
End If
 writer.xml
file2.xml
Avatar of Kent Dyer
Kent Dyer
Flag of United States of America image

Running the code from the start..

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Error parsing XML: System error: -2146697210.

Changed line 19 to:
If XmlDoc.load("C:\Users\Kent\Documents\AdminScriptEditor\writer.xml") Then

Open in new window


Changed line 26 to:
strResult = XmlDoc.save("C:\Users\Kent\Documents\AdminScriptEditor\file2.xml")

Open in new window


Now, when run we see:

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Found value writer8

HTH,

Kent
Avatar of PRSEXPERT

ASKER

I actually am getting that already, but if you look in file2 where it made the modifications, you will see it messes up the formatting.

I had it working, it just will not leave the tags and only replace the value.

ie.

under the section Property that I pull:

prop[attribute::oor:name='ooSetupFactoryDefaultFilter']

<value>writer8</value>

which it outputs as 'writer8' when i query var value_b.text as you saw in my script.

it just cannot replace that value correctly no matter what I have done.

I have tried setting value_b.text ="MS Office 97" and the current value.

this is what I am hoping to achieve this value under the property that I sited above.

<value>MS Office 97</value>
Actually I took snips of the xml files before and after my script.

BEFORE:
<prop oor:name="ooSetupFactoryDefaultFilter">
  <value>writer8</value>
  </prop>
- <prop oor:name="ooSetupFactoryEmptyDocumentURL" oor:finalized="true">
  <value>private:factory/swriter</value>
  </prop>

AFTER:
  <prop oor:name="ooSetupFactoryDefaultFilter"><value>MS Office 97</value></prop>
- <prop oor:name="ooSetupFactoryEmptyDocumentURL" oor:finalized="true">
  <value>private:factory/swriter</value>
  </prop>
ASKER CERTIFIED SOLUTION
Avatar of PRSEXPERT
PRSEXPERT
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This was a pain to figure out, but this script will parse through the open office apps and change default associations.  I actually created an installer and am willing to share the knowledge with anyone that needs it.