如何在MS Word 2010 Addin的页脚中添加图像?

时间:2016-04-17 07:48:24

标签: c# ms-word vsto add-in

我已使用以下代码将图像添加到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;

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

2 个答案:

答案 0 :(得分:1)

尝试这种方式:

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;

对于.Net 4,参数是可选的,因此您不需要所有这些oMissing参数。

答案 1 :(得分:1)

您需要更加具体地了解您添加的图形对象的目标范围。使用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不会改变选择,这意味着屏幕保持更安静,代码执行速度更快。