Pages: 1
Print
Author Topic: Using the MoveToElement method to set attribute values  (Read 986 times)
bjorn
Member

Posts: 26


« on: October 09, 2015, 01:48:46 AM »

Hi.

I'm trying to set an attribute on a specific element.

The way I want to do this, is to iterate through the xml-elements and set the attribute.


What I got so fare is that I could use the code beneath to actually set the attribute:

Code:
var rng = ActiveDocument.Range;
rng.MoveToDocumentStart();
if(rng.MoveToElement("Section")) {
rng.ContainerAttribute("numbering") = "on"

With this code it just set the first section elements attribute to "on". After some head-scratching here
I can't figure this out.


How do I apply this code to iterate through all the sections elements?
Or is there some other way to set this attribute?


I could set the attribute as default "on" in the DTD, but I want the value to be stored in the xml-file.


Thanks!




BR,
Bjorn
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2469



WWW
« Reply #1 on: October 09, 2015, 02:02:32 PM »

Something like this?

Code:
var rng = ActiveDocument.Range;
rng.MoveToDocumentStart();
while (rng.MoveToElement("Section")) {
rng.ContainerAttribute("numbering") = "on";
}

The verbose version (with JScript comments):
Code:
//create a Range object named rng
//this is created at the user's current selection in the document
var rng = ActiveDocument.Range;

//move the rng to the start of the document
rng.MoveToDocumentStart();

//repeatedly move to the next element named "Section" until that fails
//and each time that is successful do whatever is inside the loop's {}
while (rng.MoveToElement("Section")) {
//set the value for the "numbering" attribute inside the current rng's container to "on"
rng.ContainerAttribute("numbering") = "on";
}
Logged
Pages: 1
Print
Jump to:  

email us