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().
Related
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.
I have about 500 word documents that I need to change the server name in the Document template path. I am not an expert with VBA but I have tried several solutions that have not worked for me. Is there a way to do this (perhaps with C#, with a foreach loop on the directory?) that I can do a very simple find and replace on this field ?
i.e.
\\ASDCFS\NtierFiles\...
becomes
\\NewServer\NtierFiles\...
You can't write to the field in the dialog box directly. In the object model the equivalent is Document.AttachedTemplate and yes, you can work with that. Using in the object model (whether using VBA or C#) you'd loop the documents in a folder, open each in Word, assign the correct path, save and close.
More efficient and less prone to "hiccups" if the original template path is already invalid, would be to edit the documents' Word Open XML directly, without using the Word application. The Open XML SDK would be a good tool for this. It provides the AttachedTemplate class (https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.attachedtemplate(v=office.14).aspx).
You can use WTC to correct the template path in a bulk of documents. You find the source code and binary on Github: https://github.com/NeosIT/wtc
Is it possible to create an RTF template document and programmatically insert elements like tables or populate variables? If so, what do I need to do this?
I understand that there is a RichTextBox control that can represent the document, but I need to define a template (or more specifically, the sales guys will be defining the template and giving it to me) which I need to then populate with the data. The RichTextBox control is no good to them. We need RTF because not all clients have Word.
As #ToastyMallows suggested, it's come down to a dirty game of replacing strings.
I'm using the following to generate tables and formatting and stuff:
http://www.codeproject.com/Articles/30902/RichText-Builder-StringBuilder-for-RTF
I've done this, but in an old Windows Forms client-side application using Word automation. It works but not elegantly. We defined the templates in WordPad I think, then extracted the RTF which gets stored in a database. There are pre-defined fields that get populated on document creation.
One thing I have discovered in doing RTF stuff, is that the meaning of RichText (the format) and RichText (as in controls) are not necessarily the same.
I have tried using the System.Windows.Documents.FlowDocument server side, but ran into a problem with images.
What I need to produce is a document with headings, section breaks, page breaks, images (with text wrapping around from the left or the right), tables and ideally some kind of table of contents.
I use c# and asp.net.
Is there a library that will do most of this?
RTF has been chosen because the document needs to be openable in older versions of word, be editable, and we can't run word on the server.
Thank-you
I used MigraDoc in the past, it is a free library. You can create PDFs or RTFs. Just Google it.
I have started using .net rtf writer.
It produces clean rtf, but doesn't do everything I need.
There is pretty good documentation for rtf here.
I am working some things out for my self. For example, I needed to be able to wrap text around an image. Whilst the rtf writer above enables you to add images to documents, it does so by putting the image in its own paragraph. What I need is a shape element.
In the rtf it ends up looking something like this (some of the numbers define the size and position of the image in twips):
{\shp{\*\shpinst\shpleft3801\shptop1\shpright8300\shpbottom4500\shpfhdr0\shpbxcolumn\shpbxignore\shpbypara\shpbyignore\shpwr2\shpwrk0\shpfblwtxt0\shpz0
{\sp
{\sn pib}
{\sv
{\pict\pngblip\pichgoal4499\picwgoal4499
-- image binary data goes here --
}}}
{\sp
{\sn fLine}
{\sv 0}}}}
I sometimes just save something in word and try and understand what it did (but word seems to add a lot of noise).
I'm trying to write C# code to Copy-Paste charts from Excel and use Paste Link in MS Word, because with Excel, when you use Paste Link for the tables, text, charts, etc. I can click update all fields in Word after you change anything in Excel.
I was trying to use OpenXML to add content controls in Word and paste the charts to those spots, like what this blogger does.
http://www.dotblogs.com.tw/angus/archive/2010/05/19/15332.aspx
I added content control of the type "pictures" for each chart and they were successfully pasted in but not as links. Thus when I click "update links now" in Word after Excel data change, the charts in Word stay the same.
Maybe I didn't add the right type of content control? I don't know which type I should use for links.
Could any one point me to some source code or even a software?
Have a look here:
VBA: How to Programmatically Insert an Excel Chart into Word.
C#: Automating Word Tables for Data Insertion and Extraction
The magic does PasteSpecial method and its input parameter: Link set to True ;)
For further information, please see: Link or embed an Excel worksheet