Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-11-2023, 05:59 AM
shortbred shortbred is offline Select where to insert new image Windows 10 Select where to insert new image Office 2021
Novice
Select where to insert new image
 
Join Date: Oct 2023
Posts: 4
shortbred is on a distinguished road
Default Select where to insert new image

Hi all,

This is probably really basic, I have been trawling through information trying to find the answer with no luck. Basically I have an arduous task of changing some text and a picture on over 250 documents. The code I have is as below (obviously text and file path changed)

The top of it works fine, the text replaces, the old picture gets deleted (well replaced with " "). It's the bottom bit as it will pull in the new image at whatever location the cursor is at when the file opens. I was it to be where the old one is, that is half the reason I replaced the photo with " " thinking maybe I can get the cursor to go to " " and then pull the image in but I can't even find how to do that, I'm sure there's a much easier way than the roundabout way I am trying anyway, so any help will be gratefully apricated.



Sub SimpleReplace()


Selection.Find.ClearFormatting


Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "ABC"
.Replacement.Text = "XYZ"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

End With
Selection.Find.Execute Replace:=wdReplaceAll


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^g"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

End With
Selection.Find.Execute Replace:=wdReplaceAll


Selection.InlineShapes.AddPicture FileName:= _
"C:\FILE PATH", LinkToFile:=False, _
SaveWithDocument:=True

End Sub
Reply With Quote
  #2  
Old 10-12-2023, 10:04 AM
vivka vivka is offline Select where to insert new image Windows 7 64bit Select where to insert new image Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi! Catch it:
Code:
Sub Replace_Pic()
    
    selection.Find.ClearFormatting
    selection.Find.Replacement.ClearFormatting
    With selection.Find
       .text = "^g"
       .MatchWildcards = False
       .Execute
       If .found Then
          selection.InlineShapes.AddPicture filename:= _
            "d:\Desktop\2.jpg", LinkToFile:=False, _
            SaveWithDocument:=True
       End If
    End With
End Sub
Reply With Quote
  #3  
Old 10-13-2023, 12:22 AM
shortbred shortbred is offline Select where to insert new image Windows 10 Select where to insert new image Office 2021
Novice
Select where to insert new image
 
Join Date: Oct 2023
Posts: 4
shortbred is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
Hi! Catch it:
Code:
Sub Replace_Pic()
    
    selection.Find.ClearFormatting
    selection.Find.Replacement.ClearFormatting
    With selection.Find
       .text = "^g"
       .MatchWildcards = False
       .Execute
       If .found Then
          selection.InlineShapes.AddPicture filename:= _
            "d:\Desktop\2.jpg", LinkToFile:=False, _
            SaveWithDocument:=True
       End If
    End With
End Sub
Thank you so much!
Reply With Quote
  #4  
Old 10-13-2023, 12:45 AM
vivka vivka is offline Select where to insert new image Windows 7 64bit Select where to insert new image Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Thanks for your thanks!

Note that this code raplaces a single picture. If you want to replace multiple pictures with the same image, the code should include backward looping.
Reply With Quote
  #5  
Old 10-13-2023, 01:24 AM
shortbred shortbred is offline Select where to insert new image Windows 10 Select where to insert new image Office 2021
Novice
Select where to insert new image
 
Join Date: Oct 2023
Posts: 4
shortbred is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
Thanks for your thanks!

Note that this code raplaces a single picture. If you want to replace multiple pictures with the same image, the code should include backward looping.
Understood. There is only one image on the page so this one works fine. I have also posted another query. I am unsure if you can help
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Image and Move to Front Pestomania Word VBA 1 12-24-2020 09:07 AM
Word 2007-cannot select or copy image calvin-c Word 2 01-05-2017 02:26 PM
ComboBox : select image to insert in Sheet1 smallxyz Excel Programming 0 02-07-2016 05:41 PM
Select where to insert new image Select an entire line (to insert one) Officer_Bierschnitt Excel Programming 3 11-06-2015 03:05 AM
Background image vs. insert image lilaria PowerPoint 0 04-18-2011 08:45 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:21 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