Insert text into a table unprotected section of a protected document

M

mattchorno

Hi. I am using Word 2003 and have a document that has 3 sections. I
have the first section protected leaving the other 2 unprotected. In
one of the unprotected sections I have a table. I have a macro that
uses a loop to populates that table. Code is:

For i = 1 To UBound(CAPSID)
If i <> 1 Then '--if not on first cell in table
ActiveDocument.Tables(2).Rows.Add '--add a row to the table
End If

With ActiveDocument.Tables(2).Cell(Row:=i, Column:=1).Range
.InsertAfter Text:=CAPSID(i) '--CAPSID is an array that stores
text values
End With
Next i

However when I run the macro I get the runtime error 6124: "You are
not allowed to edit this region because document protection is in
effect". The first part of the code works (inserting a row into the
table), but it will not insert text into it. Why am I getting this
error if the table is in an unprotected section of the document? I am
able to type into the table manually, but running the code throws an
error. If I turn off all protection it works fine. I know the work
around, but I'd rather not turn off and on protection during the
macro. I don't see why it shouldn't work if the table is not in a
protected part of the document. I wrote this program a long time ago
and I don't remember it not working before. I think it may have
started not working after moving to Word 2003, but am not sure. Any
clues?
 
J

Jean-Guy Marcil

mattchorno was telling us:
mattchorno nous racontait que :
Hi. I am using Word 2003 and have a document that has 3 sections. I
have the first section protected leaving the other 2 unprotected. In
one of the unprotected sections I have a table. I have a macro that
uses a loop to populates that table. Code is:

For i = 1 To UBound(CAPSID)
If i <> 1 Then '--if not on first cell in table
ActiveDocument.Tables(2).Rows.Add '--add a row to the table
End If

With ActiveDocument.Tables(2).Cell(Row:=i, Column:=1).Range
.InsertAfter Text:=CAPSID(i) '--CAPSID is an array that stores
text values
End With
Next i

However when I run the macro I get the runtime error 6124: "You are
not allowed to edit this region because document protection is in
effect". The first part of the code works (inserting a row into the
table), but it will not insert text into it. Why am I getting this
error if the table is in an unprotected section of the document? I am
able to type into the table manually, but running the code throws an
error. If I turn off all protection it works fine. I know the work
around, but I'd rather not turn off and on protection during the
macro. I don't see why it shouldn't work if the table is not in a
protected part of the document. I wrote this program a long time ago
and I don't remember it not working before. I think it may have
started not working after moving to Word 2003, but am not sure. Any
clues?

Protected document are a weird beast.

It is probably better to unprotect them before manipulating them with code.

I am not sure why you object to doing this, it is simple and hassle-free,
no?
 
M

mattchorno

It is probably better to unprotect them before manipulating them with code.
I am not sure why you object to doing this, it is simple and hassle-free,
no?
Well, I am hoping not to do that because the macro creates a rather
large document (over 100-150 pages long) and it has calculated
fields. When you re-lock after unlocking, it updates all of the
calculated fields and takes a long time to do so in a 100-150 page
document. And not only that, I just would like to understand what's
going on...it doesn't make any sense to me why it should throw that
error.
 
J

Jean-Guy Marcil

mattchorno was telling us:
mattchorno nous racontait que :
Well, I am hoping not to do that because the macro creates a rather
large document (over 100-150 pages long) and it has calculated
fields. When you re-lock after unlocking, it updates all of the
calculated fields and takes a long time to do so in a 100-150 page
document. And not only that, I just would like to understand what's
going on...it doesn't make any sense to me why it should throw that
error.

As far as I can tell it has to do with the table being linked to the
document collection of tables, because the document is protected in part,
the collection is also locked.

So, you can use the Selection object instead, which does not refer directly
to the table,.
Try this:

With ActiveDocument.Tables(2).Cell(Row:=i, Column:=1).Range
.Select
Selection.Collapse wdCollapseStart
Selection.TypeText CAPSID(i) '--CAPSID is an array that stores
Text values
End With
 
M

mattchorno

As far as I can tell it has to do with the table being linked to the
document collection of tables, because the document is protected in part,
the collection is also locked.

So, you can use the Selection object instead, which does not refer directly
to the table,.
Try this:

    With ActiveDocument.Tables(2).Cell(Row:=i, Column:=1).Range
        .Select
        Selection.Collapse wdCollapseStart
        Selection.TypeText CAPSID(i)   '--CAPSID is an array that stores
Text values
    End With

Genius. Worked like a charm. Thank you so much!
 

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