Extending Word Model Object

L

Lauro

I'm writing quite a complex application in Word.

I've a lot of code that I would like to keep clear. I thougth several
time of using custom classes, but I don't know if I can achieve what I
have in mind.

For instance in a Word Document based on MyTemplate.dot I have a Table
"ActiveDocument.Tables(1)" and each Row in the table is not just a row
but has a lot of special data and code for manipulate it (for instance
in the first cell of each row there is some hidden text to identify
the row, etc.).

I would like to extend the standard Word Model Object and create my
own objects wrapping the existing one in Word.
For instance, having a new table or new row object that acts as the
standard one + same new properties and methods.
If I'll create a new object MyRow, how I can tell Word to use MyRow in
the normal Table object instead of the normal Row?

Do you have any suggestions?

All the example I found is about new control object none on extension
of build-in Word objects.

Thanks, Lauro
 
J

Jezebel

This isn't really possible the way you describe it. There's no way to get
Word to use your objects in place of its own. You might be able to achieve
something of what you want by turning the who thing around: use your classes
as the primary elements, and Word as the secondary. (Ie, instead of running
Word and having your code added-in, run your app and have Word as the
add-in.) This is hard to do with VB and pretty well impossible with VBA --
apart from anything else, VB/VBA classes don't support inheritance.

Do a Google on 'sub-classing with VB' - you might get some ideas that help.

Specifically in respect of Word tables, there are some tricky problems: the
row 'object' (it's actually a kind of collection, rather than an object in
its own right) doesn't necessarily exist at all, eg if the table is
non-uniform. Rather than hiding information in the table, where there is
always the risk that the user will delete it, you might find it easier to
set the table's ID property, and associate the ID with data stored in a
document variable.
 
J

Jean-Guy Marcil

Jezebel was telling us:
Jezebel nous racontait que :


Specifically in respect of Word tables, there are some tricky
problems: the row 'object' (it's actually a kind of collection,
rather than an object in its own right) doesn't necessarily exist at
all, eg if the table is non-uniform. Rather than hiding information
in the table, where there is always the risk that the user will

My thought exactly.
delete it, you might find it easier to set the table's ID property,
and associate the ID with data stored in a document variable.

I thought that the ID property was for HTML use only? In regular document,
it does not stick from one session to the next, unless you save as HTML....
Have I misunderstood or has Word changed this property in 2003 so that there
is a way to make it stick?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
L

Lauro

Jezebel was telling us:
Jezebel nous racontait que :


My thought exactly.


I thought that the ID property was for HTML use only? In regular document,
it does not stick from one session to the next, unless you save as HTML....
Have I misunderstood or has Word changed this property in 2003 so that there
is a way to make it stick?

Yes, ID Property is saved only in HTML in Word 2002.
That's why I was trying t find another solution.
Not a very elegant could be:
Trap every time the user try to move the selection on the first column
of my table.


*******
'A New class:
EventClassModule
'with
Public WithEvents App As Word.Application

Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Dim intInizio As Integer, intFine As Integer

intInizio = Selection.Information(wdStartOfRangeColumnNumber)
intFine = Selection.Information(wdEndOfRangeColumnNumber)

If Selection.Information(wdWithInTable) And intInizio = 1 And
intFine = 1 Then
MsgBox "You can't edit this cell!"
Selection.MoveRight wdCell, 1, wdMove
End If
End Sub

'A start up
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
 
J

Jezebel

Yes, ID doesn't stick. I have some code that does some similar work with
tables. The IDs are set up each time the document is opened.
 
J

Jean-Guy Marcil

Jezebel was telling us:
Jezebel nous racontait que :
Yes, ID doesn't stick. I have some code that does some similar work
with tables. The IDs are set up each time the document is opened.

So there is no way of knowing/tracking a specific table.
If Table ID "Number0012" is deleted in a session, then when the document is
reopened you do not know that it was deleted.
Unless,you keep track of table ID's at opening (in a DocVariable for
example) and then have all the ID's in another DocVariable when you close.
So, next time you open the document, you can compare the two, then you know
if a table is missing, or if a table was created before resetting the
opening DocVariable. Alternatively, the resetting could be done when you
closed the document.
But if Word crashes, this could all go wrong, unless you have some code to
warn you that the last session was terminated abruptly.

Is this something like this you re referring to?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jezebel

In my case it's not so fraught: each table is an integral unit, and the
information relates specifically to that table -- so it actually doesn't
matter if a table is missing. The general method is to record the
information into document variables when the table is saved (using the table
index as the identifier), then put the information back when the document is
opened.

This relies on a) the document not becoming corrupt -- which *any* method
has to assume, b) the document not being used without the macros running --
which is an acceptable assumption in my case, and c) the document not
changing while it's closed -- which seems a reasonable assumption, given b).
 

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