Hyperlinks and Images in forms

M

Matt

Hello,

I need to create a form based template with:

1.) An editable field that outputs a hyperlink
2.) An editable field that outputs an image

How do I go about doing this? Do I need some special scripts, or have I
totally overlooked a simple solution?

Your help is very much appreciated!
 
J

Jay Freedman

Hello,

I need to create a form based template with:

1.) An editable field that outputs a hyperlink
2.) An editable field that outputs an image

How do I go about doing this? Do I need some special scripts, or have I
totally overlooked a simple solution?

Your help is very much appreciated!

Hi Mark,

Your terminology is confusing because it doesn't quite correspond to
anything Word does with fields. Exactly what do you mean by "outputs"?
And how should the field be "editable"? Are the hyperlinks and images
supposed to be pre-formed and stored in the template, to be retrieved
depending on the value of the field, or are they supposed to be
created "on the fly" as the user enters arbitrary text?

On the chance that it might describe what you intended, please read
the article at
http://www.word.mvps.org/FAQs/TblsFldsFms/AutoTextList.htm
 
M

Matt

Please excuse my newbieness...I seem to have gotten in over my head in
tackling this project, as I've never made a Word template before and
underestimated the complexity of making one. I do appreciate your help...

Further explanation:

I am creating templates based on a few documents. The text portions of these
documents I have converted using Text Form Fields. These fields are tabbable
and the text is editable when the template is locked. What I need to
accomplish is

adding a field that contains a hyperlink in which the text and the url can
be edited when the template is locked

and

adding a field that conains an image which can be changed when the template
is locked

I suppose this might involve the use of macros and other stuff. I've looked
all over and haven't found a solution yet.

Thank you again for attempting to understand me!

Matt
 
J

Jay Freedman

Hi Matt,

I do apologize for addressing you as Mark before -- that was from
another post I answered just before yours.

You certainly have not started with the easy end of things! Your
requests are quite difficult to satisfy, even with some fancy macro
programming.

The first difficulty is that form fields in protected forms have no
ability to contain either hyperlinks or images. You'll be forced to
program workarounds, or to completely redesign your form.

The second difficulty is that if you want to *replace* the form field
with a hyperlink or image -- as opposed to placing the hyperlink or
image *next to* the form field -- you'll no longer have a field that
you can edit. It would be a use-once form.

The page at http://word.mvps.org/FAQs/TblsFldsFms/HLinksInForms.htm
explains how to put an unchanging hyperlink into a protected form so
that it can be clicked to follow the link. At the bottom of that page
is a link to
http://word.mvps.org/FAQs/MacrosVBA/NestedFieldsWithVBA.htm, which
explains how to insert the necessary field from a macro. The macro
named "InsertHyperLinkFieldWithinMacroButtonField" in the middle of
that page does it, but only for the constant URL
http://www.mvps.org/word/. You would need to modify the macro in three
important ways: (a) use the content of the form field as the URL to
place in the hyperlink, (b) unprotect the document at the start of the
macro and reprotect it at the end, and (c) move the Selection to a
bookmark outside the original form field so the macro doesn't
overwrite it with the new hyperlink. When the macro creates the
hyperlink, it needs to recreate the bookmark to cover it, so the next
execution of the macro can find it again.

Once that's done, you can go into the form field's Properties dialog
and set the Exit Macro box to point to the macro.

The image field could be either more or less difficult, depending on
your requirements. If there is a reasonably small selection of images
from which the user is forced to choose, you could store the images in
the template as AutoText entries. The form could contain a dropdown
list of the images, and the exit macro of the dropdown would unprotect
the document, select a predefined bookmark, insert an AUTOTEXT field
containing the name of the correct AutoText entry, reapply the
bookmark, update the field, and reprotect the document.

If the form field needs to be edited by the user to the name of an
arbitrary graphic file on their PC (or elsewhere), the macro needs to
be a bit more complex. First, it needs to verify that the named file
actually exists -- that the user hasn't made a typing mistake. Then it
does the same unprotect/get bookmark, inserts an INCLUDEPICTURE field
with the file name, bookmark/reprotect.

You'll find help with some of these tasks by studying the pages at
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
(reapplying a bookmark),
http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm
(unprotecting/reprotecting), and
http://word.mvps.org/FAQs/TblsFldsFms/ValidateFFields.htm (forcing the
user back to the form field if the entry isn't valid).

If you're open to redesigning the whole form, you may actually have an
easier job by learning to design a userform (a custom dialog box).
There's a lot of information about userforms at
http://word.mvps.org/FAQs/Userforms.htm.

Whichever way you decide to go, you can get more help here. If you're
having trouble with macro code, be sure to post the relevant piece of
code as part of your question.

--
Regards,
Jay Freedman
Microsoft Word MVP

Please excuse my newbieness...I seem to have gotten in over my head in
tackling this project, as I've never made a Word template before and
underestimated the complexity of making one. I do appreciate your help...

Further explanation:

I am creating templates based on a few documents. The text portions of these
documents I have converted using Text Form Fields. These fields are tabbable
and the text is editable when the template is locked. What I need to
accomplish is

