2015-03-02 213 views
0

我有一個Excel例程,用於將數據從Excel文件傳輸到Word文檔中。該程序在將文件保存爲Office 97-2003時起作用,但在將文件更新到Office 2010時崩潰。該問題涉及將文本框放在照片上並添加標題的編程部分。這部分編程從Excel中調用,但子例程在Word中。調整圖像大小的子程序「ResizePic」有效,但子程序「AddPictureBox」不適用。有人可以向我提供可在Office 2010和2013中使用的代碼。我不在乎它是否仍能在早期版本中使用。請注意,我最初並沒有寫這段代碼,我也不是高級用戶。只顯示CreateDocumment子例程的相關部分。此代碼「Selection.CreateTextbox」適用於單詞版本93-2001文件,但不適用於單詞2010文件

sub CreateReport() 
Set wdApp = GetObject("", "Word.Application") 
wdApp.Documents.Open FileName:=strDefaultPath & "\tempReport.doc", ReadOnly:=True 
Excel.Sheets("Export").Activate 
'add line items from Excel 
i = 1 
Do Until IsEmpty(Excel.Sheets("Export").Cells(i, 5)) 
wdApp.Selection.Goto What:=-1, Name:="WorkItemList" 
strItemName = Excel.Sheets("Export").Range("b" & i).Value 
wdApp.Selection.Style = wdApp.activedocument.Styles("Heading 3") 
wdApp.Selection.TypeText Text:=strItemName 
wdApp.Selection.InlineShapes.AddPicture FileName:=Excel.Sheets("Export").Range("a" & i).Text, LinkToFile:=False, SaveWithDocument:=True 
If Excel.Sheets("Export").Range("a" & i).Value <> "" Then 
wdApp.Selection.InlineShapes.AddPicture FileName:=Excel.Sheets("Export").Range("a" & i).Text, LinkToFile:=False, SaveWithDocument:=True 
End If 
wdApp.Selection.TypeParagraph 
If Excel.Sheets("Export").Range("c" & i).Value > 1 Then 
    strItemName = Excel.Sheets("Export").Range("c" & i).Value 
    wdApp.Selection.Style = wdApp.activedocument.Styles("Body Text") 
    wdApp.Selection.TypeText Text:=strItemName 
    wdApp.Selection.TypeParagraph 
End If 
i = i + 1 
Loop 
wdApp.activedocument.ResizePic 
wdApp.activedocument.AddPictureBox 

以下子程序是在Word中的「wdApp.activedocument.AddPictureBox」文件

Sub ResizePic() 
NumPic = ActiveDocument.InlineShapes.Count 
For i = 1 To NumPic 
origWidth = ActiveDocument.InlineShapes(i).Width 
origHeight = ActiveDocument.InlineShapes(i).Height 
scaleVal = (200/origWidth) 
With ActiveDocument.InlineShapes(i) 
    .Height = origHeight * scaleVal 
    .Width = origWidth * scaleVal 
End With 
Next i 
End Sub 


Sub AddPictureBox() 
NumPic = ActiveDocument.InlineShapes.Count 
Dim currentText As Variant 
For i = 1 To NumPic 
ActiveDocument.InlineShapes(1).Select 
Selection.CreateTextbox 
Selection.ShapeRange.Fill.Visible = msoFalse 
Selection.ShapeRange.Line.Visible = msoFalse 
Selection.ShapeRange.LockAspectRatio = msoFalse 
Selection.ShapeRange.Height = 180 
Selection.ShapeRange.Width = 200 
Selection.ShapeRange.TextFrame.MarginLeft = 0 
Selection.ShapeRange.TextFrame.MarginRight = 0 
Selection.ShapeRange.TextFrame.MarginTop = 3.69 
Selection.ShapeRange.TextFrame.MarginBottom = 3.69 
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn 
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionLine 
Selection.ShapeRange.Left = wdShapeRight 
Selection.ShapeRange.Top = wdShapeTop 
Selection.ShapeRange.LockAnchor = True 
Selection.ShapeRange.WrapFormat.AllowOverlap = True 
Selection.ShapeRange.WrapFormat.Side = wdWrapBoth 
Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0) 
Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0) 
Selection.ShapeRange.WrapFormat.DistanceLeft = CentimetersToPoints(0.32) 
Selection.ShapeRange.WrapFormat.DistanceRight = CentimetersToPoints(0.32) 
Selection.ShapeRange.WrapFormat.Type = wdWrapSquare 
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight 
'insert caption 
Selection.MoveRight Unit:=wdCharacter, Count:=1 
Selection.TypeParagraph 
Selection.TypeText Text:="Caption " & i 
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend 
Selection.Style = ActiveDocument.Styles("Caption") 
Next I 

Excel的代碼崩潰,從我所知道的,碼字的崩潰「的選擇。 CreateTextbox「

我將不勝感激任何幫助。

回答

0

因此,作爲一個初學者,我確實看到2件事。

  1. 失蹤End Sub陳述在AddPictureBox()的底部。我假設這是一個複製/粘貼問題。
  2. 此行:ActiveDocument.InlineShapes(1).Select,正好在for循環的開始處,索引1,而不是i。這可能是它吹起來的原因,可能在第二回合。

編輯:所以我放置(一次一個)一個GIF,JPG和PNG一句話文件內。單步執行代碼,會得到一個運行時錯誤5無效的過程調用或參數。這種情況發生在這個聲明上:Selection.ShapeRange.Fill.Visible = msoFalse

我可能會把我放在Word文檔中的圖片放在兔子洞裏,所以我會退出。我建議你通過你的代碼,看看你的錯誤,然後谷歌提示。

祝你好運

相關問題