none
Word 2010 - 4198 Error on Macro to insert and image, add caption and boarder with a box RRS feed

  • Question

  • Hi Everyone,

    I'm working on a macro to insert a user selected image, format it, put a caption above it, and then put a box around both box and image.  It works great for me.  However, when I send the template the macro is part of to others for testing, they get a 4198 error at the same place in the macro every time.  I'm not sure how to fix it as I've tried breaking the macro into a couple pieces and it still happens at the same point in the code, even if the user manually selects the image to caption and box.  I've included only the portion of the macro where the error is occurring and after it and highlighted where the error occurs.  The only part of the macro missing is the code around inserting the image because I'm not having any issues with it.

    I'm relatively new to working in VBA, so I appreciate any help I can get.  This macro is used by folks both within my organization and by contractors supporting us.

    Thanks - JC

    '   Insert caption and delete number

        Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

            Selection.InsertCaption Label:="<Insert Title Here>", TitleAutoText:="", _

            Title:="", "margin-bottom:0pt;">    Selection.TypeBackspace

        Selection.TypeBackspace

        Selection.TypeBackspace

    '   Change caption font color to black

        Selection.HomeKey Unit:=wdLine, Extend:=wdExtend

        With Selection.Font

            .Name = "Arial Bold"

            .Size = 11

            .Bold = True

            .Italic = False

            .Underline = wdUnderlineNone

            .UnderlineColor = wdColorAutomatic

            .StrikeThrough = False

            .DoubleStrikeThrough = False

            .Outline = False

            .Emboss = False

            .Shadow = False

            .Hidden = False

            .SmallCaps = False

            .AllCaps = False

            .Color = wdColorAutomatic

            .Engrave = False

            .Superscript = False

            .Subscript = False

            .Spacing = 0

            .Scaling = 100

            .Position = 0

            .Kerning = 0

            .Animation = wdAnimationNone

            .Ligatures = wdLigaturesNone

            .NumberSpacing = wdNumberSpacingDefault

            .NumberForm = wdNumberFormDefault

            .StylisticSet = wdStylisticSetDefault

            .ContextualAlternates = 0

        End With

    '   Border caption and picture

        Selection.HomeKey Unit:=wdLine

        Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend

        With Selection.ParagraphFormat

            With .Borders(wdBorderLeft)

                .LineStyle = wdLineStyleSingle

                .LineWidth = wdLineWidth100pt

                .Color = wdColorAutomatic

            End With

            With .Borders(wdBorderRight)

                .LineStyle = wdLineStyleSingle

                .LineWidth = wdLineWidth100pt

                .Color = wdColorAutomatic

            End With

            With .Borders(wdBorderTop)

                .LineStyle = wdLineStyleSingle

                .LineWidth = wdLineWidth100pt

                .Color = wdColorAutomatic

            End With

            With .Borders(wdBorderBottom)

                .LineStyle = wdLineStyleSingle

                .LineWidth = wdLineWidth100pt

                .Color = wdColorAutomatic

            End With

            .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone

            With .Borders

                .DistanceFromTop = 4

                .DistanceFromLeft = 4

                .DistanceFromBottom = 4

                .DistanceFromRight = 4

                .Shadow = False

            End With

        End With

        With Options

            .DefaultBorderLineStyle = wdLineStyleSingle

            .DefaultBorderLineWidth = wdLineWidth100pt

            .DefaultBorderColor = wdColorAutomatic

        End With

        With Selection.ParagraphFormat

            .LeftIndent = InchesToPoints(1.5)

            .RightIndent = InchesToPoints(1.5)

            .SpaceBefore = 0

            .SpaceBeforeAuto = False

            .SpaceAfter = 10

            .SpaceAfterAuto = False

            .Alignment = wdAlignParagraphCenter

            .WidowControl = True

            .KeepTogether = False

            .PageBreakBefore = False

            .NoLineNumber = False

            .Hyphenation = True

            .FirstLineIndent = InchesToPoints(0)

            .OutlineLevel = wdOutlineLevelBodyText

            .CharacterUnitLeftIndent = 0

            .CharacterUnitRightIndent = 0

            .CharacterUnitFirstLineIndent = 0

            .LineUnitBefore = 0

            .LineUnitAfter = 0

            .MirrorIndents = False

            .TextboxTightWrap = wdTightNone

        End With

        Selection.MoveRight Unit:=wdCharacter, Count:=1

        Selection.TypeParagraph

        Selection.Style = ActiveDocument.Styles("Normal")
    Monday, June 27, 2016 5:47 PM

Answers

  • Hi JC,

    Your issue is caused by value for Label. If the label has not yet been defined or it did not exist in Label options, you will get error. You could refer Selection.InsertCaption Method (Word).

    To work around this issue, you could set default Label like Figure, Table. Or, you could create new Labels before set it by CaptionLabels.Add Method (Word).

      CaptionLabels.Add "New Label"
    
            Selection.InsertCaption Label:="New Label", TitleAutoText:="", _
    
            Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 21, 2016 8:49 AM

