PDA

View Full Version : Changing ActiveX Image Control Picture



Chunk
02-03-2020, 06:44 AM
Good morning everyone,

Each week I have to replace approximately 50 pictures in a project document (MS Word).

I currently have 5 projects I am dealing with and am looking for a better/more efficient solution other than copying/pasting/resizing each picture.

The pictures are in the same order in every project's report.

I have each of the projects pictures in a separate folder.

It would be wonderful to either activate a button or even get them to automatically update when the document is opened.

I unsuccessfully attempted to use ActiveX Image Controls and couldn't manage to get the .Picture parameter to change.

I am looking for ideas, at the current moment. I will play with the code, to begin with, then more than likely be back with questions.

Thanks in advance for any help.

Chunk

gmayor
02-03-2020, 10:54 PM
Forget ActiveX controls. You need Picture Content controls. If you insert such controls and title them Picture1 to Picture50 then it is a simple matter to loop through the images and add a corresponding numbered image. e.g.


Dim oCC As ContentControl
Dim i As Integer
For i = 1 To 503
Set oCC = ActiveDocument.SelectContentControlsByTitle("Picture" & i).Item(1)
oCC.Range.InlineShapes.AddPicture "C:\Path\ImageName" & i & ".png"
Next i
Set oCC = NothingObviously in this case the images must also be named ImageName1 to ImageName50 and all be in the same folder, which seems unlikely, so you are going to have to list the images and correspond them to the appropriate content controls. If the names of the images are subject to constant change then you will need to produce an appropriate list each time and relate the lines of that list to the numbered controls. A text file with a filename and its path on each line in the order that they are placed is the simplest approach and you could then modify the code as follows to read that text file and fill the content controls.

Sub InsertImages()
Const strFilename As String = "C:\Path\ImageList.txt"
Dim oCC As ContentControl
Dim iFile As Integer, i As Integer
Dim strDataLine As String
iFile = FreeFile()
Open strFilename For Input As #iFile
i = 0
On Error GoTo lbl_Exit
While Not EOF(iFile)
i = i + 1
Line Input #iFile, strDataLine
Set oCC = ActiveDocument.SelectContentControlsByTitle("Picture" & i).Item(1)
oCC.Range.InlineShapes.AddPicture strDataLine
Wend
lbl_Exit:
Set oCC = Nothing
End Sub
If the images are all together or in blocks of multiple images, you may find https://www.gmayor.com/photo_gallery_template.html useful. As for inserting content controls and/or retitling them see (http://www.gmayor.com/insert_content_control_addin.htm)https://www.gmayor.com/insert_content_control_addin.htm