hyperlinks in a paragraph

S

slaprade

Anyone know how to insert hyperlinks in a blank word paragraph from .net -
Using vs2005SE and office 2007

I need to programatically replace parts of a paragraph with hyperlinks that
I know the texttodisplay and address of
 
S

Shauna Kelly

Hi

Something like this should work:

Sub AddHyperlink()

Dim oDoc As Word.Document
Dim rngHL As Word.Range

Set oDoc = ActiveDocument
Set rngHL = Selection.Range

oDoc.Hyperlinks.Add Anchor:=rngHL, _
Address:="http://www.example.com", SubAddress:="", ScreenTip:="", _
TextToDisplay:="Click here to see example.com"

End Sub

You'll need to change the syntax to suit the language you're using.

By the way, the macro recorder is a good way to find out what objects and
methods you need for tasks like this. It sometimes produces poor code and
often produces too much code, but it's often a good start and the object
model help can round out the rest.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Greg Maxey

I don't know anything abour .net or vs2005SE, but if you want to
search a document or paragraph and replace text with a hyperlink then
you could try something like:

Sub Scratchmacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Greg Maxey"
While .Execute
ActiveDocument.Hyperlinks.Add oRng, "http://
gregmaxey.mvps.org", , , "A great website"
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
 
S

slaprade

Hey thanks for the replies - here's the problem
I am working on a specific paragraph that I found with a for each para in
document.paragraps. Obviously, I should be able to select certain text out of
that paragraph and then do a search but the particular problem here is that
the paragraph I am working with is replicated often throughout the document.
I have discovered the particular paragraph by thing that happen before and
after that graph. Now, if I could turn that paragraph into a selection that
would work but I have other selection made thathelped me find it. I don't
have any idea about bookmarks. Can they be used here to add a hyperlink? or
turned into a selection?
 
G

Greg Maxey

Sorry but you only confused me. Perhaps you could give an example of what
you are trying to do.
 
S

slaprade

Love to

documument might look like this (without the numbers)

1 Title
2 paragraphs about title
3
4 Coming Soon
5
6 Title
7 paragraphs about title
8
9 Coming soon
.....

My application has some hyperlinks usually 2 or 3 that need to replace
paragraph #9 so that it might look like

9 Author | Location | Related material

This document may have 20 or thiry such sections (1 throught 4). By walking
through the paragraphs with a for each .... next I can discover the paragraph
I want to work on. I then want to replace the "Coming soon" or whatever is
there with several hyperlinks. I can't really use a selection.find because
there will possible be many "Coming soon" paragraphs in the document
 
S

slaprade

to complicate the issue, I used a selection to locate the beginning of the
section I want to work with and show that to the user as selected text - sort
of a visual clue as to what they are about to add hyperlinks to. I believe
there can be only one selection and I don't really want to loose the visual
clue
 
S

Shauna Kelly

Hi

I think it would help us if you could write out in pseudocode what you would
like to happen in terms of Finding, the Selection and the Hyperlinks.

Meanwhile, bear in mind that the .Find method works on a Range, as well as
the Selection.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Greg Maxey

The fog thickens.

It sounds like you want to "find" something(1) and when and if you do then
you want to find something(2) in that something(1) and replace the
something(2) with a hyperlink.

Tell us what something(1) and something(2) is and me or someone else will
help you with the code.
 
S

slaprade

Hey thanks for the replys

I have a solution - its kind of clunky - but it seems to work

Better idea's are always appreciated

I start by doing a find for a title with somethinglike this

TD is the activedocument
Msel is a Microsoft.Office.Interop.Word.Selection


MSel.Find.Text = Title_text
If MSel.Find.Execute() Then
MSel.MoveEnd(Microsoft.Office.Interop.Word.WdUnits.wdParagraph, 1)
MSel.Shrink() 'moves msel.start to begining of paragraph
MR = TD.Range(MSel.Start, ELoc) 'define range of bjtLine
If MR.Hyperlinks.Count > 0 Then
BjtItemLnk = MR.Hyperlinks(1).Address
End If
Call Discover(MR.Text)
MR = TD.Range(ELoc, ELoc) 'reset range to user selected location
MR.MoveEnd(Microsoft.Office.Interop.Word.WdUnits.wdParagraph, 2)
'extend to get document links
Else
MsgBox("Find Failed")
End If


If find worked, I then call discover to find the end of the section

Public Sub Discover(ByVal MStr As String)
'locates end of budget item to work on in BJT document
Dim Para As Microsoft.Office.Interop.Word.Paragraph
TxtLink = Microsoft.VisualBasic.Left(MStr, InStr(MStr, Chr(151)))
For Each Para In TD.Paragraphs
If InStr(Para.Range.Text, TxtLink) > 0 Then
FndBjt = True
End If
If FndBjt Then
Select Case True
Case Microsoft.VisualBasic.Left(Para.Range.Text, 11) =
"TEXT | HTML"
Call ProcPara(Para)
Exit For
Case Para.Range.Hyperlinks.Count > 1
Call ProcPara(Para)
Exit For
Case Microsoft.VisualBasic.Left(Para.Range.Text, 17) =
"Book moving later"
Call ProcPara(Para)
Exit For
Case Len(Para.Range.Text) < 5
Call ProcPara(Para)
Exit For
Case Para.Style.namelocal = "Title"
Exit Sub
Case Para.Style.namelocal = "Author"
Exit Sub
Case Para.Style.namelocal = "BjtDivider"
Exit Sub