adding a field that contains a hyperlink in which the text and the url can
be edited when the template is locked

and

adding a field that conains an image which can be changed when the template
is locked

I suppose this might involve the use of macros and other stuff. I've looked
all over and haven't found a solution yet.

Thank you again for attempting to understand me!

Matt
 
M

Matt

Wow, seems like a lot of work for little gain. Is there no way to create a
macro that would just briefly unprotect the document, bring up the insert
image or insert hyperlink diaglogue, and then relock the document?

Also, if that idea is no good and if you have any knowledge of Acrobat, do
you suspect it might be easier to accomplish this using it rather than Word,
since it is more web and graphics oriented?

Thanks for all the time you must have put into your reply. I do really
appreciate your help.

Jay Freedman said:
Hi Matt,

I do apologize for addressing you as Mark before -- that was from
another post I answered just before yours.

You certainly have not started with the easy end of things! Your
requests are quite difficult to satisfy, even with some fancy macro
programming.

The first difficulty is that form fields in protected forms have no
ability to contain either hyperlinks or images. You'll be forced to
program workarounds, or to completely redesign your form.

The second difficulty is that if you want to *replace* the form field
with a hyperlink or image -- as opposed to placing the hyperlink or
image *next to* the form field -- you'll no longer have a field that
you can edit. It would be a use-once form.

The page at http://word.mvps.org/FAQs/TblsFldsFms/HLinksInForms.htm
explains how to put an unchanging hyperlink into a protected form so
that it can be clicked to follow the link. At the bottom of that page
is a link to
http://word.mvps.org/FAQs/MacrosVBA/NestedFieldsWithVBA.htm, which
explains how to insert the necessary field from a macro. The macro
named "InsertHyperLinkFieldWithinMacroButtonField" in the middle of
that page does it, but only for the constant URL
http://www.mvps.org/word/. You would need to modify the macro in three
important ways: (a) use the content of the form field as the URL to
place in the hyperlink, (b) unprotect the document at the start of the
macro and reprotect it at the end, and (c) move the Selection to a
bookmark outside the original form field so the macro doesn't
overwrite it with the new hyperlink. When the macro creates the
hyperlink, it needs to recreate the bookmark to cover it, so the next
execution of the macro can find it again.

Once that's done, you can go into the form field's Properties dialog
and set the Exit Macro box to point to the macro.

The image field could be either more or less difficult, depending on
your requirements. If there is a reasonably small selection of images
from which the user is forced to choose, you could store the images in
the template as AutoText entries. The form could contain a dropdown
list of the images, and the exit macro of the dropdown would unprotect
the document, select a predefined bookmark, insert an AUTOTEXT field
containing the name of the correct AutoText entry, reapply the
bookmark, update the field, and reprotect the document.

If the form field needs to be edited by the user to the name of an
arbitrary graphic file on their PC (or elsewhere), the macro needs to
be a bit more complex. First, it needs to verify that the named file
actually exists -- that the user hasn't made a typing mistake. Then it
does the same unprotect/get bookmark, inserts an INCLUDEPICTURE field
with the file name, bookmark/reprotect.

You'll find help with some of these tasks by studying the pages at
http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
(reapplying a bookmark),
http://word.mvps.org/FAQs/MacrosVBA/SpellcheckProtectDoc.htm
(unprotecting/reprotecting), and
http://word.mvps.org/FAQs/TblsFldsFms/ValidateFFields.htm (forcing the
user back to the form field if the entry isn't valid).

If you're open to redesigning the whole form, you may actually have an
easier job by learning to design a userform (a custom dialog box).
There's a lot of information about userforms at
http://word.mvps.org/FAQs/Userforms.htm.

Whichever way you decide to go, you can get more help here. If you're
having trouble with macro code, be sure to post the relevant piece of
code as part of your question.

--
Regards,
Jay Freedman
Microsoft Word MVP

Please excuse my newbieness...I seem to have gotten in over my head in
tackling this project, as I've never made a Word template before and
underestimated the complexity of making one. I do appreciate your help...

Further explanation:

I am creating templates based on a few documents. The text portions of these
documents I have converted using Text Form Fields. These fields are tabbable
and the text is editable when the template is locked. What I need to
accomplish is

adding a field that contains a hyperlink in which the text and the url can
be edited when the template is locked

and

adding a field that conains an image which can be changed when the template
is locked

I suppose this might involve the use of macros and other stuff. I've looked
all over and haven't found a solution yet.

Thank you again for attempting to understand me!

Matt
 
J

Jay Freedman

Hi Matt,

Yes, it's possible to create a macro like that.

