Unprotect a Section of a Table

L

Laura Ashley

How do you unprotect just a section of a form?
I'm trying to make one of the cells in my table
unprotected so that ppl can place there picture into?
My understand of the code is basic so detailed
instructions would be appreciated
Thanks inadvance
 
J

Jay Freedman

Hi, Laura,

Sorry, no can do. The minimum part of a document that can be unprotected is
a section (i.e., between section breaks), and a table can't span across a
section break.

As an alternative, you can write a macro that lets the user choose the
picture, then (without giving the user control of the document) unprotects
the document, inserts the picture, and reprotects the document.

An example of code like this is at
http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm, where the
operation between the protect and unprotect is a spellchecker run. Adapting
this code for picture insertion probably isn't going to be simple, though.
If you post back with specifics about the form, one of us could probably
make the necessary changes. Things we need to know:

- Is there only one table in the form? If not, which table (counting from
the start of the document) receives the picture?

- Which cell (counting number of rows from the top and number of columns
from the left) receives the picture?

- Is there anything in that cell (text, form field, etc.) before the picture
is inserted? If so, should it be preserved or overwritten?

- When the user selects the picture file, is there a specific folder and a
specific file extension that should be the default in the dialog?

- Should the picture be given a specific size on the page, regardless of its
size in the file?
 
J

Jonathan West

Jay Freedman said:
Hi, Laura,

Sorry, no can do. The minimum part of a document that can be unprotected is
a section (i.e., between section breaks), and a table can't span across a
section break.

Hi Jay,

In Word 2003 there is a new feature for document protection, and it is now
possible to protect any arbitrary range of text.

So the first question that needs to be asked of Laura is "Which version of
Word are you using?"
 
M

Mark Baird

If you move to Office 2003 you will be able to use the new documet protection schemes. With this version of office you can lock an entire document and then unlock down to the character, an area that an assigned person can edit. You can have multiple "editable" regions with multiple people.

Mark Baird
 
L

Laura Ashley

Hi Jay

- There is more than one table. However, its the 1st table
I want it in.

- And to make things even easier, its the first cell of
the first table.

- There is nothing in the cell. However, it would be nice
to have something for ppl to click on. Or something that
tells them how to insert a picture. The text can be
overwriten with the picture when completed.

- There is no specific file folder or file extension.
Ideally if the user could select from like a browser box.
Similiar to who you save or open files.

- The picture to should fit to the size of the cell
regarless of file size. The cell is squarish, if the
picture is a rectangle the longest side should fit to the
cell.

I really appreciate your help. I'm also going to look at
that link. However, my understanding of macro is very
basic and I'm pretty sure I won't beable to sort this out
on my own.

Thanks again, Laura

PS. Its Office 2002
 
J

Jay Freedman

Hi, Laura,

In the template for your documents, turn off protection temporarily. In the
first cell of the first table, press Ctrl+F9 to get a pair of field braces,
and type this text (all in one line) inside the braces:

MacroButton InsertPictureInTable [Double-click here to insert picture]

Press F9 to update the field, and all that will be visible is the part in
square brackets. You can format this as you like, maybe in red so it stands
out.

Then open the VBA editor (Alt+F11). In the Project Explorer window, select
the name of your template, and use the Insert > Module menu item. Copy the
macro code below, and paste it into the blank editing window. Then close the
VBA editor, protect the template for forms, and save it. When you create a
new document from the template, you can double-click the text in the cell to
start the macro.

Option Explicit

Public Sub InsertPictureInTable()
Dim oTbl As Table, oCell As Cell
Dim oRg As Range
Dim ProtType As WdProtectionType
Dim RowHt As Single, ColWd As Single
Dim PicHt As Single, PicWd As Single
Dim ScaleFactor As Single
Dim PicFile As String
Dim oPic As InlineShape
Dim dlg As Dialog

' unprotect document if necessary
ProtType = ActiveDocument.ProtectionType
If ProtType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

If ActiveDocument.Tables.Count = 0 Then
MsgBox "The table is missing!"
GoTo bye
End If

Set oTbl = ActiveDocument.Tables(1)
Set oCell = oTbl.Cell(Row:=1, Column:=1)
Set oRg = oCell.Range

' if the cell contains one or more
' fields, remove it/them
Do While oRg.Fields.Count > 0
oRg.Fields(1).Delete
Loop

' find cell size
RowHt = oCell.Height
ColWd = oCell.Width

' let user choose picture
Set dlg = Dialogs(wdDialogInsertPicture)
With dlg
If .Display = -1 Then
PicFile = .Name
Else
GoTo bye
End If
End With

' insert picture in cell
Set oPic = ActiveDocument.InlineShapes.AddPicture( _
FileName:=PicFile, LinkToFile:=False, _
SaveWithDocument:=True, Range:=oRg)

