Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Need Help With Hyperlink Macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Strong Eagle - 07 Oct 2006 02:56 GMT
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
Doug Robbins - Word MVP - 07 Oct 2006 09:31 GMT
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

Signature

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

> Hello All,
>
[quoted text clipped - 42 lines]
>      SubAddress:=""
>    End If
Strong Eagle - 07 Oct 2006 10:31 GMT
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.

> The following lines of code are selecting the last picture in the document
> as i is being set to the count of the pictures.
[quoted text clipped - 60 lines]
> >      SubAddress:=""
> >    End If
Strong Eagle - 07 Oct 2006 10:32 GMT
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.

> The following lines of code are selecting the last picture in the document
> as i is being set to the count of the pictures.
[quoted text clipped - 60 lines]
> >      SubAddress:=""
> >    End If
Strong Eagle - 07 Oct 2006 11:33 GMT
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

> Doug,
>
[quoted text clipped - 87 lines]
> > >      SubAddress:=""
> > >    End If
Strong Eagle - 07 Oct 2006 11:34 GMT
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

> Doug,
>
[quoted text clipped - 87 lines]
> > >      SubAddress:=""
> > >    End If

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.