如何在MS Word 2010插件的页脚中添加图像

本文关键字:添加 图像 插件 MS Word 2010 | 更新日期: 2024-09-19 12:11:53

我已经使用以下代码将Image添加到Addin中的MS Word标头中。

Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
            Microsoft.Office.Interop.Word.Shape logoCustom = null;
            object oMissing = System.Reflection.Missing.Value;
            object oFalse = false;
            object oTrue = true;
            String logoPath = @"C:'Users'Hasan'Desktop'headers_footers'wordtemplate'logo_wordtemplate_150dpi.jpg";
            logoCustom = Globals.ThisAddIn.Application.Selection.HeaderFooter.Shapes.AddPicture(logoPath,
             ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            logoCustom.Select(ref oMissing);
            logoCustom.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
            logoCustom.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeLeft;
            Globals.ThisAddIn.Application.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;

但我在添加到页脚时遇到了问题。

如何在MS Word 2010插件的页脚中添加图像

试试这个方法:

var range = Globals.ThisAddIn.Application.Selection.HeaderFooter.Range;
var inlineShape = Globals.ThisAddIn.Application.Selection.InlineShapes.AddPicture(sLogo, False, True, range);
var shape = inlineShape.ConvertToShape();
shape.Left = nHPos;
shape.Top = nVPos;
shape.Width = nWidth;
shape.Height = nHeight;

Net4的参数也是可选的,所以你不需要所有的oMissing参数。

您需要更具体地了解添加的图形对象的目标范围。使用Globals.ThisAddIn.Application.Selection.HeaderFooter.Range不会告诉Word是页眉还是页脚,所以Word会做它认为最好的事情。

指定页脚范围:

object oMissing = System.Reflection.Missing.Value;
object oFalse = false;
object oTrue = true;
Word.Section sec = Globals.ThisAddIn.Application.Selection.Sections[1];
Word.HeaderFooter ft = sec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
Word.Range rngFooter = ft.Range;
object oRange = rngFooter;
Word.Shape LogoCustom = ft.Shapes.AddPicture(logoPath, ref oFalse, ref oTrue, 
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                        ref oRange);

请注意,使用Range对象的方法意味着您不需要带有SeekView的行。直接使用Range不会改变选择,这意味着屏幕保持安静,代码执行速度更快。