To make the form reusable, you still need to set things up so the
picture is inserted somewhere other than as a replacement for the
field that invokes the macro. In what follows, I'll assume that your
form contains a one-row-by-two-column table (the table borders can be
turned off so it isn't visible). Put a macrobutton field in the left
cell of the table, with this code:

{macrobutton AddPicture Insert picture}

The field refers to this macro, which goes in the same template:

Public Sub AddPicture()
Dim strFile As String
Dim rgDest As Range

If Not Selection.Information(wdWithInTable) Then
Exit Sub
End If

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

With Dialogs(wdDialogInsertPicture)
If .Display Then
strFile = WordBasic.FileNameInfo$(.Name, 1)
Else
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True
Exit Sub
End If
End With

Set rgDest = Selection.Tables(1).Cell(1, 2).Range
rgDest.MoveEnd Unit:=wdCharacter, Count:=-1
rgDest.Text = ""

ActiveDocument.InlineShapes.AddPicture _
FileName:=strFile, LinkToFile:=False, _
SaveWithDocument:=True, Range:=rgDest

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True

Set rgDest = Nothing
End Sub

It unprotects the document and displays the Insert Picture dialog. If
the user cancels the dialog, the document is reprotected and the macro
quits. If a file was selected, it's inserted in the right cell of the
table and the document is reprotected. Running the macro again will
replace any existing picture with the new selection.

If you don't want the user to have to double-click the macrobutton
field, you can put this line in an AutoNew macro and an AutoOpen
macro:

Options.ButtonFieldClicks = 1

This solution isn't perfect. If the selected picture is big enough,
the resizing of the table cell will squeeze the macrobutton field, and
when it tries to wrap to a second line it will display an error
message. The cure for this (more code!) would be to check the width of
the inserted picture, and if it's more than the allowable amount then
resize the picture.

Something similar could be done with the Insert Hyperlink dialog.
 
M

Matt

You are awesome! That works perfectly. The only think I changed was the cell
arrangement in order to keep the alignment centered. I created a 1 column, 2
row table, and changed
Set rgDest = Selection.Tables(1).Cell(1, 2).Range

To

Set rgDest = Selection.Tables(1).Cell(2, 1).Range

I also tried tweaking it for the hyperlink and got the hyperlink dialog to
come up, but I get an error message at this line:

strFile = WordBasic.FileNameInfo$(.Name, 1)

I'm not familar enough with VBA to tweak the rest...
 
J

Jay Freedman

Hi Matt,

It isn't quite a matter of familiarity with VBA -- it's more a matter
of Microsoft having screwed up the Insert Hyperlink dialog so it's
barely usable from VBA.

In the case of the Insert Picture dialog, you can use the .Display
method to put up the dialog and let the user choose a file, but not
actually insert the picture. Then the macro can grab the .Name
parameter of the dialog, which contains the filename, and it can play
games with the location and formatting of the picture at will.

The Hyperlink dialog doesn't work that way. Regardless of whether you
use the .Display or .Show method, the dialog *always* inserts the
chosen hyperlink as soon as the user clicks the OK button, and it does
so at the cursor location -- the macro doesn't get a chance to do
anything with it. The Hyperlink dialog doesn't even have any
parameters the macro can get at, for the URL, display text, and other
bits associated with the link.

This calls for a slightly different approach: unprotect, move the
cursor to the desired location, show the dialog (which inserts the
hyperlink), add the necessary MacroButton field surrounding the
hyperlink (see
http://word.mvps.org/faqs/tblsfldsfms/HLinksInForms.htm), and
reprotect. You also need a separate macro to launch the hyperlink when
it's clicked. Here's working code:

Public Sub AddHyperlink()
If Not Selection.Information(wdWithInTable) Then
Exit Sub
End If

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

With Selection
.Tables(1).Cell(2, 1).Range.Select
.MoveEnd Unit:=wdCharacter, Count:=-1
.Text = "macrobutton FollowLink "
.Collapse wdCollapseEnd
End With

Dialogs(wdDialogInsertHyperlink).Show

With Selection
.Tables(1).Cell(2, 1).Range.Select
.MoveEnd Unit:=wdCharacter, Count:=-1
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
End With

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End Sub

Sub FollowLink()
Selection.Hyperlinks(1).Follow
End Sub
 
M

Matt

Awesome. You are the best. Only one issue remains...since the hyperlink and
image are not replacing the macrobutton text, the text stays there after the
macro is run. Is there any way to add something to the macros which will hide
the text after they are completed?
 
J

Jonathan West

Hi matt,

After the first With Selection line, add the following

.Delete

That will delete the macrobutton which was clicked to run the macro.
 
J

Jay Freedman

That's fine, but I thought part of the original request was to be able
to change the picture or hyperlink multiple times in the same
document; that's why I left the macrobutton in place.
 
M

Matt

I implemented the .delete function and it worked fine. It would be nice to be
able to run these macros again in case of a mistake though. Is there any way
to simply make the macrobutton text invisible after it's run? Should I just
change the color to white so the button is sort of invisible (the user would
have to know that it's there beforehand) or is there a simple VBA function
similar to .delete that can either make it invisible or change the color to
white?

Thanks again!
 
S

slapana

How do you make the Insert picture button disappear or turn white? There's
no With Selection line in the VBA for inserting pictures.

Thanks!
Sharon
 
S

slapana

Also, my form is actually mulitple pages and I need to allow the user to
insert a different image on each page. How do I alter the VBA to allow this?
 

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