Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-12-2017, 03:26 AM
nsv nsv is offline Rename a VBA imported picture Windows 8 Rename a VBA imported picture Office 2013
Novice
Rename a VBA imported picture
 
Join Date: Jul 2010
Location: Denmark
Posts: 17
nsv is on a distinguished road
Default Rename a VBA imported picture

I need to import a bunch of pictures to a Word document.
Such pictures are given a name starting with 'Picture 1' and adding up as more are imported. I need, however, to be able to identify each picture for possible deletion later, so I therefore want to give them a name immediately when importing them.
So far this code works:
Code:
Dim ImPath1 As String
ImPath1 = "C:\Users\nivo\Pictures\Icons\Water_icon_square.jpg"
ActiveDocument.Shapes.AddPicture FileName:=ImPath1, LinkToFile:=False, SaveWithDocument:=True, _
Left:=-15, Top:=275, Anchor:=Selection.Range, Width:=40, Height:=40
but I need to add something like:
Code:
, Name:="Water".
which unfortunately does not work.

Anyone has a hint?

best regards, NSV

Last edited by nsv; 12-12-2017 at 06:51 AM.
Reply With Quote
  #2  
Old 12-12-2017, 06:01 AM
gmayor's Avatar
gmayor gmayor is offline Rename a VBA imported picture Windows 10 Rename a VBA imported picture Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can't do this, but you could insert the image and add a bookmark to the image location, which will make it easier to address. The following should help

Code:
Public Sub ImageToBM(strbmName As String, strImagePath As String)
'Graham Mayor - http://www.gmayor.com
Dim orng As Range
Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(strImagePath) Then
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set orng = .Bookmarks(strbmName).Range
            orng.Text = ""
            orng.InlineShapes.AddPicture _
                    FileName:=strImagePath, LinkToFile:=False, _
                    SaveWithDocument:=True
            orng.End = orng.End + 1
            orng.Bookmarks.Add strbmName
        End With
    End If
lbl_Exit:
    Set fso = Nothing
    Set orng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 12-12-2017, 06:49 AM
nsv nsv is offline Rename a VBA imported picture Windows 8 Rename a VBA imported picture Office 2013
Novice
Rename a VBA imported picture
 
Join Date: Jul 2010
Location: Denmark
Posts: 17
nsv is on a distinguished road
Default

Thanks Graham, it looks a bit complicated. I was hoping it could be done by adding an extra line or two, but if that can't be done and I will have to use your code then I will have to study it more closely.

Thanks for your effort.

/NSV
Reply With Quote
  #4  
Old 12-12-2017, 04:19 PM
macropod's Avatar
macropod macropod is offline Rename a VBA imported picture Windows 7 64bit Rename a VBA imported picture 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

You could use something like:
Code:
Dim ImPath1 As String, Shp As Shape
ImPath1 = "C:\Users\nivo\Pictures\Icons\Water_icon_square.jpg"
Set Shp = ActiveDocument.Shapes.AddPicture(FileName:=ImPath1, LinkToFile:=False, SaveWithDocument:=True, _
          Left:=-15, Top:=275, Anchor:=Selection.Range, Width:=40, Height:=40)
Shp.Name = Split(Split(ImPath1, "\")(UBound(Split(ImPath1, "\"))), ".")(0)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-13-2017, 01:07 PM
nsv nsv is offline Rename a VBA imported picture Windows 8 Rename a VBA imported picture Office 2013
Novice
Rename a VBA imported picture
 
Join Date: Jul 2010
Location: Denmark
Posts: 17
nsv is on a distinguished road
Default

Thanks Paul, I will certainly give a try.
This Word VBA appears to be somehow less powerful than VBA for Excel, but that is probably because I am new to it.

NSV
Reply With Quote
  #6  
Old 12-14-2017, 07:28 AM
nsv nsv is offline Rename a VBA imported picture Windows 8 Rename a VBA imported picture Office 2013
Novice
Rename a VBA imported picture
 
Join Date: Jul 2010
Location: Denmark
Posts: 17
nsv is on a distinguished road
Default

OK Paul, that worked so that even I could understand it! - thanks again!
Reply With Quote
  #7  
Old 09-26-2019, 08:21 PM
msdiniz msdiniz is offline Rename a VBA imported picture Windows 10 Rename a VBA imported picture Office 2016
Novice
 
Join Date: Apr 2016
Location: Rio de Janeiro, Brazil
Posts: 3
msdiniz is on a distinguished road
Default No space allowed in Shape name

Hi, all.
Thanks, Paul, it functions.

But, I think there is a catch, at least on Office(Word) 365 USA and Brazil versions:

Shape gets a name Picture # (Picture 1, Picture 2, etc); if you try to add a "Tag", like
Code:
shpPicture.Name = shpPicture.Name & "_" & Format(Now(), "yyyymmdd") & imageTag
it will not work because of the space in Picture 1...
You have to remove the space, like:
Code:
shpPicture.Name = VBA.Replace(shpPicture.Name & "_" & Format(Now(), "yyyymmdd") & imageTag, " ", "")
PS 1: the Date and imageTag? To know what they are and safe delete or move in another occasion.
PS 2: Amazing thing, the index Array at the end, with no need of intermediate variables, thanks for teach it.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename a VBA imported picture Picture content control -> insert picture -> empty? Jaymond Flurrie Word 3 07-13-2016 07:00 AM
Rename a VBA imported picture Convert an imported chart (picture) to an editable drawing object fishguy Drawing and Graphics 1 05-23-2016 02:23 AM
Rename Files gsrikanth Excel Programming 3 05-14-2012 03:03 AM
Move Picture by picture name, rename picture by picture name CatMan PowerPoint 2 04-18-2012 12:21 PM
Powerpoint automatically changing picture size when adding a picture (2010) One_Life PowerPoint 7 01-20-2012 06:57 AM

Other Forums: Access Forums

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