inserting custom tags around selection

J

Jan Kucera

Hi all,
I want to mark a fragment of the document to be able to populate it with
data. I'm afraid I have no chance in standard word document, have I? But I
have a little hope that I can do that in WordML format. Here is what I want
to do, for simplicity in HTML:

<table>
<tr><td>Head1</td><td>Head2</td></tr>
<tr><td>{dataFromHead1}</td><td>{dataFromHead2}</td></tr>
</table>

I expect that the user select the second row in the table (with {data...}
text) and press a MARK button, which results in saving this document:

<table>
<tr><td>Head1</td><td>Head2</td></tr>
<MARKED
source="MySource"><tr><td>{dataFromHead1}</td><td>{dataFromHead2}</td></tr></MARKED>
</table>

And as you expect, I would later process the file like this:

<table>
<tr><td>Head1</td><td>Head2</td></tr>
<tr><td>Data11</td><td>Data21</td></tr>
<tr><td>Data12</td><td>Data22</td></tr>
<tr><td>Data13</td><td>Data23</td></tr>
</table>

Is there any way how to get the XML node of selection and modify it
accordingly or any other idea?

Thank you,
Jan
 
J

Jan Kucera

Well, I've discovered Selection.XML, however this contains whole document
xml containg only the selection. I am afraid I won't be able to match it to
the actual selection and change it... :-(
 
J

Jan Kucera

And if I try Selection.InsertXML Selection.XML when a row in table is
selected, it gets nested into the first cell...
 
D

Doug Robbins - Word MVP

I am not up to speed on HTML, but in case it helps, the following vba code
will insert a table into a document and populate it with data from a Table
in an Access Database:

Dim myDataBase As Database

Dim myActiveRecord As Recordset

Dim i As Long

Dim dtable As Table, drow As Row

'Open a database

Set myDataBase = OpenDatabase("c:\Access\Procurement Plan.mdb")

'Access the first record from a particular table

Set myActiveRecord = myDataBase.OpenRecordset("Currencies",
dbOpenForwardOnly)

'Add a table to the document with one row and as many fields as there are in
the database table

Set dtable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1,
numcolumns:=myActiveRecord.Fields.Count)

Set drow = dtable.Rows(1)

'Loop through all the records in the table until the end-of-file marker is
reached

Do While Not myActiveRecord.EOF

'Populate the cells in the Word table with the data from the current
record

For i = 1 To myActiveRecord.Fields.Count

drow.Cells(i).Range.Text = myActiveRecord.Fields(i - 1)

Next i

'Add a new row to the Word table and access the next record

Set drow = dtable.Rows.Add

myActiveRecord.MoveNext

Loop

'The last row will be empty, so delete it

drow.Delete

'Then close the database

myActiveRecord.Close

myDataBase.Close

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

Peter Jamieson

You could apply a bookmark with a specially constructed name (MARKED001
etc.) to the selection then look for w:type="Word.Bookmark.Start" and so on.

Peter Jamieson
 
J

Jan Kucera

Hi Doug,
thank you for your code. I used HTML just for illustartion what I need to
do in Word XML. I will do the population on the server side in C# and the
selection don't have to be a table row, it could be whole document fragment,
just as templates in server controls. Bookmarks seem to be the most suitable
idea.
thanks for help,
Jan
 
J

Jan Kucera

Yes, I found that after several experiments later. It seems this is the best
what I can get. However, the bookmark start is just before the text and
bookmark end after the paragraph or similar. That means, that I have in the
xml something like that:
<table>
<tr>
<td>
<bookmark start/>
mytext
</td>
</tr>
<bookmark end/>
</table>

(after selecting row in a table). I unfortunately don't know how this is
constructed, but for case somebody knows, is it correct to find the first
element before the bookmark.start that is the same depth as the bookmark.end
and get the xml between them?
As I don't have any possibility to save my parameters in the bookmark tag
attributes, I will have to do that in document variables. Are there any
restrictions on the character set that can be used?

And a little question more, what is the option "Hidden bookmarks" in the
bookmark window for? Can I create hidden bookmarks? (How?)

Thank you,
Jan
 
P

Peter Jamieson

I think you face two problems:
a. a specification problem: what is the user allowed to mark, and what do
their marks mean?
b. a "find the right XML to replicate" problem.

As far as (a) is concerned, are they /only/ going to be allowed to bookmark
the whole first row of a table? Or might they be akllowed to bookmark some
of the cells (in which case maybe you would only fill in the data for those
cells in the rows you add from the database). Or what? If it's
always"whatever they mark within the table, it always means the same thing"
then things are probably easier and of course you may even get to choose
exactly where your bookmark is placed.

As far as (b) is concerned, I don't know for sure what you need to look for,
but personally I would start by looking for either <w:tr></w:tr> pairs
As I don't have any possibility to save my parameters in the bookmark tag
attributes, I will have to do that in document variables. Are there any
restrictions on the character set that can be used?

As far as I know, you can use any Unicode characters in the string but
that's about all I know.

Peter Jamieson
 
J

Jan Kucera

Hi Peter!
Thank you for your answers.

For your questions. The user is unfortunately allowed and has to be allowed
to mark anything he would like to replicate. This can be a paragraph, an
image, whole table, or just piece of text in one cell. As long as the
specification is mentioned I am sure that I should not replicate any AML
elements. So the best thing would be to search the replicated string for aml
ends and remove all corresponding starts in the document and vice versa; or,
to recompute all the replicated aml id's. I have very very little time so I
just assume there are no other amls in the replicated xml.
I made a working example using the method I had written above (replication
starts at the first element the same depth as bookmark's end) and it seems
it does the work well. I can imagine in which situations this fails (for
example when selecting the page boundary, cell to cell including row
boundary etc.), but..I just believe the author does not want anything crazy.

If you believe complex xml checking should be made on my side please let me
know and also how could I find out wheter the xml is word-valid.
Thanks,
Jan
 
P

Peter Jamieson

Hi Jan,

You are already way ahead of me on this one (just as I expected!)

Peter Jamieson
 
J

Jan Kucera

Peter Jamieson said:
You are already way ahead of me on this one (just as I expected!)

No worries, that's just because I'm trying to make it working. I have lots
and lots things to learn to be as all you guys! ;-)

Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top