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:
HTML Format
msSourceUrl
FileName and FileNameW
UniformResourceLocator and UniformResourceLocatorW
ObjectLink
Hyperlink
etc.
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.
Related
We need to copy and re-post what is in the clipboard - all formats. Is there a way to do this?
Here's the use case. We have small docx/pptx/xlsx documents that, in our Office AddIn, we want to enable the user to drag/drop into word/ppt/xl. Unfortunately, the best documented format for this is html which is not that close to the actual docx/pptx/xlsx.
So, my thought is we (using Word as the example) create an instance of a Word Document object (our code is a Word AddIn so by definition Word is running). We then call that Document object to select all of it and then paste the selection.
At this point the clipboard contains that pasted document. Including in the undocumented format Word uses for copy/paste within Word documents. That's the format we want to use.
If we can then copy all of the clipboard contents into an object of ours that implements IDataObject and pass that object to Control.DoDragDrop(), then when the user drops it somewhere in Word, they are giving it to Word in the undocumented format which has all the docx formatting.
Is there a way to do this?
Update: We need to drag/drop the file contents (not just the text, the fully formatted text, tables, shapes, charts, etc.).
Update 2: We need to pass this as an IDataObject to DoDragDrop(), not do a paste from the clipboard. We do not need to be notified when it happens because we've passed the IDataObject to Windows and Windows takes it from there.
It turns out there's a call for this - Clipboard.GetDataObject().
Suppose that I would like to add extra information about a file, without writing that information as content of that file. How would I do this? A couple of good examples are:
With Word documents, you can add Author tag to a document. And,
MP3 files have lots of info stored inside of them but when you play the file, you don't see that info (unless the program playing the file has been programmed to display that information).
How does Windows do this?
This information is stored in the file system (on windows - NTFS).
In NTFS, you can actually store another file, as part of this information, and it stores much more information about each file than you may expected.
NTFS file streams
Exapmle in C how to consume them
About MP3 and word - In these cases the information is stored inside the file, as part of its format.
I am developing a Word Addin. There is a piece of functionality within the Addin that is required to retireive the original location of a picture that has been inserted into a document.
It doesn't matter if the Image file no longer exists in the original directory. I will handle that in the code.
I think there is no way to do this. I did had the same requirement to find the file name from the image in the document. So I had to insert the image with the file name in its alternative text description to achieve this.
The question got me curious, so I tried the following: add an image to a word document, save it, zip it and start looking into the xml document. The media folder contains the image as embedded in word, which at that point has been renamed and "forgot" about its origin. On the other hand, document.xml does contain a lot of information about the image enclosed in the tag, and that includes the whole path to the original picture.
I don't know if the Open XML SDK gives you directly access to this (doubt it), but worst case you should be able to get to it by digging into the file, assuming you are working with an already-saved file.
If the file is not saved yet, I don't know.
I know this is years old, but the full path of an image that has been drag&dropped into a document is available in the AlternativeText field of the InlineShape. Unfortunately you cannot get this value when it has been inserted with Insert Picture. Images that have been pasted probably vary on whether this is available, e.g. if it was pasted from a document where it was drag&dropped it's probably there, but otherwise it isn't.
This info comes from targeting Word 2010 with VSTO.
Ive been trying for a while now to get a rich textbox to populate from a text file. Normally I would say this is easy and there are tons of solutions on here for it. My only issue is I don't want the open file dialog to populate it I want it to be populated on the onload event of the form, and populate from a known text file on the network drive here. Every solution I find seems to be asking for the users input to open the file I don't want this. This is due to the fact that when the text file is edited( which it will often be) I want the textbox to mimic this rather then have an outdated version hard coded in. Please help
If you know the location then
const string Myfile = #"c:\test\myfile.rtf";
richTextBox1.Loadfile(Myfile);
you can use UNC etc, but it may error if it cant find the file - so I would suggest file checks etc.
Use System.IO.FileSystemWatcher to watch for changes to the file, and then System.IO.File.ReadAllText to read the contents of the file into your textbox.
I am using the Clipboard object to play with data copied to the clipboard. Lets say I have a few applications from which I can copy data to the clipboard like Excel, Notepad, etc.
I want to know whether there is any function or any way to find out from where the data is captured in clipboard, whether it is coming from Excel or Notepad or from some unknown application.
I am using C# and .NET 2.0
If you are using SetClipboardViewer api to detect when something is copied to the clipboard and processing WM_DRAWCLIPBOARD message then you can use GetClipboardOwner function to find handle to the window which initiated the operation. Using the handle you can retrieve the process id and path of the executable.
Based on the Clipboard class reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
You cannot tell exactly which application the content in the clipboard comes from.
You can make some sort of guess work though.
E.g. From the Clipboard.ContainsText Method (TextDataFormat), you can tell if it's an app that outputs/displays HTML, Text, RTF etc.
See http://msdn.microsoft.com/en-us/library/system.windows.forms.textdataformat.aspx
Other than that, I doubt you can go further.