VBA Replace bookmarked image

SkylabOne

New Member
Joined
Oct 22, 2013
Messages
21
Hello MVP's,

I believe this to be quite easy, however, I just don't know the right syntax for it.

My question is about the following line: Rng.Text = vbNullString '<-- This only deletes text, not images

Sometimes I want to replace an existing client image, however, the code posts an additional image everytime, while it should delete the existing one.
Perhaps I haven't inserted the image into the bookmark, but instead after it.

I have tried the following, which didn't work: Set Rng = ActiveDocument.Bookmarks("Client_logo").Range.InsertBefore

Code:
Sub AutoLogo()
[COLOR=#008000]
''''''''''''''''''''''''''''''''''''''''''''''''''''
'Populate textbox: txtLogo
''''''''''''''''''''''''''''''''''''''''''''''''''''[/COLOR]

[COLOR=#008000]'Establish connection[/COLOR]
Set dbs = OpenDatabase("C:\Users\Public\Downloads\Contacts.accdb")

[COLOR=#008000]'Fill recordset with data matching the text of combo cboContacts[/COLOR]
Set rst = dbs.OpenRecordset("Select * FROM Projects WHERE Projects.Project_Number & '   ' & Projects.Project_Title = '" & Me.cboContacts.Text & "';")
    
[COLOR=#008000]'Fill Textbox with Hyperlink from Databasa[/COLOR]
txtLogo.Value = ("" & rst.Fields("Hyperlink"))


[COLOR=#008000]''''''''''''''''''''''''''''''''''''''''''''''''''''
'Fill 'Client_logo' bookmark in document header
''''''''''''''''''''''''''''''''''''''''''''''''''''[/COLOR]

Dim ClientLogo As InlineShape
Dim Rng As Range

[COLOR=#008000]'Identify current Bookmark range and insert image[/COLOR]
Set strBM = txtLogo
    With ActiveDocument
        Set Rng = ActiveDocument.Bookmarks("Client_logo").Range
            
            Rng.Text = vbNullString [COLOR=#b22222]'<-- [B]This only deletes text, not images[/B][/COLOR]
                Set ClientLogo = .InlineShapes.AddPicture(FileName:=strBM, LinkToFile:=False, Range:=Rng)
                    With ClientLogo
                        .LockAspectRatio = msoTrue
                        .Width = CentimetersToPoints(3.5)
                        .Height = CentimetersToPoints(1.25)
                    End With
                
                [COLOR=#008000]'Re-insert the bookmark[/COLOR]
                .Bookmarks.Add "Client_logo", Rng
    End With

End Sub

Thank you for looking into my problem.

Kind regards,
Rolf
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I solved the problem. For those who are interested:

This code replaces an existing image (including those in header and footer, if that's where your bookmark is) in a certain bookmark with another based on the selection in a database.

Code:
With ActiveDocument

[COLOR=#008000]        'Identify current Bookmark range
[/COLOR]        If .Bookmarks.Exists("Client_logo") Then
            Set Rng = ActiveDocument.Bookmarks("Client_logo").Range
            With Rng
[COLOR=#008000]                'Delete existing image (if applicable)[/COLOR]
                While .InlineShapes.Count > 0
                    .InlineShapes(1).Delete
                Wend
            End With
[COLOR=#008000]            'Place new image[/COLOR]
            Set ClientLogo = .InlineShapes.AddPicture(FileName:=strBM, LinkToFile:=False, Range:=Rng)
        End If

[COLOR=#008000]        'Resize image[/COLOR]
        With ClientLogo
            .LockAspectRatio = msoTrue
            .Width = CentimetersToPoints(3.5)
            .Height = CentimetersToPoints(1.25)
        End With
                
[COLOR=#008000]        'Re-insert the bookmark[/COLOR]
        .Bookmarks.Add "Client_logo", Rng

End With

It wasn't so difficult after all, but perhaps someone else wants to know the answer in the future.
 
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,277
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top