Save text from MS Word document from clipboard to variable and back with Interop

0

I have this code that take all MS Word document, save it to clipboard, save clipboard to variable a finally put it back to clipboard. It all run in BackgroundWorker, but that should not be problem.

wordDoc.StoryRanges[WdStoryType.wdMainTextStory].Copy();
IDataObject originalWordDocument = null;
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => originalWordDocument = Clipboard.GetDataObject()));
...
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => Clipboard.SetDataObject(originalWordDocument)));

The last line - specifically Clipboard.SetDataObject(originalWordDocument) throws this error:

Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))

And this

originalWordDocument.GetFormats();

return array that contains

[0] "Object Descriptor" string
[1] "Rich Text Format"  string
[2] "HTML Format"   string
[3] "Text"  string
[4] "UnicodeText"   string
[5] "System.String" string
[6] "EnhancedMetafile"  string
[7] "System.Drawing.Imaging.Metafile"   string
[8] "MetaFilePict"  string
[9] "Embed Source"  string
c#
ms-word
interop
clipboard
asked on Stack Overflow May 17, 2016 by Sonny

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0