Automating import process in a protected document

B

Bill Pearson

A little background...We have a client, an endodontist, for whom I have
created a protected form that allows her staff to fill-in a few fields and
fill in some check boxes. In the unprotected section of the document, they
can paste the digital X-ray photo for the dentist's records. This all gets
printed out on photo stock and sent to the dentist.

Now she says that the cutting and pasting and resizing of the photos is too
much work for her staff (!) and wants to automate it. They have both Word
2003 and Word 2007.

I think it might be able to be done in Visual Basic, but that's not my
forte. The X-ray program can output to the same folder the document is in
with the same file name each time. All I have to do is get it imported into
the unprotected section of the form and resize it.

Am I right in thinking that I can do this with VB? Any ideas?

Thanks a lot.

And if you need any Web help, come on over to the Expression Web forum;
we'll treat you right.

Bill
 
B

Bill Pearson

So cool. Thank you, Jay.

One or two questions (it's late, and I haven't examined the code yet; I'll
do it tomorrow).

1. If I put this in a protected area, am I correct in believing that this VB
script turns off protection and then turns it back on as part of the process
(I've done that in Excel protected cells for inserting new rows)?

2. The X-rays will be either landscape or portrait. When the script resizes
the picture to the maximum width and height that I set early on, will it fill
the area or will it keep the aspect ratio?

This is such a help, thank you (and I'll check out your link, too).

Take care.
Bill Pearson

Jay Freedman said:
Hi Bill,

You're right, VBA can do it.

If you have the flexibility, I'd suggest putting the location for pasting the
picture into a protected section rather than the unprotected section, for one
reason: You need a specific range to put the picture into, and the simplest way
to get that range is to have a predefined bookmark in the template at the right
spot. But bookmarks are easy to overwrite, delete, or otherwise screw up because
people don't usually have them displayed. If it's in a protected area where they
can't touch it, it's safer. The alternative is to tell the macro to insert the
picture wherever the cursor is when they click a toolbar button, but then you
have to check that it's in the right section.

Anyway, the code to let them choose a picture file and insert it at a bookmark
named "picHere" would look something like this. If the file name is always the
same, you can get rid of the dialog and just set up the path+filename from known
strings.

Public Sub FormInsertPicture()
' change these numbers to the maximum width and height
' (in inches) to make the inserted pictures
Const PicWidth = 1.9
Const PicHeight = 2.25

Dim PicRg As Range
Dim Photo As InlineShape
Dim RatioW As Single, RatioH As Single, RatioUse As Single
Dim FName As String

If Not .Bookmarks.Exists("picHere") Then GoTo BadRange

If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=FormPassword
End If

' standard Insert > Picture > From File dialog
' [Note: If you try to call the dialog before you
' unprotect the document, it doesn't work.]
With Dialogs(wdDialogInsertPicture)
If .Display <> -1 Then GoTo Canceled

' get chosen file's full path and file name
' (see http://word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm)
FName = WordBasic.FileNameInfo$(.Name, 1)
End With

' create range for picture location
Set PicRg = .Bookmarks("picHere").Range

' if a picture is already there, delete it
Do While PicRg.InlineShapes.Count > 0
PicRg.InlineShapes(1).Delete
Loop

' insert the picture
Set Photo = .InlineShapes.AddPicture(FileName:=FName, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=PicRg)
With Photo
RatioW = CSng(InchesToPoints(PicWidth)) / .Width
RatioH = CSng(InchesToPoints(PicHeight)) / .Height

' choose the smaller ratio
If RatioW < RatioH Then
RatioUse = RatioW
Else
RatioUse = RatioH
End If

' size the picture to fit the cell
.Height = .Height * RatioUse
.Width = .Width * RatioUse
End With

' re-add the bookmark so it can be reused
.Bookmarks.Add Name:=LocName, Range:=Photo.Range

Canceled:
.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=FormPassword
End With

Exit Sub

BadBtn:
MsgBox "The MacroButton field's bookmark is missing or incorrect", _
, "Error"
Exit Sub
BadRange:
MsgBox "The picture location bookmark is missing or incorrect", _
, "Error"
End Sub


This is swiped nearly intact from my Form_Picture.dot template, which you can
download from http://jay-freedman.info/form_picture.zip.


A little background...We have a client, an endodontist, for whom I have
created a protected form that allows her staff to fill-in a few fields and
fill in some check boxes. In the unprotected section of the document, they
can paste the digital X-ray photo for the dentist's records. This all gets
printed out on photo stock and sent to the dentist.

Now she says that the cutting and pasting and resizing of the photos is too
much work for her staff (!) and wants to automate it. They have both Word
2003 and Word 2007.

I think it might be able to be done in Visual Basic, but that's not my
forte. The X-ray program can output to the same folder the document is in
with the same file name each time. All I have to do is get it imported into
the unprotected section of the form and resize it.

Am I right in thinking that I can do this with VB? Any ideas?

Thanks a lot.

And if you need any Web help, come on over to the Expression Web forum;
we'll treat you right.

Bill

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
J

Jay Freedman

In line...

So cool. Thank you, Jay.

One or two questions (it's late, and I haven't examined the code yet; I'll
do it tomorrow).

1. If I put this in a protected area, am I correct in believing that this VB
script turns off protection and then turns it back on as part of the process
(I've done that in Excel protected cells for inserting new rows)?

That's correct.

I just noticed that the code I copied includes the Password argument in the
..Unprotect and .Protect statements, but I forgot to copy the declaration and
setting of the variable FormPassword. I assume you know what to do with that, or
you can look at the code in the form_picture.dot template.
2. The X-rays will be either landscape or portrait. When the script resizes
the picture to the maximum width and height that I set early on, will it fill
the area or will it keep the aspect ratio?

It will keep the aspect ratio. That's the purpose of the section of code between
"With Photo" and "End With".
This is such a help, thank you (and I'll check out your link, too).

Take care.
Bill Pearson

Jay Freedman said:
Hi Bill,

You're right, VBA can do it.

If you have the flexibility, I'd suggest putting the location for pasting the
picture into a protected section rather than the unprotected section, for one
reason: You need a specific range to put the picture into, and the simplest way
to get that range is to have a predefined bookmark in the template at the right
spot. But bookmarks are easy to overwrite, delete, or otherwise screw up because
people don't usually have them displayed. If it's in a protected area where they
can't touch it, it's safer. The alternative is to tell the macro to insert the
picture wherever the cursor is when they click a toolbar button, but then you
have to check that it's in the right section.

Anyway, the code to let them choose a picture file and insert it at a bookmark
named "picHere" would look something like this. If the file name is always the
same, you can get rid of the dialog and just set up the path+filename from known
strings.

Public Sub FormInsertPicture()
' change these numbers to the maximum width and height
' (in inches) to make the inserted pictures
Const PicWidth = 1.9
Const PicHeight = 2.25

Dim PicRg As Range
Dim Photo As InlineShape
Dim RatioW As Single, RatioH As Single, RatioUse As Single
Dim FName As String

If Not .Bookmarks.Exists("picHere") Then GoTo BadRange

If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=FormPassword
End If

' standard Insert > Picture > From File dialog
' [Note: If you try to call the dialog before you
' unprotect the document, it doesn't work.]
With Dialogs(wdDialogInsertPicture)
If .Display <> -1 Then GoTo Canceled

' get chosen file's full path and file name
' (see http://word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm)
FName = WordBasic.FileNameInfo$(.Name, 1)
End With

' create range for picture location
Set PicRg = .Bookmarks("picHere").Range

' if a picture is already there, delete it
Do While PicRg.InlineShapes.Count > 0
PicRg.InlineShapes(1).Delete
Loop

' insert the picture
Set Photo = .InlineShapes.AddPicture(FileName:=FName, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=PicRg)
With Photo
RatioW = CSng(InchesToPoints(PicWidth)) / .Width
RatioH = CSng(InchesToPoints(PicHeight)) / .Height

' choose the smaller ratio
If RatioW < RatioH Then
RatioUse = RatioW
Else
RatioUse = RatioH
End If

' size the picture to fit the cell
.Height = .Height * RatioUse
.Width = .Width * RatioUse
End With

' re-add the bookmark so it can be reused
.Bookmarks.Add Name:=LocName, Range:=Photo.Range

Canceled:
.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=FormPassword
End With

Exit Sub

BadBtn:
MsgBox "The MacroButton field's bookmark is missing or incorrect", _
, "Error"
Exit Sub
BadRange:
MsgBox "The picture location bookmark is missing or incorrect", _
, "Error"
End Sub


This is swiped nearly intact from my Form_Picture.dot template, which you can
download from http://jay-freedman.info/form_picture.zip.


A little background...We have a client, an endodontist, for whom I have
created a protected form that allows her staff to fill-in a few fields and
fill in some check boxes. In the unprotected section of the document, they
can paste the digital X-ray photo for the dentist's records. This all gets
printed out on photo stock and sent to the dentist.

Now she says that the cutting and pasting and resizing of the photos is too
much work for her staff (!) and wants to automate it. They have both Word
2003 and Word 2007.

I think it might be able to be done in Visual Basic, but that's not my
forte. The X-ray program can output to the same folder the document is in
with the same file name each time. All I have to do is get it imported into
the unprotected section of the form and resize it.

Am I right in thinking that I can do this with VB? Any ideas?

Thanks a lot.

And if you need any Web help, come on over to the Expression Web forum;
we'll treat you right.

Bill

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 

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