making text URL to hpyerlink in paragraph(s)

DevAccess

Registered User.
Local time
Today, 09:37
Joined
Jun 27, 2016
Messages
321
Hello,

I am creating word paragraphs ( part of access report ), the below way I create a paragraph.

Set oPara4 = wddoc.Content.Paragraphs.Add
With oPara4.Range
.InsertParagraphAfter
.Text = "this is paragraph where in there would be text url in within a paragraph"
.Paragraphformat.Alignment = 0
.Paragraphformat.TabIndent (1)
With .Font
.Name = "Verdana"
.Size = "9"
'.Bold = True
End With
End With

there may be N number of paragraphs, and there could be N number of text URLs ( not hyperlinked ) within paragraph or in set of paragraphs, would it be possible to make them as HYPERLINK using programmatic way ? precisely using VBA.
 
Hi DevAccess

You would use the Hyperlinks.Add method.

Leave the anchor word out of the text you are writing and then just use the .Add method after that. So in your example leave out the last word paragraph;
Code:
.Text = "this is paragraph where in there would be text url in within a "
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "C:\some folder\document", SubAddress:="", ScreenTip:= _
        "", TextToDisplay:="paragraph"


ALTERNATIVELY

After creating the text you want to hyperlink, select it and use the Add method. The selection method depends on whether you link a single word or multiple words, but EG selects the last 8 letters as the anchor.
Code:
Selection.MoveLeft Unit:=wdCharacter, Count:=8, Extend:=wdExtend
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "C:\some folder\document", SubAddress:="", ScreenTip:= _
        "", TextToDisplay:="hyperlnk"
 
Hi IssKint,

I am not sure if I understand your below statement thoroughly.

Leave the anchor word out of the text you are writing and then just use the .Add method after that. So in your example leave out the last word paragraph;

Can you help me understand with simple example.

Thanks
DJ
 
A hyperlink uses a word or phrase to anchor the hyperlink to (the bit that is underlined and in a different colour). Normally in a Word doc you would highlight the word/phrase and add the hyperlink OR use the add hyperlink function. That is what you need to do in VBA.

So, in your original post code you had this line
Code:
.Text = "this is paragraph where in there would be text url in within a paragraph"

Lets use the last word as the anchor, so in your example i will use the last word = paragraph, so remove it from your code.
Code:
.Text = "this is paragraph where in there would be text url in within a "
then add the extra line of code to create the hyperlink;
Code:
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "C:\some folder\document", SubAddress:="", ScreenTip:= _
        "", TextToDisplay:="paragraph"
The Hyperlinks.Add method uses the following;
Selection.Range - this is the highlighted text, so in this example nothing is highlighted so it will use the current cursor position (in the alternate option i suggested this range would be the part selected by the Selection.MoveLeft code.)
Address - this is the file/web address you want as the hyperlink
ScreenTip - this is the text to display when a user hovers the mouse over the hyperlink (you could make this "Click here to go!" or something)
TextToDisplay - this is the text you want highlighted, so in this case paragraph
 

Users who are viewing this thread

Back
Top Bottom