Help with code to insert photo into bookmark

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

Help with code to insert photo into bookmark

Post by ABabeNChrist »

After I insert a photo using this code

Code: Select all

    Dim strBookmark As String
    strBookmark = InputBox("")
    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, ""
It will then return to the top of document of page 1, this document contains selection breaks.
This can sometimes be a pain, especially if I was working on page 21, I would then have to scroll all the way back down after photo was inserted to bookmark. Is there any way to keep at same location when photo is inserted?

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

Re: Help with code to insert photo into bookmark

Post by HansV »

In a document protected for forms, you can't just go to any place in the document, only to the form fields.

Added: does it help if you add a line

ActiveDocument.Bookmarks(strBookmark).Select

at the end of the macro?
Best wishes,
Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

Hi Hans
I have a toggle button on my tool bar that will protect and unprotect my document so I guess what I’ll do is, remove this line of code that will reapply protection

Code: Select all

    ActiveDocument.Protect wdAllowOnlyFormFields, True, ""
then use my toggle button, if needed to reapply protection.
this should keep me in same location afetr photo is inserted

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

Re: Help with code to insert photo into bookmark

Post by HansV »

I added a suggestion to my original reply - did you see it?
Best wishes,
Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

:thankyou: Hans
I just got it, and yes that seems to work
It does hiccup, but I can live with that.

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

Re: Help with code to insert photo into bookmark

Post by HansV »

You can try adding

Application.ScreenUpdating = False

at the beginning of the macro, and

Application.ScreenUpdating = True

at the end, but this is not perfect.
Best wishes,
Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

Thank you Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

Can this code above be adjusted to target a single bookmark and to remove/replace any current photo at target bookmark.

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

Re: Help with code to insert photo into bookmark

Post by HansV »

1) Change the line

Code: Select all

    strBookmark = InputBox("")
to

Code: Select all

    strBookmark = "SpecificName"
where SpecificName is the name of the bookmark whose picture you want to replace.

2) Above the line With Dialogs(wdDialogInsertPicture), insert the following:

Code: Select all

    Do While rng.InlineShapes.Count
        rng.InlineShapes(1).Delete
    Loop
Best wishes,
Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

When i tried to change the photo it did not delete the previous one, just added a new photo along side

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

Re: Help with code to insert photo into bookmark

Post by HansV »

Try this version:

Code: Select all

    Dim strBookmark As String
    Dim rng As Range
    Dim rng2 As Range
    Dim sFileName As String
    Dim ilImage As InlineShape
    strBookmark = "Test"
    If Not ActiveDocument.Bookmarks.Exists(strBookmark) Then
        Beep
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Set rng = ActiveDocument.Bookmarks(strBookmark).Range
    Set rng2 = ActiveDocument.Range(rng.End, rng.End + 1)
    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
        ActiveDocument.Unprotect ""
    End If
    Do While rng2.InlineShapes.Count
        rng2.InlineShapes(1).Delete
    Loop
    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.Bookmarks(strBookmark).Select
    ActiveDocument.Protect wdAllowOnlyFormFields, True, ""
    Application.ScreenUpdating = True
Best wishes,
Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

That works great thank you

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

I was wondering if the photo size could be adjusted. The height is ok, sometimes the width can vary depending on the photo.

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

Re: Help with code to insert photo into bookmark

Post by HansV »

Do you want to adjust the width according to the aspect ratio of the picture, or do you want a fixed width?
Best wishes,
Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

According to the aspect ratio of the picture

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

Re: Help with code to insert photo into bookmark

Post by HansV »

Change the lines

Code: Select all

            With ilImage
                .Height = Application.InchesToPoints(1.35)
                .Width = Application.InchesToPoints(1.5)
            End With
to

Code: Select all

            With ilImage
                .LockAspectRatio = True
                .Height = Application.InchesToPoints(1.35)
            End With
Best wishes,
Hans

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

Re: Help with code to insert photo into bookmark

Post by ABabeNChrist »

:clapping: Thank you