2016-04-17 65 views
1

我已经使用下面的代码将图像添加到Addin中的MS Word标题中。如何在MS Word 2010 Addin的页脚中添加图像?

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; 

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

回答

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参数。

+0

我粘贴上面的代码工作正常的标题,但我也想在页脚中添加图像。我怎么可以做到这一点 –

1

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

要指定页脚范围:即使用Range对象的方法意味着你需要SeekView

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); 

注意。直接使用范围不会改变选择,这意味着屏幕保持安静,代码执行更快。

+0

嘿@ cindy-meister谢谢你的回复,但事情是, 'Word.Footer ft'是Word没有任何页脚属性。你能帮忙吗? –

+0

啊,对不起,我的错字:'Word.HeaderFooter ft' –

+0

嘿,但代码再次添加标题中的图像。你能重新检查代码吗? –