Excel将文字alignment图片

我有一个从Excel中build立的报告,并在Word中输出,我也有一个名为“图片7”的图片。 我的问题是一旦这个从excel粘贴到word中有没有中间alignment的图片呢?

该图片将作为一系列单元格的一部分进行复制。 所以我需要用word来引用图片。

它以单元格的范围为中心,但并不完全出现在单词文档中

编辑:目前我正在尝试这个

For Each shp In oDoc.Shapes If Left(shp.Name, 7) = "RN Logo" Then shp.Left = wdShapeCenter End If Next 

但是,这只是把图片放在左上angular,我想是因为它粘贴的表格,我可能需要做一个绝对的位置。

编辑2:我find了一个工作,但它只是一个大的如果/绝对定位,片段下面

 Sub Update_RN_Logo_Location() For Each shp In oDoc.Shapes If Left(shp.Name, 7) = "RN Logo" Then If Right(shp.Name, 1) = 1 Then shp.Left = oWord.CentimetersToPoints(2.4) Else shp.Left = oWord.CentimetersToPoints(0.75) End If ElseIf Left(shp.Name, 4) = "UKAS" Then If Right(shp.Name, 1) = 1 Then shp.Left = oWord.CentimetersToPoints(1.25) ElseIf Right(shp.Name, 1) = 2 Then shp.Left = oWord.CentimetersToPoints(2.5) ElseIf Right(shp.Name, 1) = 3 Then shp.Left = oWord.CentimetersToPoints(0) ElseIf Right(shp.Name, 1) = 4 Then shp.Left = oWord.CentimetersToPoints(2.5) End If End If Next End Sub 

一些删除了敏感信息的文件的图片

Word文档中的问题

我相信这里有两个问题。 首先,graphics在词中有一个锚。 粘贴graphics时,将其锚定放置在由Excel单元格创build的表格中。 这抛出了定位。

其次,我build议使用Shape.RelativeHorizontalPosition属性,它将允许Shape.Left属性为您提供相对于另一个页面元素的真正中心alignment方式。

在下面的代码中,我将graphics相对于文档的边距进行了定位,但还有其他select:

Word 2007 WdRelativeHorizo​​ntalPosition枚举

此枚举也适用于Word 2010和2013。

为确保徽标graphics正确放置,请在粘贴徽标和表格之前在文档顶部插入回车符(确保回车没有缩进或应用空间格式的样式):

  Selection.HomeKey Unit:=wdStory Selection.InsertBefore vbCr 

然后粘贴到graphics和表格中,然后运行以下代码:

  For Each shp In oDoc.Shapes If Left(shp.Name, 7) = "RN Logo" Then With shp .Select Selection.Cut Selection.HomeKey Unit:=wdStory Selection.Paste 'places graphic on carriage return before table .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin .Left = wdShapeCenter .RelativeVerticalPosition = wdRelativeVerticalPositionMargin '.Top = measurement of choice End With End If Next 

你有没有尝试简单的中心alignment默认段落(或添加一个文本框),并粘贴在图片? 保持图片与其他一切分开。 您可能还需要设置文本运行。

这是稍微修改了录制的Word VBA

 Sub Macro1() ActiveDocument.Paragraphs.Alignment = wdAlignParagraphCenter ActiveDocument.InlineShapes.AddPicture FileName:= _ "C:\Users\user\Desktop\01.jpg", LinkToFile:=False, _ SaveWithDocument:=True End Sub 

你可以做一个“Control + A”select全部,然后把所有东西都放在中间:

 Sub Testing() 'Select All: Selection.WholeStory 'Center Align All: Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter End Sub 

我宁愿从Excel / Word以外插入图片; 您可以将它们完全放在您想要的段落中:

 Sub M_snb() ActiveDocument.Paragraphs(5).Range.InlineShapes.AddPicture("G:\Excel_.bmp").Range.ParagraphFormat.Alignment = 1 End Sub