All replies

  • Hi JC,

    I insert an image in Word, select it, then run below code, it works correctly.

    Sub Image()
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.InsertCaption Label:="Figure", _
     Title:=": Sales Results", Position:=wdCaptionPositionBelow
    Selection.TypeBackspace
    End Sub

    >> even if the user manually selects the image to caption and box. 

    Based on this and my test, I assume your issue is related with how you insert the image or the image object. To narrow down this issue, I would suggest you make some tests below:

    1. Manually insert image, and then manually select the image to caption and box
    2. Manually insert image, and then manually select the image, use VBA code to caption and box
    3. Test with other images to check whether it is related with the special image.

    It would be helpful if you could share us a simple template with this macro and an image, then we could test at our side.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 28, 2016 5:23 AM
  • Hi Edward,

    One of the challenges I am facing with this is that the template will be used by multiple people and will involve inserting a variety of different images within a single document.  This means that there isn't a single special image.  The image is often a JPG of a PPT slide or something similar.  We've been testing with the sample images that are part of the Windows installs.

    I've had others try each of the tests you listed, and they still get the error.  I'm not really sure how to share a template and image here aside from pasting the code and image into a message.  Sorry, I'm just new to the forum and trying to do all this. How do I go about doing that?

    Thanks,

    JC

    Tuesday, July 5, 2016 2:34 PM
  • Hi JC,

    >> I've had others try each of the tests you listed, and they still get the error

    Do you mean you still get error when you manually operate the cation and box?

    >> One of the challenges I am facing with this is that the template will be used by multiple people and will involve inserting a variety of different images within a single document

    To narrow down this issue, I would suggest you create a new template, and only share with one people to check whether it is related with multiple people sharing.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, July 6, 2016 6:49 AM
  • Hi Edward,

    Sorry, I wasn't very clear.  Everything works fine when manually inserting the picture and then manually inserting the caption and box.

    At this time, there is only one person other than myself testing the template.  The template works as intended every time I test it.  When I email it to her, everything works as intended except this one macro.

    >> 2. Manually insert image, and then manually select the image, use VBA code to caption and box

    The macro works as intended for me, but she continues to get the run-time error.  This happens when I write the macro to start at the captioning portion instead of the inserting an image portion.

    >> 3. Test with other images to check whether it is related with the special image.

    It doesn't matter what image she chooses to use with the VBA code.  She gets the same error.

    I have tried testing this with other testers also in case there was something with her particular machine or account, and we get the same error.

    Thanks for your help and patience.

    JC

    Thursday, July 7, 2016 12:58 PM
  • Hi JC,

    Do you mean all got this error except you? What is others’ Word version? Do you mind share us your simple word file which could reproduce your issue at other’s computers?

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, July 9, 2016 4:17 AM
  • Hi Edward,

    Yes, I don't receive the error, but other users do.  They are using the same version of Word that I am.

    I don't mind sharing a Word file with just that macro in it.  How do I share it with you here?  I don't see how to include an attachment.

    JC

    Thursday, July 14, 2016 3:17 PM
  • Hi JC,

    You could upload your file to OneDrive, and then share us link here.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, July 15, 2016 3:14 AM
  • Hi Edward,

    I apparently can't include any links in this until my account is verified.  This is the message I get when I try to insert a link to the file on OneDrive - "Body cannot contain links or images until we are able to verify your account."

    Thanks,

    JC

    Tuesday, July 19, 2016 7:13 PM
  • Hi JC,

    I think you could insert a link like "www.google.com" which removes "https://"

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, July 20, 2016 1:23 AM
  • Hi Edward,

    Let's try this. 1drv.ms/w/s!AtJtdFw7SjGmazxrJLJz42MYRCs

    Thanks,

    John

    Wednesday, July 20, 2016 10:33 AM
  • Hi JC,

    Your issue is caused by value for Label. If the label has not yet been defined or it did not exist in Label options, you will get error. You could refer Selection.InsertCaption Method (Word).

    To work around this issue, you could set default Label like Figure, Table. Or, you could create new Labels before set it by CaptionLabels.Add Method (Word).

      CaptionLabels.Add "New Label"
    
            Selection.InsertCaption Label:="New Label", TitleAutoText:="", _
    
            Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 21, 2016 8:49 AM
  • Hi Edward,

    That tentatively looks like the fix I needed.  I'll let you know for sure after more folks test it.

    Thanks,

    John

    Thursday, July 21, 2016 7:36 PM
  • This is the first post on this issue that explains the problem and the fix.  I have been trying to add a custom Figure caption using VBA and getting the 4198 error.  I added the custom label and everything worked.
    Wednesday, October 31, 2018 12:34 PM