Insert Field Macro

D

Dave

I want to create a macro which will take file names from a
list in a Word doc and convert the name into a hyperlink
using the Field dialog box.

When setting the macro up, using the recorder, the
filename pasted into the box is used everytime the macro
is run, instead of the actual filename selected from the
list in the doc.

What is required is a way of telling the macro to paste in
the filename to the dialog box each time it runs.

How can this be done?

Thanks,

Dave.
 
D

Doug Robbins - Word MVP

Hi Dave,

You won't be able to use the dialog box for this.

Where do you want the hyperlinks to be inserted? In place of the filename
in the list, or at some other location?

Is the list the only thing in the document? If not, how will the macro
interate through the list?

Post back to the group with the answers to these questions and we can
probably give you a solution.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

Dave

Some further clarification:

The Field to be inserted is now Include Text, not a
hyperlink.

The Field will be inserted in place of the filename in the
list, and the list is the only thing in the document.

Dave.
 
D

Doug Robbins - Word MVP

Hi Dave,

If each item in the list is a separate paragraph (and there are no empty
paragraphs in the document), the following should do what you want.

Dim docrange As Range, doclength As Integer, apara As Paragraph, i As
Integer
For Each apara In ActiveDocument.Paragraphs
Set docrange = apara.Range
docrange.End = docrange.End - 1
docrange.Select
doclength = Len(docrange)
For i = doclength To 1 Step -1
If Mid(docrange, i, 1) = "\" Then
docrange = Left(docrange, i - 1) & "\" & Mid(docrange, i)
i = i - 2
End If
Next i
ActiveDocument.Fields.Add Range:=docrange, Type:=wdFieldEmpty, Text:= _
"INCLUDETEXT " & Chr(34) & docrange & Chr(34),
PreserveFormatting:=True
Next apara

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

Dave

Being a beginner at this, and normally creating macros
using the recorder, with minor changes by editing, how do
I use this code?

Thanks again,

Dave.
 
J

Jonathan West

J

Jonathan West

I suspect that the syntax error is because the first line of code was split
into two when it was included in the message. Put the second line "Integer"
on the end of the first, and all should be well.


--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email
 

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