Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-19-2015, 02:50 PM
newbieX newbieX is offline Bookmark not replaced when inserting but to lower right corner of image Windows 7 64bit Bookmark not replaced when inserting but to lower right corner of image Office 2007
Novice
Bookmark not replaced when inserting but to lower right corner of image
 
Join Date: Mar 2014
Posts: 6
newbieX is on a distinguished road
Default Bookmark not replaced when inserting but to lower right corner of image

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
Thus it might look like this if bookmark = Sig1

Image here Sig1

Instead of

Image here

Reply With Quote
  #2  
Old 11-19-2015, 07:13 PM
macropod's Avatar
macropod macropod is offline Bookmark not replaced when inserting but to lower right corner of image Windows 7 64bit Bookmark not replaced when inserting but to lower right corner of image Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 11-19-2015, 09:10 PM
gmaxey gmaxey is offline Bookmark not replaced when inserting but to lower right corner of image Windows 7 32bit Bookmark not replaced when inserting but to lower right corner of image Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 11-19-2015, 09:18 PM
macropod's Avatar
macropod macropod is offline Bookmark not replaced when inserting but to lower right corner of image Windows 7 64bit Bookmark not replaced when inserting but to lower right corner of image Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 11-20-2015, 06:09 AM
gmaxey gmaxey is offline Bookmark not replaced when inserting but to lower right corner of image Windows 7 32bit Bookmark not replaced when inserting but to lower right corner of image Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 11-20-2015, 10:50 AM
newbieX newbieX is offline Bookmark not replaced when inserting but to lower right corner of image Windows 7 64bit Bookmark not replaced when inserting but to lower right corner of image Office 2007
Novice
Bookmark not replaced when inserting but to lower right corner of image
 
Join Date: Mar 2014
Posts: 6
newbieX is on a distinguished road
Default

Greg's final code did the trick. I did not check the other snippets. Thank you all for helping.
Reply With Quote
  #7  
Old 11-20-2015, 02:03 PM
macropod's Avatar
macropod macropod is offline Bookmark not replaced when inserting but to lower right corner of image Windows 7 64bit Bookmark not replaced when inserting but to lower right corner of image Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
access 2010, bookmarks, vba code

Thread Tools
Display Modes


Similar Threads
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
Bookmark not replaced when inserting but to lower right corner of image Image blurry when inserting 72dpi JPEGS 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
Bookmark not replaced when inserting but to lower right corner of image TOC: Link to bookmark or image Mijin Word 2 09-22-2011 03:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:47 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft