Need Help With Hyperlink Macro

S

Strong Eagle

Hello All,

I have created a macro which uses the insert picture dialog and then
automatically creates a hyperlink for the picture using the name taken from
the insert picture dialog box.

The problem is that I can only insert pictures at the end of the document.
If I attempt to insert a picture using the macro between already inserted
pictures, the hyperlink is added at the end of the hyperlink list and then
the two lists are out of order.

In other words, I cannot figure out how to search for the correct item in
the hyperlink list so that I can insert in the middle of the list.

The code I am using is posted below (VB). Would much appreciate assistance
in making this more robust... ability to insert a picture into the middle of
already inserted pictures, change a picture, or delete an inserted picture.

Many thanks.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2/27/2005 by Wayne Herbert
'
Dim PicDlg As Dialog
Dim i As Integer
Dim FName As String
Dim Shape1 As InlineShape
Set fs = CreateObject("Scripting.FileSystemObject")
Set PicDlg = Dialogs(wdDialogInsertPicture)
If PicDlg.Show = -1 Then
' get the index of last picture added and select it
i = ActiveDocument.InlineShapes.Count
ActiveDocument.InlineShapes.Item(i).Select
' extract the filename from full path
FName = fs.GetFileName(PicDlg.Name)
' set a hyperlink with name and selection
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=FName, _
SubAddress:=""
End If
 
D

Doug Robbins - Word MVP

The following lines of code are selecting the last picture in the document
as i is being set to the count of the pictures.

i = ActiveDocument.InlineShapes.Count
ActiveDocument.InlineShapes.Item(i).Select

Use the following instead:

Dim FName As String
With Dialogs(wdDialogInsertPicture)
If .Display Then
FName = WordBasic.FilenameInfo$(.Name, 1)
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=FName,
_
SubAddress:=""
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Strong Eagle

Doug,

Thank you for the information. The code snippet you provided does not make
the picture itself a hyperlink but rather creates a separate hyperlink line
when executed.

It seems to me that I need to use the Dialog.Display method, the add the
picture myself, as in

If PicDlg.Display = -1 Then
' add the picture to inline shapes
Shape1 = ActiveDocument.InlineShapes.AddPicture _
(FileName:=PicDlg.Name, LinkToFile:=False, SaveWithDocument:=True)

Then I would add the hyperlink as in

ActiveDocument.Hyperlinks.Add Anchor:=Shape1, Address:=FName, _
SubAddress:=""

However, when I try this I get the error message 'Object variable or With
block variable not set'. I've defined shape one as

Dim Shape1 As InlineShape

but unfortunately I do not know enough VB to understand the format of the
set statement for an inlineshape.
 
S

Strong Eagle

Doug,

Thank you for the information. The code snippet you provided does not make
the picture itself a hyperlink but rather creates a separate hyperlink line
when executed.

It seems to me that I need to use the Dialog.Display method, the add the
picture myself, as in

If PicDlg.Display = -1 Then
' add the picture to inline shapes
Shape1 = ActiveDocument.InlineShapes.AddPicture _
(FileName:=PicDlg.Name, LinkToFile:=False, SaveWithDocument:=True)

Then I would add the hyperlink as in

ActiveDocument.Hyperlinks.Add Anchor:=Shape1, Address:=FName, _
SubAddress:=""

However, when I try this I get the error message 'Object variable or With
block variable not set'. I've defined shape one as

Dim Shape1 As InlineShape

but unfortunately I do not know enough VB to understand the format of the
set statement for an inlineshape.
 
S

Strong Eagle

OK... I got it... code not as elegant as yours but it works

Dim PicDlg As Dialog
Dim i As Integer
Dim FName As String
Dim Shape1 As InlineShape
Dim Range1 As Range
Set fs = CreateObject("Scripting.FileSystemObject")
Set PicDlg = Dialogs(wdDialogInsertPicture)
If PicDlg.Display = -1 Then
' add the picture to inline shapes
Set Range1 = Selection.Range
Set Shape1 = ActiveDocument.InlineShapes.AddPicture _
(FileName:=PicDlg.Name, LinkToFile:=False, SaveWithDocument:=True, _
Range:=Range1)
' now move insertion point to end of picture just inserted
Range1.Collapse Direction:=wdCollapseStart
Range1.Move Unit:=wdCharacter, Count:=1
Range1.Select
' extract the filename from full path
FName = fs.GetFileName(PicDlg.Name)
' set a hyperlink with name and selection
ActiveDocument.Hyperlinks.Add Anchor:=Shape1, Address:=FName, _
SubAddress:=""
End If
 
S

Strong Eagle

OK... I got it... code not as elegant as yours but it works

Dim PicDlg As Dialog
Dim i As Integer
Dim FName As String
Dim Shape1 As InlineShape
Dim Range1 As Range
Set fs = CreateObject("Scripting.FileSystemObject")
Set PicDlg = Dialogs(wdDialogInsertPicture)
If PicDlg.Display = -1 Then
' add the picture to inline shapes
Set Range1 = Selection.Range
Set Shape1 = ActiveDocument.InlineShapes.AddPicture _
(FileName:=PicDlg.Name, LinkToFile:=False, SaveWithDocument:=True, _
Range:=Range1)
' now move insertion point to end of picture just inserted
Range1.Collapse Direction:=wdCollapseStart
Range1.Move Unit:=wdCharacter, Count:=1
Range1.Select
' extract the filename from full path
FName = fs.GetFileName(PicDlg.Name)
' set a hyperlink with name and selection
ActiveDocument.Hyperlinks.Add Anchor:=Shape1, Address:=FName, _
SubAddress:=""
End If
 

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