[Solved] Simple Way for Hyperlink in table cell

Creating a macro - Writing a Script - Using the API

[Solved] Simple Way for Hyperlink in table cell

Postby tbuecki » Thu Jun 14, 2012 5:54 pm

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
Last edited by tbuecki on Mon Jun 18, 2012 4:16 pm, edited 1 time in total.
OpenOffice 3.4 on Windows 7
tbuecki
 
Posts: 10
Joined: Wed May 23, 2012 4:29 pm

Re: Simpe Way for Hyperlink in table cell

Postby Charlie Young » Thu Jun 14, 2012 11:58 pm

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. 
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Simpe Way for Hyperlink in table cell

Postby tbuecki » Fri Jun 15, 2012 3:29 pm

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
OpenOffice 3.4 on Windows 7
tbuecki
 
Posts: 10
Joined: Wed May 23, 2012 4:29 pm

Re: Simpe Way for Hyperlink in table cell

Postby Charlie Young » Fri Jun 15, 2012 4:29 pm

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

Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

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

Postby tbuecki » Mon Jun 18, 2012 4:17 pm

Hello,

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

Regards
Thomas
OpenOffice 3.4 on Windows 7
tbuecki
 
Posts: 10
Joined: Wed May 23, 2012 4:29 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: MSN [Bot] and 10 guests