Avatar of PRSEXPERT
PRSEXPERT
Flag 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
XMLVB Script

Avatar of undefined
Last Comment
PRSEXPERT

8/22/2022 - Mon
Kent Dyer

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
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>
PRSEXPERT

ASKER
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>
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
PRSEXPERT

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PRSEXPERT

ASKER
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.