Setting up protected document that can insert picture (photo ID template)

C

Craig

Cannot find a way to insert picture from file with
document protected. We have an inhouse design department
and are attempting to create Photo ID templates that a
user can insert pictures and text with the document
protected. The text works great but to save my life I
cannot figure out how to insert a picture while the
document is protected.
 
J

Jay Freedman

Hi Craig

Word doesn't have a form field that can hold a picture. There is a
workaround, though. With a macro you can unprotect the document momentarily,
insert the picture, and reprotect the document.

First copy/paste this macro into a module of the template (see
http://www.gmayor.com/installing_macro.htm):

Public Sub ProtectedInsertPicture()
Dim ilsPicture As InlineShape
Dim strFileName As String
Dim sngRatio As Single
Const max_width = 216 ' = 3 inches (in points)

' temporarily unprotect
ActiveDocument.Unprotect

' show Insert Picture dialog
With Dialogs(wdDialogInsertPicture)
If .Display = 0 Then Exit Sub
strFileName = .Name
End With

' remove macrobutton
Selection.Delete

Set ilsPicture = ActiveDocument.InlineShapes _
.AddPicture( _
FileName:=strFileName, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Range:=Selection.Range)

' limit size of picture to max_width (optional)
With ilsPicture
If .Width > max_width Then
sngRatio = CSng(max_width) / .Width
.Width = max_width
.Height = .Height * sngRatio
End If
End With

' reprotect, keeping form field contents intact
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End Sub

Then insert a MacroButton field at each place where the user should be able
to insert a picture, using the field code

{ MacroButton ProtectedInsertPicture Double-click to add picture }

You can use font, border, and shading formatting to make the "Double-click"
part look like a rectangula button. Press Ctrl+A and then F9 to update the
fields. Protect and save the template.

When the user creates a new form from the template and fills it in, s/he can
double-click the "button" even though it's in a protected area of the
document. The macro will unprotect, pop up the Insert > Picture > From File
dialog, insert the picture (replacing the macrobutton), and reprotect. The
optional section of the macro will resize the picture if it's wider than the
number of points you assign to max_width -- if you don't need that, just
delete the section of code between With ilsPicture and End With.
 
J

Jay Freedman

Oops, missed a bit. If the user cancels out of the Insert Picture dialog,
the macro leaves the document unprotected. This fixes it:

' show Insert Picture dialog
With Dialogs(wdDialogInsertPicture)
If .Display = 0 Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True
Exit Sub
End If
strFileName = .Name
End With
 
C

Craig

Amazing workaround! Thank you very much for taking the
time to help - It works wonderfully. . I have been
struggling with this issue for a while and this is a
great solution.

Is there any way to give the user a chance to re-insert a
picture (in the case that they select the wrong file or
just need to modify it.

THANK YOU!!!

Craig
 
J

Jay Freedman

Hi Craig

Replacing the picture gets a bit fiddly, especially as the macrobutton
isn't there any more to rerun the macro. I'll give that one a bit more
thought. It should be possible to make the picture replace just the
display text inside the macro code, so the picture itself becomes the
"button" for next time. I need to play with it to make sure it doesn't
head off into Never-Never-Land.

What version(s) of Word does this have to work with? It may make a
difference.

In the meantime, you could (a) use Tools > Unprotect Document, (b)
manually delete the old picture and insert the new one, and (c)
reprotect. In most versions of Word you have to use a macro to
reprotect, because using Tools > Protect Document from the menu will
clear all the text form fields. See
http://word.mvps.org/FAQs/MacrosVBA/TurnFmFlfResetOff.htm for that.
 
S

skylark_za

Hi

I am unable to run this macro 100% correctly. It seems to work if I
double click the macro button when the document is unprotected but when
the document is protected, the macro no longer functions at all when
double clicked. The cursor just jumps to the next text form field. Am I
doing something wrong when applying this macro or when I am executing
it?

I copied and added you macro as it appears below.

Public Sub ProtectedInsertPicture()
Dim ilsPicture As InlineShape
Dim strFileName As String
Dim sngRatio As Single
Const max_width = 216 ' = 3 inches (in points)

' temporarily unprotect
ActiveDocument.Unprotect

' show Insert Picture dialog
With Dialogs(wdDialogInsertPicture)
If .Display = 0 Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True
Exit Sub
End If
strFileName = .Name
End With

' remove macrobutton
Selection.Delete

Set ilsPicture = ActiveDocument.InlineShapes _
AddPicture( _
FileName:=strFileName, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Range:=Selection.Range)

' limit size of picture to max_width (optional)
With ilsPicture
If .Width > max_width Then
sngRatio = CSng(max_width) / .Width
Width = max_width
Height = .Height * sngRatio
End If
End With

' reprotect, keeping form field contents intact
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End Sub


Regards

Jason
 
Top