2015-12-21 47 views
2

我在Word 2013中制作了一个简单的VBA宏,并且一切正常。 当我试图在Word 2010中运行它时,它退出时出现运行时错误号。 4198.插入标题词vba 2010

工作的代码,它插入在MS Word 2013多张照片如下:

Sub AddPics() 

Application.ScreenUpdating = False 

Dim oTbl As Table, i As Long, j As Long, k As Long, StrTxt As String 

Dim MarginLeft As Long, MarginRight As Long, TopDist As Long, BottomDist As Long 

Dim NCols As Long, NRows As Long, TotalRows As Long 
'Number of Columns and Rows of Pictures per page, total number of Rows in the table 

Dim CaptionHeight As Long 


NCols = 1 
NRows = 2 

CaptionHeight = CentimetersToPoints(0.7) 

'Select and insert the Pics 
With Application.FileDialog(msoFileDialogFilePicker) 
    .Title = "Select image files and click OK" 
    .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png" 
    .FilterIndex = 2 

    If .Show = -1 Then 

     'Add a 'Picture' caption label 
     CaptionLabels.Add Name:="Photograph" 
     'Add a 1-row by N-column table with adjusted columns to take the images 
     TotalRows = Round(.SelectedItems.Count/NCols) * 2 

     Set oTbl = Selection.Tables.Add(Selection.Range, TotalRows, NCols) 

     For i = 1 To TotalRows 
      With oTbl.Rows(i) 
       If ((i Mod 2) = 1) Then 
         .Height = (ActiveDocument.PageSetup.PageHeight - ActiveDocument.PageSetup.TopMargin - ActiveDocument.PageSetup.BottomMargin - NRows * CaptionHeight)/NRows 
         .HeightRule = wdRowHeightExactly 
       Else 
         .Height = CaptionHeight 
         .HeightRule = wdRowHeightExactly 
       End If 
      End With 
     Next i 
     'This loop has created a table 

     i = 1 

     For k = 1 To .SelectedItems.Count 


      'Insert the Picture 
      ActiveDocument.InlineShapes.AddPicture FileName:=.SelectedItems(k), _ 
      LinkToFile:=False, SaveWithDocument:=True, _ 
      Range:=oTbl.Cell(i, NCols - (k Mod NCols)).Range.Characters.First 

      'Get the Image name for the Caption 
      StrTxt = Split(.SelectedItems(k), "\")(UBound(Split(.SelectedItems(k), "\"))) 
      StrTxt = ": " & Split(StrTxt, ".")(0) 

      **'Insert the Caption in the cell below the picture 
      With oTbl.Rows(i + 1).Cells(NCols - (k Mod NCols)).Range 
       .InsertBefore vbCr 
       .Characters.First.InsertCaption _ 
       Label:="Picture", Title:=StrTxt, _ 
       Position:=wdCaptionPositionBelow, ExcludeLabel:=False 
       .Characters.First = vbNullString 
       .Characters.Last.Previous = vbNullString 
      End With** 

      'Jump along the rows 
      If k Mod NCols = 0 Then 
       i = i + 2 
      End If 

     Next k 

     For Each oCell In oTbl.Range.Cells 
      oCell.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter 
     Next oCell 

    Else 
    End If 

End With 
Application.ScreenUpdating = True 

结束子

所述的钻头,其无法运行:

'Insert the Caption in the cell below the picture 
      With oTbl.Rows(i + 1).Cells(NCols - (k Mod NCols)).Range 
       .InsertBefore vbCr 
       .Characters.First.InsertCaption _ 
       Label:="Picture", Title:=StrTxt, _ 
       Position:=wdCaptionPositionBelow, ExcludeLabel:=False 
       .Characters.First = vbNullString 
       .Characters.Last.Previous = vbNullString 

      End With 

你能告诉我这里有什么问题吗? 我怀疑InsertCaption方法在MS Word 2010中无法正常工作;但是,我找不到任何有关这方面的文档。

+0

欢迎来到SO!你能否包含错误信息? –

回答

1

的问题,我认为,这是在你的代码的开头创建一个标题标签“照片”,但在你真正插入使用标签“图片”的标题部分。 (顺便说一句,创建标签的注释也是如此......)

在Word 2013中,可能会出现名为“图片”的标题,这就是为什么您没有看到错误在2013年。显然,它不是在2010年,因此错误。