编辑:该问题的文本已更改为反映使用开放xml代码和互操作的情况。
我正在尝试通过功能区将基数为64的编码图像插入Word文档。以下代码用于复制目的:
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void InsertPicture_Click(object sender, RibbonControlEventArgs e)
{
Word.Application wordApp = null;
Word.Document currentDocument = null;
Word.ContentControls controls = null;
try
{
wordApp = (Word.Application) Marshal.GetActiveObject("Word.Application");
currentDocument = wordApp.ActiveDocument;
controls = currentDocument.ContentControls;
currentDocument.Range().InsertXML(@"<pkg:package xmlns:pkg=""http://schemas.microsoft.com/office/2006/xmlPackage"">
<pkg:part pkg:name=""/word/media/image1.png"" pkg:contentType=""image/png"" pkg:compression=""store"">
<pkg:binaryData>iVBORw0KGgoAAAANSUhEUgAAABEAAAAKCAIA
AADdHiL1AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSURBVChTY3gro0IqGtUz3PTIqAAAlO/H4+qBWxcAAAAASUVORK5CYII=</pkg:binaryData>
</pkg:part></pkg:package>");
object tr = true;
object fa = false;
}
catch(Exception ex)
{
wordApp.ActiveDocument.Range().InsertAfter(ex.Message);
}
finally
{
if (controls != null) Marshal.ReleaseComObject(controls); controls = null;
if (currentDocument != null) Marshal.ReleaseComObject(currentDocument); currentDocument = null;
if (wordApp != null) Marshal.ReleaseComObject(wordApp); wordApp = null;
}
}
}
但是,每当我执行此代码时,我都会抓住问题,错误是:“ XML标记无法插入指定的位置。”我知道此错误具有误导性,因为如果我将xml更改为<Test>Test</Text>
,则会在文档中看到“测试”。谁能对此有所启发?
请注意,所使用的图像只是一个约10像素x 10像素的红色正方形
答案 0 :(得分:0)
您将需要图像在文件系统上可用,并且需要使用Shapes.AddPicture
对象的ActiveDocument
方法。您可以在调用此方法时设置图像位置及其大小。
currentDocument.Shapes.AddPicture (imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 250, 250);
有关更多信息,请参考此URL:
https://docs.microsoft.com/en-us/office/vba/api/word.shapes.addpicture
这是工作代码:
private void InsertPicture_Click(object sender, RibbonControlEventArgs e)
{
Word.Application wordApp = null;
Word.Document currentDocument = null;
Word.ContentControls controls = null;
try
{
wordApp = (Word.Application) Marshal.GetActiveObject ("Word.Application");
currentDocument = wordApp.ActiveDocument;
controls = currentDocument.ContentControls;
string imagePath = @"D:\WordAddInTest\App_Data\Yay.jpg";
currentDocument.Shapes.AddPicture (imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 250, 250);
// currentDocument.Range ().InsertXML (@"<pkg:package xmlns:pkg=""http://schemas.microsoft.com/office/2006/xmlPackage"">
//<pkg:part pkg:name=""/word/media/image1.png"" pkg:contentType=""image/png"" pkg:compression=""store"">
// <pkg:binaryData>iVBORw0KGgoAAAANSUhEUgAAABEAAAAKCAIA
// AADdHiL1AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSURBVChTY3gro0IqGtUz3PTIqAAAlO/H4+qBWxcAAAAASUVORK5CYII=</pkg:binaryData>
//</pkg:part></pkg:package>");
object tr = true;
object fa = false;
}
catch (Exception ex)
{
wordApp.ActiveDocument.Range ().InsertAfter (ex.Message);
}
finally
{
if (controls != null) Marshal.ReleaseComObject (controls); controls = null;
if (currentDocument != null) Marshal.ReleaseComObject (currentDocument); currentDocument = null;
if (wordApp != null) Marshal.ReleaseComObject (wordApp); wordApp = null;
}
}
使用上述代码收到的输出:
希望这会有所帮助!