简体   繁体   中英

Copy text from Word and get file location from clipboard

I want to achieve the following:

I copy some text in a Word application (or another application where the clipboard is updated with text). I now want to know the location path of the Word document, so I can store the path and open the document for a reference later on.

I would like to do it for websites as well, so I can get the website where the text was copied from.

I want do this in C# and Windows 10. My initial thought was to create a CTRL + C event listener, and find the active application and get the location like that. But I cant link the copied text, and the text path together.

Any ideas out there?

You can retrieve such information, but with limitaions. When there is no such information stored in clipboard you're out of luck, but, hopefully for you, many applications store way more data in clipboard along with text, including path or url to the document.

Different applications using different different formats to track document location, so main idea is to try read from clipboard all relevant clipboard formats that include document location one by one and try to extract location.

Here several clipboard formats that contains or can contain information needed to you:

You can find more about different clipboard formats here . Also you can use any clipboard format viewer to view what is actually stored in clipboard by different applications.

For example, all modern browsers and all Microsoft Office suite applications store in clipboard actual document location in HTML Format as simple plain text:

Version:1.0
StartHTML:000000271
EndHTML:000008359
StartFragment:000008219
EndFragment:000008255
StartSelection:000008219
EndSelection:000008255
SourceURL:http://stackoverflow.com/questions/42672385/copy-text-from-word-and-get-file-location-from-clipboard-c-sharp
...

You can't do this, I am afraid. This information is not made available in the clipboard.

Even if you listen to Ctrl + C and find an active instance of Word running, you still won't be 100% you get what you need. It might be a new document, which is not even saved yet on disk. Even more convoluted case: the user copies some text from an edit field on a dialog in Word.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM