![]() |
#1
|
|||
|
|||
![]()
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 Code:
, Name:="Water". Anyone has a hint? best regards, NSV Last edited by nsv; 12-12-2017 at 06:51 AM. |
#2
|
||||
|
||||
![]()
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 |
#3
|
|||
|
|||
![]()
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 |
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
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 |
#6
|
|||
|
|||
![]()
OK Paul, that worked so that even I could understand it! - thanks again!
|
#7
|
|||
|
|||
![]()
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 You have to remove the space, like: Code:
shpPicture.Name = VBA.Replace(shpPicture.Name & "_" & Format(Now(), "yyyymmdd") & imageTag, " ", "") PS 2: Amazing thing, the index Array at the end, with no need of intermediate variables, thanks for teach it. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Jaymond Flurrie | Word | 3 | 07-13-2016 07:00 AM |
![]() |
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 |