Group: microsoft.public.word.vba.general
From: =?Utf-8?B?YWxib3Jn?=
Date: Sunday, January 27, 2008 5:22 AM
Subject: Re: Creating a hyperlink in part of a cell

Hi John, Shauna:

Why even use "vbTab"? For example, you can instantly add 18 rows into a 3
column table (Ref #, Name, Page #) with this code:

Dim rownum As Integer, I As Integer, ii As Integer, Username as String,
pageno as Integer

Username = "John Doe"
pageno = 12 ' or whatever you wish to assign to it

response = MsgBox("Add new rows?", vbQuestion + vbYesNo)
If response = vbYes Then
ii = 0
If IsNull(ActiveDocument.FormFields("admdate1").Result) Then
MsgBox "Please fill in the admission date first!", vbInformation
Else
Do Until ii > 18
ActiveDocument.Unprotect
ActiveDocument.Tables(1).Rows.Add
rownum = ActiveDocument.Tables(1).Rows.Count
For I = 1 To ActiveDocument.Tables(1).Columns.Count
ActiveDocument.FormFields.Add
Range:=ActiveDocument.Tables(1).Cell(rownum, I).Range,
Type:=wdFieldFormTextInput
Next I

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count - 1).Range.FormFields(1).Result = ii

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count).Range.Hyperlinks.Add
Anchor:=NameTable.Cells(rownum, 2).Range, Address:="",
TextToDisplay:=UserName, SubAddress:=BookMarkName, ScreenTip:="Click to view
user"

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count -
1).Range.ParagraphFormat.TabStops.Add Position:=InchesToPoints(5.4),
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderDots

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count + 1).Range.FormFields(1).Result =
pageno
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, noreset:=True
ii = ii + 1
Loop
End If
End If

*** Neat problem! *** Sorry I haven't tested the above for bugs, but it's
pretty straightforward.

Cheers,
Al

"Shauna Kelly" wrote:

> Hi John
>
> Something like this (add error checking to taste, and fix line breaks as
> needed):
>
> Sub ForJohnM()
>
> Dim oDoc As Word.Document
> Dim NameTable As Word.Table
> Dim RowNum As Long
> Dim UserName As String
> Dim BookMarkName As String
> Dim oCell As Word.Cell
> Dim rngHL As Word.Range
>
> 'Get a reference to the ActiveDocument.
> 'Avoid using 'ActiveDocument' more than once because
> 'Word is fickle
> Set oDoc = ActiveDocument
>
> Set NameTable = oDoc.Tables(1)
> RowNum = 1
> UserName = "John"
> BookMarkName = "Whatever"
>
> 'Get the cell to work with
> Set oCell = NameTable.Rows(RowNum).Cells(2)
>
> 'Add a tab stop with leader dots to the cell
> oCell.Range.ParagraphFormat.TabStops.Add
> Position:=InchesToPoints(5.4), Alignment:=wdAlignTabRight,
> Leader:=wdTabLeaderDots
>
> 'Get a range that does *not* include the end-of-cell marker
> Set rngHL = oCell.Range
> rngHL.MoveEnd Unit:=wdCharacter, Count:=-1
>
> 'Add a hyperlink to our range
> oDoc.Hyperlinks.Add Anchor:=rngHL, Address:="",
> TextToDisplay:=UserName, SubAddress:=BookMarkName, ScreenTip:="Click to
> view user"
>
> 'Add a tab at the very end of the cell, ie after the hyperlinked
> range
> oCell.Range.InsertAfter vbTab
>
>
> End Sub
>
>
> By the way, if this were mine and if there were lots of these cells, I'd
> create a separate paragraph style that includes the tab stop. You'll
> then be able to apply the style to the relevant cells. It will make it a
> lot faster to run the code and a lot easier to maintain.
>
>
> Hope this helps.
>
> Shauna Kelly. Microsoft MVP.
> http://www.shaunakelly.com/word
>
>
>
>
> "John Marshall, MVP" wrote in message
> news:eb5MP2IYIHA.5784@TK2MSFTNGP03.phx.gbl...
> >I have a routine that populates a three column table (Ref #, Name, Page
> >#) that works fine. The cell with the Name is hyperlinked. I am now
> >trying to add a tab leader of dots to the end of the Name cell. I can
> >add the tab and set the tab leader to dots, but the tab is included in
> >the hyperlink. Any ideas how to correct this?
> >
> > Username = UserName & vbTab
> >
> > ActiveDocument.Hyperlinks.Add Anchor:=NameTable.Cells(RowNum,2).Range,
> > Address:="", TextToDisplay:=UserName, SubAddress:=BookMarkName, _
> > ScreenTip:="Click to view user"
> >
> > NameTable.Cell(RowNum,2).Range.ParagraphFormat.Tabstops.Add
> > Position:=InchesToPoints(5.4), Alignment:=wdAlignTabRight,
> > Leader:=wdTabLeaderDots
> >
> > Any alternate suggestions would also be appreciated.
> >
> > John... Visio MVP
> >
>
>
>

Safety Articles | Usenet Groups | Usenet News | Bluegrass