Page 1 of 1

[Solved] Simple Way for Hyperlink in table cell

PostPosted: Thu Jun 14, 2012 5:54 pm
by tbuecki
Hi,

I have a table in a writer document. In a cell, i want a text with a hyperlink. I have done it in this way:

Code: Select all   Expand viewCollapse view
  oFrame = ThisComponent.createInstance( "com.sun.star.text.TextFrame" )
  oCell = objTabelle.getCellByName("A1")
  oCellText = oCell.Text
  oCellText.insertTextContent(oCellText.Start,  oFrame, false)
  ...
   oFrame.Text.String = "Click here"
   oFrame.Text.HyperLinkUrl = "http://user.services.openoffice.org/en/forum/"

Is there another simple possibility to set a hyperlink in a table cell?

Kind regards
Thomas

Re: Simpe Way for Hyperlink in table cell

PostPosted: Thu Jun 14, 2012 11:58 pm
by Charlie Young
You can do it without the TextFrame, which may be cleaner, if not simpler.

Code: Select all   Expand viewCollapse view
   oCell = objTabelle.getCellByName("A1")
     oCellText = oCell.Text
     oCursor = oCellText.createTextCursor()
   oCursor.setString("Click here")
   oCursor.gotoStart(False)
   oCursor.gotoEnd(True)
   oCursor.HyperLinkURL = "http://user.services.openoffice.org/en/forum/"


It is probably worth noting something I discovered here; the following works in a Calc cell:

Code: Select all   Expand viewCollapse view
Sub MakeForumLinkInA1
   Dim oDoc As Object
   Dim oSheet As Object
   Dim oCell As Object
   Dim oCursor As Object
   Dim oCellText
   Dim tField
   
   oDoc = ThisComponent
   oSheet = oDoc.Sheets(0)
   oCell = oSheet.getCellRangeByName("A1")
   tField = oDoc.createInstance( "com.sun.star.text.textfield.URL" )
   tField.Representation = "Click here"
   tField.URL = "http://user.services.openoffice.org/en/forum/"
   oCellText = oCell.getText()
   oCursor = oCellText.createTextCursor()
   oCursor.gotoStart(False)
   oCellText.insertTextContent(oCursor,tField,False)
End Sub


But it seems you cannot do insertTextContent with a com.sun.star.text.textfield.URL either into a Writer table or into the main text, and conversely, there is no HyperLinkURL property on an XTextCursor in the text of a Calc cell. Thus the Calc and Writer cases need to be handled differently, though you might be able to insert a com.sun.star.text.TextFrame into a Calc cell (I haven't tried that yet).

 Edit: insertTextContent in aCalc cell doesn't seem to work with a TextFrame. 

Re: Simpe Way for Hyperlink in table cell

PostPosted: Fri Jun 15, 2012 3:29 pm
by tbuecki
Thanks, thats works fine in Writer, if it is only one link in the cell. But when I have several Hyperlinks in one cell, I have to use text-frames, Am I right?

Regards
Thomas

Re: Simpe Way for Hyperlink in table cell

PostPosted: Fri Jun 15, 2012 4:29 pm
by Charlie Young
tbuecki wrote:Thanks, thats works fine in Writer, if it is only one link in the cell. But when I have several Hyperlinks in one cell, I have to use text-frames, Am I right?

Regards
Thomas


No, it's not necessary to use text-frames, though this can get a bit tricky depending on what you're after.

An example:

Code: Select all   Expand viewCollapse view
Sub AddLinksToTableCell()
   Dim oDoc As Object
   Dim oTables As Object
   Dim oTable As Object
   Dim oCell As Object
   Dim oCursor As Object
   Dim oCellText As Object
   Dim oString As String
   Dim Entries
   Dim Entry As String
   Dim UrlHead As String
   Dim Url As String
   Dim i As Long
   Dim EntryLoc As Long
   
   Entries = Array("Russell","Weyl","Wittgenstein","Whitehead")
   UrlHead = "http://plato.stanford.edu/entries/" 
   oDoc = ThisComponent
   oTables = oDoc.TextTables
   oTable = oTables(0)
   
   oCell = oTable.getCellByName("A1")
   oCellText = oCell.getText()
   
   oCursor = oCellText.createTextCursor()
   oString = "Russell, Weyl, Wittgenstein, Whitehead"
   oCursor.setString(oString)
   for i = 0 to UBound(Entries)
      oCursor.gotoStart(False)
      Entry = CStr(Entries(i))
      Url = UrlHead & Entry & "/"
      EntryLoc = InStr(oString,Entry) - 1
      oCursor.goRight(EntryLoc,False)
      oCursor.goRight(Len(Entry),True)
      oCursor.HyperLinkURL = Url
   next i
      
End Sub


Re: [Solved] Simple Way for Hyperlink in table cell

PostPosted: Mon Jun 18, 2012 4:17 pm
by tbuecki
Hello,

that's exactly what I'm searching for. Thanks very much.

Regards
Thomas