... as many tests as necessay

End Select
End If
Next
End Sub

which then calls a procpara if appropriate end of section is discovered

Public Sub ProcPara(ByVal Para As Paragraph)
'processes each Hyperlink in end of budget item to check boxes
Dim HL As Hyperlink
LinkPara = Para 'LinkPara is the end of the section I will be
working with
For Each HL In LinkPara.Range.Hyperlinks
Call FndCB(HL)' this adds any hyperlink info to a hashtable
Next
Try
CheckBox1.Checked = InStr(LinkPara.Previous.Range.Text, "MOVED")
Catch ex As Exception
End Try
End Sub


When I want to process this section, adding new links, fixing old ones and
removing unwanted ones, I run this

Public Sub SaveToDoc()
'push info back to summary item
Dim HLMsg As String = "" 'replacement string for hypertext at end of
summary item
Dim FndMoved As Boolean = False
Try'set variable as to if the document has been reported to have moved
FndMoved = InStr(LinkPara.Previous.Range.Text, "MOVED") > 0
Catch ex As Exception
End Try

If CheckBox1.Checked Then
HLMsg = "MCTHLtext | MCTHLhtml"
If Not FndMoved Then
LinkPara.Previous.Range.Text =
Replace(LinkPara.Previous.Range.Text, vbCr, " MOVED" + vbCr)
End If
Else
If FndMoved Then
LinkPara.Previous.Range.Text =
Replace(LinkPara.Previous.Range.Text, "MOVED", "")
End If
HLMsg = "Book moving later"
End If

HLMsg += RetMsg(HT_HLA, Me.CheckedListBox1, 0) 'get replacement
names for string of checked items from checkedlistboxs
HLMsg += RetMsg(HT_HLA, CheckedListBox2, 0)
LinkPara.Range.Text = HLMsg + vbCrLf 'put string at end of budget item

LinkPara.Previous.Style = TD.Styles("krtText")

'find keywords and change to hyperlinks
Dim msg As String = ""
Dim ZZ() = Split(HLMsg, "|")
Dim x As Integer
Dim Indx As String = ""
Dim TS As String = BjtItemLnk
x = InStr(BjtItemLnk, "post_day=") + 9
Indx = Mid(BjtItemLnk, x, 8)
TS = Replace(TS, Indx, Format(Me.DateTimePicker1.Value, "yyyyMMdd"))
For x = 0 To UBound(ZZ)
Indx = Trim(Replace(ZZ(x), "MCTHL", ""))
msg += "-" + Indx + "-" + vbCrLf
Select Case True
Case Indx = "Book moving later"
Case Indx = "text"
AddHL(ZZ(x), Replace(TS, "format=krthtml&", ""))
Case Indx = "html"
AddHL(ZZ(x), TS)
Case Else
AddHL(ZZ(x), "")
End Select
Next


End Sub

Public Function RetMsg(ByVal HT As Hashtable, ByVal CLB As
CheckedListBox, ByVal offset As Integer) As String
'returns srtring of names related to checkboxes checked state
Dim X As Integer
RetMsg = ""

For X = 0 To CLB.Items.Count - 1
If CLB.GetItemChecked(X) Then
RetMsg += " | MCTHL" + CLB.Items(X)
End If
Next
End Function


this gets each name placed in the replacment string and adds a hyperlink
which hets its address from the hashtable and texttodisplay by removing part
of the variable name passed as ST


Public Sub AddHL(ByVal ST As String, ByVal HLt As String)
'adds Hyperlink to budget item replacing items in the replacement
string
Dim MSel As Microsoft.Office.Interop.Word.Selection

If Len(HLt) < 1 Then
HLt = HT_HLA.Item(Trim(Replace(ST, "MCTHL", "")))
End If
MSel = CLS07.thisapp.Selection 'should be an insertionIP
MSel.HomeKey()
MSel.Find.Forward = True
MSel.Find.Text = ST
If MSel.Find.Execute() Then
TD.Hyperlinks.Add(MSel.Range, HLt, "", "", UCase(Replace(ST,
"MCTHL", "")))
End If

End Sub


This parts sets the hashtable value for a chercklistboxitem to the link
provided by the editor in a textbox of the taskpane. I make two entries one
for pludal version of the checkedlistboxitem and the non plural version. This
was necessary because I set the checkedlistbox items itemcheck event to
toggle betweem unchecked. checked and indeterminate
.. The link info is the same for plual and non plural items


Public Sub HT_Text(ByVal Ci As String, ByVal AdrStr As String)
'set an item in the hashtable and duplicates the value to a plural
of the key
Dim Oi As String
Ci = Trim(Ci)
Oi = IIf(Microsoft.VisualBasic.Right(Ci, 1) = "s",
Microsoft.VisualBasic.Left(Ci, Len(Ci) - 1), Ci + "s")
HT_HLA.Item(Ci) = AdrStr
HT_HLA.Item(Oi) = AdrStr
End Sub
 

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