Bug 125485 - inserting ne TextField.URL failed with expection __main__.IllegalArgumentException
Summary: inserting ne TextField.URL failed with expection __main__.IllegalArgumentExce...
Status: ASSIGNED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
6.2.3.2 release
Hardware: x86-64 (AMD64) Windows (All)
: medium normal
Assignee: Neil Roberts
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Hyperlink Macro-Python
  Show dependency treegraph
 
Reported: 2019-05-25 10:57 UTC by mt1-wiesent
Modified: 2026-03-06 16:58 UTC (History)
4 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description mt1-wiesent 2019-05-25 10:57:32 UTC
Description:
Exception is received with following code:

text = model.Text
wtf = model.createInstance("com.sun.star.text.TextField.URL")
wtf.Representation = "testURL"
wtf.URL = "www.google.de"
curs = text.createTextCursor()
text.insertTextContent(curs, wtf, False)

Traceback (most recent call last):
  File "C:/Users/mt1-w/PycharmProjects/untitled1/test.py", line 73, in <module>
    text.insertTextContent(curs, wtf, False)
__main__.IllegalArgumentException

Other TextFields are working fine.

Steps to Reproduce:
see code from description

Actual Results:
can´t set Hyperlinks by Python script

Expected Results:
execption is caused


Reproducible: Always


User Profile Reset: No



Additional Info:
Comment 1 himajin100000 2019-06-13 07:08:41 UTC
Self-Reminder:

In "com.sun.star.text.TextField.URL" case

Exception is thrown at
https://opengrok.libreoffice.org/xref/core/editeng/source/uno/unofield.cxx?r=966f40ee#668

In "com.sun.star.text.TextField.DateTime" case, the program reaches this line

https://opengrok.libreoffice.org/xref/core/sw/source/core/unocore/unofield.cxx?r=9ff648c6#2039

wnen called from the following line

https://opengrok.libreoffice.org/xref/core/sw/source/core/unocore/unotext.cxx?r=2ddc66e6#618
============================
createInstance related code:
https://opengrok.libreoffice.org/xref/core/sw/source/uibase/uno/unotxdoc.cxx?r=0e27158c#1664
Comment 3 himajin100000 2019-06-17 17:40:42 UTC
Just a guess, not sure:

the argument to attach method is SwXTextRange
and is neither SvxUnoTextRange nor SvxUnoTextRangeBase, which seems often used in editeng ?
Comment 4 mt1-wiesent 2020-03-03 07:57:57 UTC
Short update.
I also tried it with the standard LibreOffice macro language BASIC. But in the end the result is the same. Also when using BASIC I get the IllegalArgumentException. It looks like to me that the UNO interface has a problem here or I´m doing it in the wrong way.
Comment 5 mt1-wiesent 2020-03-03 08:01:26 UTC
I also tried to do it the other way arround.
Generated a odt file with some text hyperlinks, datetime and normal text inside. Then I tried to read all text fields of the document. But only the datetime text fields where shown. The text hyperlinks where not shown.

Please see python sample code.

tf = model.TextFields
tfenum = model.TextFields.createEnumeration()
while tfenum.hasMoreElements():
    tf = tfenum.nextElement()
    print(tf)
Comment 6 Stephan Bergmann 2020-03-04 12:42:44 UTC
> mt1-wiesent@web.de changed:
> 
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>            Assignee|libreoffice-bugs@lists.free |sbergman@redhat.com
>                    |desktop.org                 |

Did you mean to add me to the CC list instead?  Please do not randomly assign bugs to other people.
Comment 7 mt1-wiesent 2020-03-04 15:11:02 UTC
Hello,

The user himajin100000 mentioned that the problem is probably caused by the files unofield.cxx and / or unotext.cxx.

I took a look in the history of these files. You where one of the main editors of these files, so I thought that maybe you can have a look to the reported bug or at least can forward the bug to the right person.

Sorry for assigning this bug to you but I thought your where the right person. 

For a developer with good knowledge of the code, debugging of this bug shouldn´t be difficult. Unfortunatelly, I don´t have a developement setup my own and most of the time I´m programming in C I´m not that aware of C++ code. 

So I´m happy to get some information at least if the bug I found is a real bug or only a user fail by myself.

Thanks
Comment 8 mt1-wiesent 2020-03-04 15:12:09 UTC
(In reply to Stephan Bergmann from comment #6)
> > mt1-wiesent@web.de changed:
> > 
> >            What    |Removed                     |Added
> > ----------------------------------------------------------------------------
> >            Assignee|libreoffice-bugs@lists.free |sbergman@redhat.com
> >                    |desktop.org                 |
> 
> Did you mean to add me to the CC list instead?  Please do not randomly
> assign bugs to other people.

please see my new comment
Comment 9 Stephan Bergmann 2020-03-04 15:27:11 UTC
Sorry, I don't have knowledge about the Writer-specific UNO API and how to use it.  (Assuming this even is about manipulating Writer documents.  The original description does not tell, but it very much looks like it.)
Comment 10 QA Administrators 2022-03-05 03:33:23 UTC Comment hidden (obsolete)
Comment 11 QA Administrators 2025-08-18 03:11:34 UTC Comment hidden (obsolete)
Comment 12 Neil Roberts 2026-03-05 18:27:38 UTC
It looks like there’s no code to handle the URL text field in a writer document and instead it only works for editeng. For example if you create a new Draw document with some shapes then this macro works to add a link to all of the shapes in the first page:

    model = XSCRIPTCONTEXT.getDocument()
    page = model.getDrawPages().getByIndex(0)

    for i in range(page.getCount()):
        shape = page.getByIndex(i)
        url = model.createInstance("com.sun.star.text.TextField.URL")
        url.Representation = "LibreOffice website"
        url.URL = "https://libreoffice.org"
        curs = shape.createTextCursor()
        shape.insertTextContent(curs, url, False)

In a Writer document it looks like links are instead handled by setting the HyperLinkURL property on the text. For example you can use this code snippet to add a link to the beginning of the document:

    model = XSCRIPTCONTEXT.getDocument()
    text = model.Text
    curs = text.createTextCursor()
    curs.setString("LibreOffice Website")
    curs.setPropertyValue("HyperLinkURL", "https://libreoffice.org")

It might be nice to make Writer handle TextField.URL and convert it to some text with the right properties. I think we would need to add an entry for it in the aProvNamesId table in unocoll.cxx. Without that it looks like createInstance falls back to creating a text field for editeng, but then it throws an exception when you try to use it with insertTextContent because that expects the cursor to be an SvxUnoTextRange but it is an SwXTextRange. I think that explains the diverging code paths taken in comment #1.
Comment 13 Neil Roberts 2026-03-06 16:58:07 UTC
Thinking about it, maybe a better approach is to just make it easier for developers to discover the HyperLinkURL property. I made a patch to add a note to the docs here:

https://gerrit.libreoffice.org/c/core/+/201147/1

and then throw an exception with a helpful message if you try to instantiate textfield.URL here:

https://gerrit.libreoffice.org/c/core/+/201148/1