Bookmark used for range to insert photo

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Bookmark used for range to insert photo

Post by ABabeNChrist »

I have a docm that has numerous bookmarks some are in tables and others are not, I have been using these bookmark to insert photos to selected areas using this code.

Code: Select all

    Dim strBookmark As String
    strBookmark = InputBox("To insert a photo, enter photo location number below, then press OK", "Inspection Photos")
    If Not ActiveDocument.Bookmarks.Exists(strBookmark) Then
    End If
    Set rng = ActiveDocument.Bookmarks(strBookmark).Range
    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
        ActiveDocument.Unprotect ""
    End If
    With Dialogs(wdDialogInsertPicture)
        .Display
        If .Name <> "" Then
            sFileName = .Name

            Set ilImage = ActiveDocument.InlineShapes.AddPicture(sFileName, , True, rng)
            With ilImage
                .Height = Application.InchesToPoints(1.35)
                .Width = Application.InchesToPoints(1.5)
            End With
        End If
    End With
    ActiveDocument.Protect wdAllowOnlyFormFields, True, ""
    ActiveDocument.Bookmarks(strBookmark).Select
Everything seemed or I thought to be working good up until I added “Option Explicit” now I receive an error on this line, with rng highlighted.

Code: Select all

Set rng = ActiveDocument.Bookmarks(strBookmark).Range
Also where would I add a MsgBox if user were to selected the cancel button and No entry on the input box :scratch:

User avatar
HansV
Administrator
Posts: 78527
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Bookmark used for range to insert photo

Post by HansV »

Once you add Option Explicit, you must declare ALL variables in your code. In this example, you need to add

Code: Select all

    Dim rng As Range
    Dim sFileName As String
    Dim ilImage As InlineShape
If you want to exit the macro if the user doesn't enter anything in the InputBox, add the following below the line strBookmark = InputBox(...):

Code: Select all

  If strBookmark = "" Then
    Exit Sub
  End If
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Bookmark used for range to insert photo

Post by ABabeNChrist »

Hi Hans
Thank you
I must have just missed you on line.
I was trying different things today to see if I could figure out how to bypass the errors messages when cancel or no entry was entered.
I was able to come up with a similar approach as you mentioned, so it makes me feel good that at least I was on the right path. See what you think.
I added this before inputbox

Code: Select all

On Error GoTo Canceled
and this just before End Sub

Code: Select all

Canceled:
I also added this after inputbox

Code: Select all

    If StrPtr(strBookmark) = 0 Then
        MsgBox "You selected Cancel"
    Else
        If strBookmark = "" Then
            MsgBox "Nothing was entered when OK was selected"
        Else
        End If
    End If
I also added the changes as you mentioned about declaring all the variables.
And here's the finished product, at least I think sooooooooooooooo :clapping:

Code: Select all

    Dim rng As Range
    Dim sFileName As String
    Dim ilImage As InlineShape
    Dim strBookmark As String
    On Error GoTo Canceled
    strBookmark = InputBox("To insert a photo, enter photo location ID number below, then select OK", "Inspection Photos")
    If StrPtr(strBookmark) = 0 Then
        MsgBox "You selected Cancel"
    Else
        If strBookmark = "" Then
            MsgBox "Nothing was entered when OK was selected"
        Else
        End If
    End If

    If Not ActiveDocument.Bookmarks.Exists(strBookmark) Then
    End If
    Set rng = ActiveDocument.Bookmarks(strBookmark).Range
    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
        ActiveDocument.Unprotect ""
    End If
    With Dialogs(wdDialogInsertPicture)
        .Display
        If .Name <> "" Then
            sFileName = .Name

            Set ilImage = ActiveDocument.InlineShapes.AddPicture(sFileName, , True, rng)
            With ilImage
                .Height = Application.InchesToPoints(1.35)
                .Width = Application.InchesToPoints(1.5)
            End With
        End If
    End With
    ActiveDocument.Protect wdAllowOnlyFormFields, True, ""
    ActiveDocument.Bookmarks(strBookmark).Select
Canceled:

User avatar
HansV
Administrator
Posts: 78527
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Bookmark used for range to insert photo

Post by HansV »

You should probably also add a line

Exit Sub

between the two lines

If Not ActiveDocument.Bookmarks.Exists(strBookmark) Then
End If

(as it is now, this bit of code won't do anything at all)
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Bookmark used for range to insert photo

Post by ABabeNChrist »

Thank You Hans