2
votes

I have made a simple VBA macro in Word 2013 and everything works fine. When I am trying to run it in Word 2010, it quits with an run-time error no. 4198.

The working code, which inserts multiple pictures in MS Word 2013 is the following:

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

End Sub

The bit, which fails to run:

'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

Could you please tell me what is wrong here? I suspect that InsertCaption method is not working properly in MS Word 2010; however, I could not find any documentation on this.

1
Welcome to SO! Can you include the error message?David Rushton

1 Answers

1
votes

The problem, I believe, is that at the beginning of your code you create a caption label "Photograph", but in the section where you actually insert the caption you use the label "Picture". (That, by the way, is also what's in the comment for creating the label...)

In Word 2013 it's possible a caption named "Picture" is already present, which is why you aren't seeing the error in 2013. Apparently, it's not already there in 2010, thus the error.