如何将文件嵌入Word docx? [英] How to embed file to word docx?
问题描述
我试图以编程方式(例如* .zip)将文件插入现有的docx文件。
I'm trying to insert a file programmatically (*.zip for example) into an existing docx file.
我查看了docx开放库,但它没有
I looked at the docx open library but it doesn't have the function there.
也尝试使用Microsoft.Office.Interop.Word。我创建了一个带有表的Word文档,并且试图将文件插入到表内的单元格中。
Also tried using Microsoft.Office.Interop.Word. I created a word document with a table, and I'm trying to insert a file into a cell inside the table.
Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
Word.Document doc = new Word.Document();
doc = wordApp.Documents.Open(Environment.CurrentDirectory + "\\test.docx");
doc.Tables[1].Rows[2].Cells[1].Range.InsertFile((Environment.CurrentDirectory + "\\tttt.zip"));
但它导致错误:
文件似乎已损坏
"The file appears to be corrupted"
任何人都能从中获得经验和帮助吗?
Can anyone have experience and help with this?
推荐答案
经过多次试验和错误...
函数 Range.InsertFile实际上并未插入文件,它将读取文本并将其追加到范围中。
After a lot of trial and error... The function "Range.InsertFile" doesn't actually inserts a file, it reads and appends the text into the Range.
解决方案很简单-复制粘贴...
The solution was simple - Copy paste...
using System.Collections.Specialized;
...
...
//Copy the Filename to Clipboard (setFile function uses StringCollection)
StringCollection collection = new StringCollection();
collection.Add(Environment.CurrentDirectory + "\\MyFile.zip");
Clipboard.SetFileDropList(collection);
//Paste into the selected Range.
range.Paste();
*还有 PasteSpecial功能不起作用(仅支持特定数据类型)
*There is also function "PasteSpecial" which didn't work (Only specific data types are supported).
这篇关于如何将文件嵌入Word docx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!