C B*_*uer 10 c# add-in ms-word openxml office-interop
编辑:此问题的文本已更改为反映利用开放的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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,每当我执行此代码时,我都会遇到catch,错误是:"无法在指定位置插入XML标记.".我知道这个错误会产生误导,因为如果我将xml更改为<Test>Test</Text>my,请在我的文档中看到"Test".任何人都可以对此有所了解吗?
请注意,使用的图像只是一个大约10px x 10px的红色正方形
您需要在文件系统上提供图像,并且需要使用对象Shapes.AddPicture的方法ActiveDocument。您可以在调用此方法时设置图像位置及其大小。
currentDocument.Shapes.AddPicture (imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 250, 250);
Run Code Online (Sandbox Code Playgroud)
请参阅此网址了解更多信息:
https://learn.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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用上述代码收到的输出:
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
188 次 |
| 最近记录: |