' resize picture to fit cell
With oPic
PicWd = .Width
PicHt = .Height

' find the scale factor that makes the
' picture fit without stretching the cell
ScaleFactor = Min(ColWd / PicWd, RowHt / PicHt)

' don't enlarge the picture
If ScaleFactor > 1# Then ScaleFactor = 1#

' change the size in both directions
' by the same factor
.Height = .Height * ScaleFactor
.Width = .Width * ScaleFactor
End With

bye:
' clean up
ActiveDocument.Protect Type:=ProtType, NoReset:=True
Set oPic = Nothing
Set oRg = Nothing
Set oCell = Nothing
Set oTbl = Nothing
End Sub

Private Function Min(a As Single, b As Single) As Single
If a < b Then
Min = a
Else
Min = b
End If
End Function
 
L

Laura Ashley

Hi Jay,
It worked Perfectly. Thank you so much. :)
Have a great day.
Laura
-----Original Message-----
Hi, Laura,

In the template for your documents, turn off protection temporarily. In the
first cell of the first table, press Ctrl+F9 to get a pair of field braces,
and type this text (all in one line) inside the braces:

MacroButton InsertPictureInTable [Double-click here to insert picture]

Press F9 to update the field, and all that will be visible is the part in
square brackets. You can format this as you like, maybe in red so it stands
out.

Then open the VBA editor (Alt+F11). In the Project Explorer window, select
the name of your template, and use the Insert > Module menu item. Copy the
macro code below, and paste it into the blank editing window. Then close the
VBA editor, protect the template for forms, and save it. When you create a
new document from the template, you can double-click the text in the cell to
start the macro.

Option Explicit

Public Sub InsertPictureInTable()
Dim oTbl As Table, oCell As Cell
Dim oRg As Range
Dim ProtType As WdProtectionType
Dim RowHt As Single, ColWd As Single
Dim PicHt As Single, PicWd As Single
Dim ScaleFactor As Single
Dim PicFile As String
Dim oPic As InlineShape
Dim dlg As Dialog

' unprotect document if necessary
ProtType = ActiveDocument.ProtectionType
If ProtType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

If ActiveDocument.Tables.Count = 0 Then
MsgBox "The table is missing!"
GoTo bye
End If

Set oTbl = ActiveDocument.Tables(1)
Set oCell = oTbl.Cell(Row:=1, Column:=1)
Set oRg = oCell.Range

' if the cell contains one or more
' fields, remove it/them
Do While oRg.Fields.Count > 0
oRg.Fields(1).Delete
Loop

' find cell size
RowHt = oCell.Height
ColWd = oCell.Width

' let user choose picture
Set dlg = Dialogs(wdDialogInsertPicture)
With dlg
If .Display = -1 Then
PicFile = .Name
Else
GoTo bye
End If
End With

' insert picture in cell
Set oPic = ActiveDocument.InlineShapes.AddPicture( _
FileName:=PicFile, LinkToFile:=False, _
SaveWithDocument:=True, Range:=oRg)

' resize picture to fit cell
With oPic
PicWd = .Width
PicHt = .Height

' find the scale factor that makes the
' picture fit without stretching the cell
ScaleFactor = Min(ColWd / PicWd, RowHt / PicHt)

' don't enlarge the picture
If ScaleFactor > 1# Then ScaleFactor = 1#

' change the size in both directions
' by the same factor
.Height = .Height * ScaleFactor
.Width = .Width * ScaleFactor
End With

bye:
' clean up
ActiveDocument.Protect Type:=ProtType, NoReset:=True
Set oPic = Nothing
Set oRg = Nothing
Set oCell = Nothing
Set oTbl = Nothing
End Sub

Private Function Min(a As Single, b As Single) As Single
If a < b Then
Min = a
Else
Min = b
End If
End Function

--
Regards,
Jay Freedman
Microsoft Word MVP

Laura said:
Hi Jay

- There is more than one table. However, its the 1st table
I want it in.

- And to make things even easier, its the first cell of
the first table.

- There is nothing in the cell. However, it would be nice
to have something for ppl to click on. Or something that
tells them how to insert a picture. The text can be
overwriten with the picture when completed.

- There is no specific file folder or file extension.
Ideally if the user could select from like a browser box.
Similiar to who you save or open files.

- The picture to should fit to the size of the cell
regarless of file size. The cell is squarish, if the
picture is a rectangle the longest side should fit to the
cell.

I really appreciate your help. I'm also going to look at
that link. However, my understanding of macro is very
basic and I'm pretty sure I won't beable to sort this out
on my own.

Thanks again, Laura

PS. Its Office 2002
http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.h
tm, where
 

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