vba - 插入标题词 vba 2010

标签 vba ms-word caption word-2010

我在 Word 2013 中制作了一个简单的 VBA 宏,一切正常。 当我尝试在 Word 2010 中运行它时,它退出并显示运行时错误号。第4198章

在 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 中无法正常工作;但是,我找不到任何相关文档。

最佳答案

我认为,问题在于,在代码的开头,您创建了一个标题标签“照片”,但在实际插入标题的部分中,您使用了标签“图片”。 (顺便说一下,这也是创建标签的注释中的内容......)

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

关于vba - 插入标题词 vba 2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34392088/

相关文章:

excel - 做直到什么都没找到

vba - 加快 VBA 代码运行速度

excel - 如何复制单元格格式和样式

opencv - OpenCV中的Word文档

html - 以当今现代方式使用 "caption"HTML 元素的最合适方式是什么?

excel - VBA Excel在下拉框上设置焦点

vba - 重复 Microsoft Word VBA 直到找不到搜索结果

vba - 将电子邮件正文保存到 Word 文档

delphi分组框标题颜色变化

python - 没有用户 OAuth 过程的 YouTube API