locked
Creating a hyperlink within a table cell using VB in Word RRS feed

  • Question

  • Hi All,

    I have been tasked with creating an outlook signature, and due to the nature of the layout required, I need to use a table. I have programmed the signature and have correctly replicated the layout, however I am struggling to make the contents of one of the cells into a hyperlink.

    Can anyone give me any pointers on how to make the contents of cell (5, 2) into a hyperlink? Also, does anyone know how I can set the height of each row (or all of them at once!). Everything I have tried seems to be ignored.

    I have included the bit of the script that generates the table.

     

    SET objRange = objSelection.Range
    objDoc.Tables.Add ObjRange, 5, 3
    SET objTable = objDoc.Tables(1)
    
    objTable.Columns(1).Width = ObjWord.CentimetersToPoints(2)
    objTable.Columns(2).Width = ObjWord.CentimetersToPoints(7)
    objTable.Columns(3).Width = ObjWord.CentimetersToPoints(7)
    
    objTable.Cell(1, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(1, 1).Range.Font.Size = "9"
    objTable.Cell(1, 1).Range.Font.Bold = True
    objTable.Cell(1, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(1, 1).Range.Text = " "
    objTable.Cell(1, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(1, 2).Range.Font.Size = "9"
    objTable.Cell(1, 2).Range.Font.Bold = True
    objTable.Cell(1, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(1, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(1, 3).Range.Font.Size = "9"
    objTable.Cell(1, 3).Range.Font.Bold = True
    objTable.Cell(1, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(1, 3).Range.Text = "My Company"
    
    objTable.Cell(2, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(2, 1).Range.Font.Size = "9"
    objTable.Cell(2, 1).Range.Font.Bold = True
    objTable.Cell(2, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(2, 1).Range.Text = "Phone"
    objTable.Cell(2, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(2, 2).Range.Font.Size = "9"
    objTable.Cell(2, 2).Range.Font.Bold = False
    objTable.Cell(2, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(2, 2).Range.Text = DirectPhone
    objTable.Cell(2, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(2, 3).Range.Font.Size = "9"
    objTable.Cell(2, 3).Range.Font.Bold = False
    objTable.Cell(2, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(2, 3).Range.Text = StreetAddress
    
    objTable.Cell(3, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(3, 1).Range.Font.Size = "9"
    objTable.Cell(3, 1).Range.Font.Bold = True
    objTable.Cell(3, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(3, 1).Range.Text = "Fax"
    objTable.Cell(3, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(3, 2).Range.Font.Size = "9"
    objTable.Cell(3, 2).Range.Font.Bold = False
    objTable.Cell(3, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(3, 2).Range.Text = FaxNumber
    objTable.Cell(3, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(3, 3).Range.Font.Size = "9"
    objTable.Cell(3, 3).Range.Font.Bold = False
    objTable.Cell(3, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(3, 3).Range.Text = Town & ", " & State & " " & ZipCode
    
    objTable.Cell(4, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(4, 1).Range.Font.Size = "9"
    objTable.Cell(4, 1).Range.Font.Bold = True
    objTable.Cell(4, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(4, 1).Range.Text = "Email"
    objTable.Cell(4, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(4, 2).Range.Font.Size = "9"
    objTable.Cell(4, 2).Range.Font.Bold = False
    objTable.Cell(4, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(4, 2).Range.Text = EmailAddress
    objTable.Cell(4, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(4, 3).Range.Font.Size = "9"
    objTable.Cell(4, 3).Range.Font.Bold = False
    objTable.Cell(4, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(4, 3).Range.Text = POBoxLine1
    
    objTable.Cell(5, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(5, 1).Range.Font.Size = "9"
    objTable.Cell(5, 1).Range.Font.Bold = True
    objTable.Cell(5, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(5, 1).Range.Text = "Web"
    objTable.Cell(5, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(5, 2).Range.Font.Size = "9"
    objTable.Cell(5, 2).Range.Font.Bold = False
    objTable.Cell(5, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(5, 2).Range.Text = "www.MyCo.com"
    objTable.Cell(5, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(5, 3).Range.Font.Size = "9"
    objTable.Cell(5, 3).Range.Font.Bold = False
    objTable.Cell(5, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(5, 3).Range.Text = POBoxLine2
    

    I would really appreciate it if someone was able to assist me.

    Thanks in advanced!

    Andrew.


    • Edited by AndrewC1985 Monday, November 14, 2011 9:52 PM Title Update
    Monday, November 14, 2011 9:41 PM

Answers

  • I've tidied up the code a little so that you can more easily see what it's doing - and included code to (a) set the row height (arbitrarily to 1cm) and (b) include the hyperlink.
     
     
     
    Set objRange = objSelection.Range
    objDoc.Tables.Add objRange, 5, 3
    Set objTable = objDoc.Tables(1)
    With objTable
     
        .Columns(1).Width = objWord.CentimetersToPoints(2)
        .Columns(2).Width = objWord.CentimetersToPoints(7)
        .Columns(3).Width = objWord.CentimetersToPoints(7)
     
        .Rows.HeightRule = wdRowHeightExactly
        .Rows.Height = objWord.CentimetersToPoints(1)
       
        With ..Range.Font
            .Name = "Arial"
            .Size = 14
            .Color = wdBlue
            .Bold = False
        End With
       
        .Rows(1).Range.Font.Bold = True
        ..Columns(1).Range.Font.Bold = True
     
        .Cell(1, 1).Range.Text = " "
        .Cell(1, 3).Range.Text = "My Company"
     
        .Cell(2, 1).Range.Text = "Phone"
        .Cell(2, 2).Range.Text = DirectPhone
        .Cell(2, 3).Range.Text = StreetAddress
     
        .Cell(3, 1).Range.Text = "Fax"
        .Cell(3, 2).Range.Text = FaxNumber
        .Cell(3, 3).Range.Text = Town & ", " & State & " " & ZipCode
     
        .Cell(4, 1).Range.Text = "Email"
        .Cell(4, 2).Range.Text = EmailAddress
        .Cell(4, 3).Range.Text = POBoxLine1
     
        .Cell(5, 1).Range.Text = "Web"
     
        .Hyperlinks.Add Anchor:=objTable.Cell(5, 2).Range, _
                        Address:="http://www.MyCo.com", _
                        TextToDisplay:="www.MyCo.com"
     
        .Cell(5, 3).Range.Text = POBoxLine2
     
    End With

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by AndrewC1985 Wednesday, November 16, 2011 9:58 PM
    Tuesday, November 15, 2011 11:04 AM

All replies

  • Hi All,

    I have been tasked with creating an outlook signature, and due to the nature of the layout required, I need to use a table. I have programmed the signature and have correctly replicated the layout, however I am struggling to make the contents of one of the cells into a hyperlink.

    Can anyone give me any pointers on how to make the contents of cell (5, 2) into a hyperlink? Also, does anyone know how I can set the height of each row (or all of them at once!). Everything I have tried seems to be ignored.

    I have included the bit of the script that generates the table.

     

    SET objRange = objSelection.Range
    objDoc.Tables.Add ObjRange, 5, 3
    SET objTable = objDoc.Tables(1)
    
    objTable.Columns(1).Width = ObjWord.CentimetersToPoints(2)
    objTable.Columns(2).Width = ObjWord.CentimetersToPoints(7)
    objTable.Columns(3).Width = ObjWord.CentimetersToPoints(7)
    
    objTable.Cell(1, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(1, 1).Range.Font.Size = "9"
    objTable.Cell(1, 1).Range.Font.Bold = True
    objTable.Cell(1, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(1, 1).Range.Text = " "
    objTable.Cell(1, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(1, 2).Range.Font.Size = "9"
    objTable.Cell(1, 2).Range.Font.Bold = True
    objTable.Cell(1, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(1, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(1, 3).Range.Font.Size = "9"
    objTable.Cell(1, 3).Range.Font.Bold = True
    objTable.Cell(1, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(1, 3).Range.Text = "My Company"
    
    objTable.Cell(2, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(2, 1).Range.Font.Size = "9"
    objTable.Cell(2, 1).Range.Font.Bold = True
    objTable.Cell(2, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(2, 1).Range.Text = "Phone"
    objTable.Cell(2, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(2, 2).Range.Font.Size = "9"
    objTable.Cell(2, 2).Range.Font.Bold = False
    objTable.Cell(2, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(2, 2).Range.Text = DirectPhone
    objTable.Cell(2, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(2, 3).Range.Font.Size = "9"
    objTable.Cell(2, 3).Range.Font.Bold = False
    objTable.Cell(2, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(2, 3).Range.Text = StreetAddress
    
    objTable.Cell(3, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(3, 1).Range.Font.Size = "9"
    objTable.Cell(3, 1).Range.Font.Bold = True
    objTable.Cell(3, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(3, 1).Range.Text = "Fax"
    objTable.Cell(3, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(3, 2).Range.Font.Size = "9"
    objTable.Cell(3, 2).Range.Font.Bold = False
    objTable.Cell(3, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(3, 2).Range.Text = FaxNumber
    objTable.Cell(3, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(3, 3).Range.Font.Size = "9"
    objTable.Cell(3, 3).Range.Font.Bold = False
    objTable.Cell(3, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(3, 3).Range.Text = Town & ", " & State & " " & ZipCode
    
    objTable.Cell(4, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(4, 1).Range.Font.Size = "9"
    objTable.Cell(4, 1).Range.Font.Bold = True
    objTable.Cell(4, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(4, 1).Range.Text = "Email"
    objTable.Cell(4, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(4, 2).Range.Font.Size = "9"
    objTable.Cell(4, 2).Range.Font.Bold = False
    objTable.Cell(4, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(4, 2).Range.Text = EmailAddress
    objTable.Cell(4, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(4, 3).Range.Font.Size = "9"
    objTable.Cell(4, 3).Range.Font.Bold = False
    objTable.Cell(4, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(4, 3).Range.Text = POBoxLine1
    
    objTable.Cell(5, 1).Range.Font.Name = "Tahoma"
    objTable.Cell(5, 1).Range.Font.Size = "9"
    objTable.Cell(5, 1).Range.Font.Bold = True
    objTable.Cell(5, 1).Range.Font.Color = wdBlack 'black
    objTable.Cell(5, 1).Range.Text = "Web"
    objTable.Cell(5, 2).Range.Font.Name = "Tahoma"
    objTable.Cell(5, 2).Range.Font.Size = "9"
    objTable.Cell(5, 2).Range.Font.Bold = False
    objTable.Cell(5, 2).Range.Font.Color = wdBlack 'black
    objTable.Cell(5, 2).Range.Text = "www.MyCo.com"
    objTable.Cell(5, 3).Range.Font.Name = "Tahoma"
    objTable.Cell(5, 3).Range.Font.Size = "9"
    objTable.Cell(5, 3).Range.Font.Bold = False
    objTable.Cell(5, 3).Range.Font.Color = wdBlack 'black
    objTable.Cell(5, 3).Range.Text = POBoxLine2
    

    I would really appreciate it if someone was able to assist me.

    Thanks in advanced!

    Andrew.

     

     

     

    • Merged by Kee Poppy Tuesday, November 15, 2011 2:06 AM Keep the same topic in one thread
    Monday, November 14, 2011 6:44 AM
  • Andrew,

    This forum is for VB in VSN (Visual Studio Net).

    The VB used in Office is called VBA (Visual Basic for Applications)

    This is the forum for it.

    http://social.msdn.microsoft.com/Forums/en-US/isvvba


    Success
    Cor
    Monday, November 14, 2011 8:03 AM
  • Sorry, thanks!
    Monday, November 14, 2011 9:42 PM
  • I've tidied up the code a little so that you can more easily see what it's doing - and included code to (a) set the row height (arbitrarily to 1cm) and (b) include the hyperlink.
     
     
     
    Set objRange = objSelection.Range
    objDoc.Tables.Add objRange, 5, 3
    Set objTable = objDoc.Tables(1)
    With objTable
     
        .Columns(1).Width = objWord.CentimetersToPoints(2)
        .Columns(2).Width = objWord.CentimetersToPoints(7)
        .Columns(3).Width = objWord.CentimetersToPoints(7)
     
        .Rows.HeightRule = wdRowHeightExactly
        .Rows.Height = objWord.CentimetersToPoints(1)
       
        With ..Range.Font
            .Name = "Arial"
            .Size = 14
            .Color = wdBlue
            .Bold = False
        End With
       
        .Rows(1).Range.Font.Bold = True
        ..Columns(1).Range.Font.Bold = True
     
        .Cell(1, 1).Range.Text = " "
        .Cell(1, 3).Range.Text = "My Company"
     
        .Cell(2, 1).Range.Text = "Phone"
        .Cell(2, 2).Range.Text = DirectPhone
        .Cell(2, 3).Range.Text = StreetAddress
     
        .Cell(3, 1).Range.Text = "Fax"
        .Cell(3, 2).Range.Text = FaxNumber
        .Cell(3, 3).Range.Text = Town & ", " & State & " " & ZipCode
     
        .Cell(4, 1).Range.Text = "Email"
        .Cell(4, 2).Range.Text = EmailAddress
        .Cell(4, 3).Range.Text = POBoxLine1
     
        .Cell(5, 1).Range.Text = "Web"
     
        .Hyperlinks.Add Anchor:=objTable.Cell(5, 2).Range, _
                        Address:="http://www.MyCo.com", _
                        TextToDisplay:="www.MyCo.com"
     
        .Cell(5, 3).Range.Text = POBoxLine2
     
    End With

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by AndrewC1985 Wednesday, November 16, 2011 9:58 PM
    Tuesday, November 15, 2011 11:04 AM
  • Thank you so much for you help, that is fantastic!
    Wednesday, November 16, 2011 9:58 PM