![]() |
|
#1
|
|||
|
|||
![]()
I wrote some VBA code that is not replacing my signature bookmark when I insert it into it.
The bookmark is a single word in the last row of a one column table. When I insert the signature image it appears alongside the bookmark rather than replace it (i.e. the bookmark is not deleted.) Below is my code. Ideas as to where I went wrong? Thanks in advance. Code:
If ActiveDocument.Bookmarks.Exists("Sig1") = True Then ActiveDocument.Bookmarks("Sig1").Range.InlineShapes.AddPicture FileName:="C\Sig1.png" Else ActiveDocument.Bookmarks("Sig2").Range.InlineShapes.AddPicture FileName:="C\Sig2.png" End If Image here Sig1 Instead of Image here ![]() |
#2
|
||||
|
||||
![]()
If you want to delete the bookmark, you need to add code to do just that. For example:
Code:
With ActiveDocument If .Bookmarks.Exists("Sig1") = True Then With .Bookmarks("Sig1") .Range.InlineShapes.AddPicture FileName:="C\Sig1.png" .Delete End With ElseIf .Bookmarks.Exists("Sig2") = True Then With .Bookmarks("Sig2") .Range.InlineShapes.AddPicture FileName:="C\Sig2.png" .Delete End With End If End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
The single word "Sig1" in your column is not the bookmark but the bookmark range. You don't need to delete the bookmark. Just redefine its range and recreate it.
Code:
Dim oILS As InlineShape With ActiveDocument If .Bookmarks.Exists("Sig1") = True Then With .Bookmarks("Sig1") Set oILS = .Range.InlineShapes.AddPicture(FileName:="C:\Sig1.png") .Range.Bookmarks.Add "Sig1", oILS.Range End With ElseIf .Bookmarks.Exists("Sig2") = True Then With .Bookmarks("Sig2") Set oILS = .Range.InlineShapes.AddPicture(FileName:="C:\Sig2.png") .Range.Bookmarks.Add "Sig2", oILS.Range End With End If End With |
#4
|
||||
|
||||
![]()
Hi Greg,
Yes, I thought of that too, but the OP seemed concerned that his bookmark wasn't being deleted as expected. This may have something to do with what is expected to happen if the macro runs a second time.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Hi Paul,
Did you test your method? I didn't and it appears we could both be wrong. It seems that unlike .Range.Text = "Whatever", .Range.InlineShapes.AddPicture is not destructive. The bookmark is not destroyed and the range is expanded to include the inlineshape and previous text. Your .delete just deletes the bookmark leaving the ISL and text intact. My method was no better. Perhaps tjis will meet the OP needs: Code:
Sub Test() Dim oRng As Range With ActiveDocument If .Bookmarks.Exists("Sig1") = True Then With .Bookmarks("Sig1") Set oRng = .Range oRng.Text = vbNullString ActiveDocument.Bookmarks.Add "Sig1", oRng .Range.InlineShapes.AddPicture FileName:="C:\Sig1.png" .Delete 'Delete the bookmark End With ElseIf .Bookmarks.Exists("Sig2") = True Then With .Bookmarks("Sig2") Set oRng = .Range oRng.Text = vbNullString .Range.Bookmarks.Add "Sig2", oRng .Range.Text = vbNullString ActiveDocument.Bookmarks.Add "Sig1", oRng .Range.InlineShapes.AddPicture FileName:="C:\Sig2.png" 'or keep the bookmark End With End If End With End Sub |
#6
|
|||
|
|||
![]()
Greg's final code did the trick. I did not check the other snippets. Thank you all for helping.
|
#7
|
||||
|
||||
![]()
Hi Greg,
I was assuming the bookmark was empty before inserting the InlineShape. In that case, the code I posted should work just fine. That said, the OP has a solution, so NFA required. Generically, the code to update the pic in a bookmarked range would be: Code:
Sub Demo() Dim wdRng As Range, BkMkNm As String BkMkNm = "MyBookmark" With ActiveDocument 'Confirm that the bookmark exists If .Bookmarks.Exists(BkMkNm) Then Set wdRng = .Bookmarks(BkMkNm).Range 'Delete existing contents wdRng.Text = vbNullString 'Insert Pic .Range.InlineShapes.AddPicture FileName:="PictureFullName", Range:=wdRng 'Extend Range wdRng.End = wdRng.End + 1 'Re-apply bookmark .Bookmarks.Add BkMkNm, wdRng End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
Tags |
access 2010, bookmarks, vba code |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Losing image resolution when inserting image into MS word (2011: Mac) | Mario.N | Drawing and Graphics | 0 | 11-23-2014 02:38 AM |
![]() |
sscad27 | Word | 1 | 08-20-2014 12:46 PM |
Problem Inserting Image | rbonnell | PowerPoint | 1 | 07-22-2014 07:15 AM |
Inserting a particular image based on a combobox selection | LeonieD | PowerPoint | 2 | 06-27-2014 05:39 PM |
![]() |
Mijin | Word | 2 | 09-22-2011 03:14 AM |