[Solved] UNO copy and paste have no effect sometimes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

[Solved] UNO copy and paste have no effect sometimes

Post by wanglong »

When I use the UNO's select, copy, and paste interfaces to copy content from the source document to the target document paragraph by paragraph (hundreds or thousands of times), sometimes copying fails. The symptom of the failure is that empty paragraphs are pasted in the target document. Has anyone encountered this issue?

Code: Select all

DispatchResultEvent oo = null;
while(length-- > 0){
   xDispatchHelper.executeDispatch(xDispatchProvider, ".uno:EndOfParaSel", "", 0, new PropertyValue[]{new PropertyValue()}); 
   oo = (DispatchResultEvent) xDispatchHelper.executeDispatch(xDispatchProvider, ".uno:SelectTable", "", 0, new PropertyValue[]{new PropertyValue()}); 
}
xDispatchHelper.executeDispatch(xDispatchProvider, ".uno:Copy", "", 0, new PropertyValue[]{new PropertyValue()});
xDispatchHelper.executeDispatch(xDispatchProvider, ".uno:Paste", "", 0, new PropertyValue[]{new PropertyValue()});
I'm looking forward to and appreciate your help very much!!! :crazy: :crazy:
Last edited by MrProgrammer on Sat Mar 16, 2024 7:24 pm, edited 2 times in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
Libre Office 7.6 on Windows 11.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: UNO select,copy and past have no effect sometimes...

Post by JeJe »

There have been past threads with copy and paste issues where the clipboard isn't cleared - so there are issues even if that isn't the problem here.

Why are you doing copy and paste operations thousands of times? Would it be easier to copy the whole document and delete the bits you don't need?

Edit: Ideally you'd be able to select all the bits you want to copy with a find operation and copy and paste in one. Or select the bits you don't want that way and delete them all in one having copied everything. That might involve marking them in some way so this is possible.

You could also try gettransferable/inserttransferable instead of copy and paste.

https://www.openoffice.org/api/docs/com ... plier.html
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: UNO select, copy and past have no effect sometimes...

Post by wanglong »

Emmm....The statement "thousands of times" may be an exaggeration; it is just a quantification to ensure reproducibility of the issue. Even if I only perform dozens of copy-paste operations, repeatedly using this function will still encounter this problem.

I will try to use gettransferable/inserttransferable instead of copy and paste.But I want to ensure that the format of the pasted text remains consistent with the source format. Is it possible to achieve this?
Libre Office 7.6 on Windows 11.
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: UNO select, copy and past have no effect sometimes...

Post by JeJe »

xtransferable is the same as the clipboard data except the clipboard isn't used.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: UNO select, copy and past have no effect sometimes...

Post by JeJe »

You could run some tests where you examine the selection every time you copy and paste to see if you can pin down the circumstance where this happens.

You could try a pause(s) in your code (in Basic a Wait statement would be used) to give OO the time to complete the operation before the next call - in case that's part of the problem.

As you're on windows you try the Windows API to whether opening and closing the clipboard helps with this.
viewtopic.php?p=522162#p522162
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: UNO select, copy and past have no effect sometimes...

Post by wanglong »

You're absolutely right.

I tried adding a 500ms delay between 'select' and 'copy', which partially resolved the issue, but it also slowed down my document processing speed. Moreover, it hasn't been thoroughly tested, so I can't guarantee that this approach will be problem-free 100% of the time.
Libre Office 7.6 on Windows 11.
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: UNO select, copy and paste have no effect sometimes

Post by wanglong »

:super: :super: :super: :super: :super: :super:
Libre Office 7.6 on Windows 11.
Post Reply