Durability of objects

B

Bear

I've managed to confuse myself.

I open a document and set objTable to the first table in the document. Now I
close the document, but I'm still able to access objTable and its contents.
How can this be?

I thought setting the object to some part of the document meant that the
object was pointing to the same area of memory used to hold a range of the
document. Am I wrong?

Is it only by luck that I'm able to access that area of memory with the
document closed? Is what I'm doing bad practice?

Would it be better to load the table into an array?

Confused,

Bear
 
J

Jean-Guy Marcil

Bear was telling us:
Bear nous racontait que :
I've managed to confuse myself.

I open a document and set objTable to the first table in the
document. Now I close the document, but I'm still able to access
objTable and its contents. How can this be?

What did you do exactly?
How did you create the object?
How are you accessing it later?
How/when did you close your table document?

Why don't you post your code?

For example, the following will not work:

Dim objTable As Table

Set objTable = Documents("Document1").Tables(1)

Documents("Document1").Close wdDoNotSaveChanges

MsgBox objTable.Cell(1, 1).Range.Text

So, what *did* you do?

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

Bear

Jean-Guy and all:

Here are the relevant details.



[Declarations]

Public tblVariables As Table
Public TRows As Integer
Public TCols As Integer



EditionLoad

' Opens the document and loads the table

Dim docEditionData As Document

Dim rngCell As Range
Dim strCell As String

Dim R As Integer
Dim C As Integer

' Load the edition variables table

Set docEditionData = Documents.Open( _
FileName:=ActiveDocument.CustomDocumentProperties("Edition Data").Value, _
revert:=False, _
Visible:=False)

If docEditionData.Tables.Count = 0 Then
Err.Number = 1001
GoTo Error
End If

Set tblVariables = docEditionData.Tables(1)

' Verify that the first table is a valid
' edition variables table

Set rngCell = tblVariables.Cell(1, 1).Range
' Omit the end of cell marker
rngCell.End = rngCell.End - 1
strCell = rngCell.Text

If strCell <> "Edition" Then
Err.Number = 1002
GoTo Error
End If

' Get the number of rows and columns in the table

TRows = tblVariables.Rows.Count
TCols = tblVariables.Columns.Count

' Load the Show list

Set ctrList = ActiveDocument.CommandBars("DFC Editions").Controls("Show")

For C = 2 To TCols
Set rngCell = tblVariables.Cell(1, C).Range
rngCell.End = rngCell.End - 1
strCell = rngCell.Text
If strCell = "" Then Exit For
ctrList.AddItem strCell
Next C
ctrList.AddItem "All editions"
ctrList.ListIndex = TCols
ctrList.OnAction = "EditionShow"

Done:

docEditionData.Close SaveChanges:=wdDoNotSaveChanges
Set rngCell = Nothing

End Sub



Sub EditionShow()

' Show the selected edition by setting the
' document variables and changing the hidden
' property for the character styles.

Dim rngCell As Range

Dim R As Integer
Dim C As Integer
Dim I As Integer

[deleted code]

' Set the document variables

' Disable On Error to handle adding variables
' that are already there. The first column of the
' variable table is the variable name, so edition
' columns are one greater than edition numbers.

On Error Resume Next
For R = 1 To TRows
Set rngCell = tblVariables.Cell(R, 1).Range
' Omit the end of cell marker
rngCell.End = rngCell.End - 1
strVariableName = rngCell.Text
ActiveDocument.Variables.Add Name:=strVariableName
Set rngCell = tblVariables.Cell(R, intEditionNumber + 1).Range
' Omit the end of cell marker
rngCell.End = rngCell.End - 1
strVariableValue = rngCell.Text
ActiveDocument.Variables(strVariableName).Value = strVariableValue
Next R
On Error GoTo Error

' Update the variable fields

For Each objField In ActiveDocument.Fields
If objField.Type = wdFieldDocVariable Then objField.Update
Next objField

Done:

End Sub
 
J

Jean-Guy Marcil

Bear was telling us:
Bear nous racontait que :
Jean-Guy and all:

Here are the relevant details.

I Guess

EditionLoad

should have been

Sub EditionLoad()

No?

I see an End Sub but not Sub line.

Also, You have in both subs:

GoTo Error

But no Error label, just a Done label.

From your first post, I have to guess that you experience the persistence of
the table object when running the second sub after the first one.

Personally, I do not see such a persistence.
After you run the first sub, if you ru the second one right away, you will
notice that

Set rngCell = tblVariables.Cell(R, 1).Range

always equals Nothing.

But the code runs because tblVariables is a global object.


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

Bear

Jean-Guy:

Please ignore any errors I introduced when editing the code I copied. The
basic situation is:

- tblVariables is declared as a global table object
- EditionLoad opens a document, sets the table object, then closes the
document
- EditionShow uses the table object

Since your reply, I've discovered that after closing the document, the table
object is nothing, as you've said.

The persistence I first was when the table was in an attached template. I
opened the template as a document, set the object, then closed the document.
I guess the table object persisted because the template was still attached.
Strange.

In any case, I'm betting it would be better to load a global array with the
table data. The array would be sure to persist until my code destroyed it.

Is that right?

Bear
 
J

Jean-Guy Marcil

Bear was telling us:
Bear nous racontait que :
Jean-Guy:

Please ignore any errors I introduced when editing the code I copied.
The basic situation is:

- tblVariables is declared as a global table object
- EditionLoad opens a document, sets the table object, then closes the
document
- EditionShow uses the table object

Since your reply, I've discovered that after closing the document,
the table object is nothing, as you've said.

The persistence I first was when the table was in an attached
template. I opened the template as a document, set the object, then
closed the document. I guess the table object persisted because the
template was still attached. Strange.

In any case, I'm betting it would be better to load a global array
with the table data. The array would be sure to persist until my code
destroyed it.

Is that right?

If what you are after is the Table Data (not formatting information), then
yes, you could do it that way.

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

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