现在的问题是:所有图片都粘贴为 wrap.format.type = wdInLine 另一个人需要稍后在他的模板中添加单词 doc:插入 - 对象 - 文件中的文本,然后所有图片都无法正确显示。它们被包裹在书签中。
如果我为每张图片手动将 wdInline 更改为 wdWrapTopBottom,这又可以了。
这是我的代码:
Sub excelToWord()
Dim xlApp As Object
Dim xlDoc As Object
Dim wdApp As Object
Dim wdDoc As Object
'late binding needs constants
Const wdOutlineView = 2
Const wdPrintView = 3
Const wdNormalView = 1
Const wdSeekCurrentPageHeader = 9
Const wdSeekCurrentPageFooter = 10
Const wdFieldEmpty = -1
Const wdSeekMainDocument = 0
Const wdAlignParagraphCenter = 1
Const wdAlignParagraphRight = 2
Const wdGoToBookmark = -1
Const wdGoToHeading = 11
Const wdPageBreak = 7
Const wdGoToNext = 2
Const wdGoToPage = 1
Const wdCollapseEnd = 0
Const wdCollapseStart = 1
Const wdPasteMetafilePicture = 3
Const wdPasteEnhancedMetafile = 9
Const wdPasteShape = 8
Const wdFloatOverText = 1
Const wdInlineShapePicture = 3
Const wdPaneNone = 0
Set xlApp = GetObject(, "Excel.Application")
Set wdApp = GetObject(, "Word.Application")
wdApp.Visible = True
Set xlDoc = ActiveWorkbook
repeat: 'normally for each .. in ..
xlDoc.Activate
'I make some selection I would like to paste as picture in word.
Range("A1:B2").Select 'dummy in this case
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
lWidth = Selection.Width
lHeight = Selection.Height
'with a picker I pick a word file 'myFile'
Set wdDoc = wdApp.Documents.Open(myFile)
wdDoc.Activate
With wdApp
.Visible = True
If Page = 0 Then
.Selection.Goto what:=wdGoToBookmark, Name:="input"
.Selection.Collapse Direction:=wdCollapseEnd
'only first time insert Break
.Selection.InsertBreak (wdPageBreak)
.Selection.InsertLine
.Selection.InsertAfter Chr(13)
.Selection.TypeText Text:=" Excel Input"
End If
.Selection.InsertLine
.Selection.InsertAfter Chr(13)
.Cursor.MoveDown Unit:=wdLine, Count:=1
.Selection.TypeText Text:=Chr(13)
.Cursor.MoveDown Unit:=wdLine, Count:=1
.Selection.PasteSpecial Link:=False, DisplayAsIcon:=False, _
DataType:=wdPasteEnhancedMetafile, Placement:=wdInline 'Placement:=wdInLine
'The problem now is from here. All pictures are pasted as wrap.format.type = wdInLine
' another person needs to add the word doc later in his template ( I can not change this template-no access).
' with insert - object - text from file
'and then all pictures are not shown correctly anymore. they are wrapped in the bookmark.
'If I change the wdInline into wdWrapTopBottom manually this is ok again.
'this is what I tried but it does not select the last pasted picture. myPicture is just empty. so no change is made.
'Selection.MoveLeft Unit:=wdLine, Count:=1, Extend:=wdExtend
Set myPicture = Selection.InlineShapes(1)
Set MyShape = myPicture.ConvertToShape
With MyShape.WrapFormat
.Type = wdWrapTopBottom
.AllowOverlap = False
End With
End With
Page = Page + 1
'repeat for next xl copy picture..... etc
GoTo repeat 'normally next...
End Sub