PDA

View Full Version : [SOLVED:] Vba insert 4 images at bookmarked places depending on point(middle) number value



oskar13
09-16-2015, 01:27 AM
Hi all,

Need help with this problem for work and would really appreciate any help.

I have 4 images I need to insert them at different places that are bookmarked in word.
Images are called 1393_1_FOTO,1393_1_PHOTO,
1393_1_ST, 1393_1_DS for point number 1, for point number 2 names of images are 1393_2_FOTO,1393_2_PHOTO,
1393_2_ST, 1393_2_DS, for point number 3 1393_3_FOTO,1393_3_PHOTO,
1393_3_ST, 1393_3_DS and so on up to number like 300.

So everything stays the same except middle number which is always rising +1 for every point. I would need a module that can always put images depending on point number in same places. I mean 1393_n_FOTO no matter for what point number it needs to go on bm1 bookmark, 1393_n_PHOTO needs to go on bm2, 1393_n_ST needs to go on bm3, 1393_n_DS needs to go on bm4.

Is there way to create macro that asks me for point number "n" and when i enter it it insert 1393_n_FOTO at bm1,1393_n_PHOTO at bm2, 1393_n_ST at bm3, 1393_n_DS at bm4.

gmayor
09-16-2015, 02:16 AM
You asked this question in another forum at http://www.msofficeforums.com/word-vba/27859-vba-word-multiple-images-bookmarks.html, moving the goalposts from a previous answer. Please cross reference your posts if you swap forums.

This problem is rather more complex than the original and cannot be handled in quite the same way. Can we take it that you have a folder of images with names like 1393_n_FOTO, 1393_n_PHOTO, 1393_n_ST, 1393_n_DS where n is a number between 1 and approx 300 and those numbers are to be sequentially added to your bookmarks as in the earlier example?

Each 'n' is unique and represents the order the images are to be inserted into the four bookmarks with a new document as previously described every four images? Your information that suggests 1393_1_FOTO,1393_1_PHOTO, 1393_1_ST, 1393_1_DS for point number 1 indicates 'n' may not be unique, but that would not make sense with the rest of the description.

The number 1393 remains constant?

What if anything is the significance of the DS, ST, and PHOTO? You need to clarify the order in which a folder full of files will be sequentially added to four bookmarks in documents, because at the moment your requirement doesn't make sense.

oskar13
09-16-2015, 02:47 AM
Thx gmayor i hoped i run into you again :)

This problem is rather more complex than the original and cannot be handled in quite the same way. Can we take it that you have a folder of images with names like 1393_n_FOTO, 1393_n_PHOTO, 1393_n_ST, 1393_n_DS where n is a number between 1 and approx 300 and those numbers are to be sequentially added to your bookmarks as in the earlier example?

The answer is yes they are all in same folder and those numbers are to be sequentially added to your bookmarks as in the earlier example.


Each 'n' is unique and represents the order the images are to be inserted into the four bookmarks with a new document as previously described every four images? Your information that suggests 1393_1_FOTO,1393_1_PHOTO, 1393_1_ST, 1393_1_DS for point number 1 indicates 'n' may not be unique, but that would not make sense with the rest of the description.

The number 1393 remains constant?

There are 4 images for every point. This is for point 1 "1393_1_FOTO","1393_1_PHOTO", "1393_1_ST", "1393_1_DS".
Name of the images are always the same only n is changing for every point.
So constant for every other point will be "1393_except this number which is point number_FOTO","1393_except this number which is point number_PHOTO", "1393_except this number which is point number_ST", "1393_except this number which is point number_DS".

Images would always have the same order 1393_N_FOTO has to go on bm1, 1393_n_PHOTO has to go on bm2, 1393_n_ST need to go on bm3, 1393_n_DS need to go on bm4.
For example for point 1: 1393_1_FOTO will go on bm1, 1393_1_PHOTO will go on bm2, 1393_1_ST will go on bm3, 1393_1_DS will go on bm4.
In new document for point number 2: 1393_2_FOTO will go on bm1, 1393_2_PHOTO will go on bm2, 1393_2_ST will go on bm3, 1393_2_DS will go on bm4.
All images are always in same folder.

DS, ST, PHOTO and FOTO are all names of images and are also constant like 1393. Every point has those 4 images and those 4 constant will repeat itself through all points.

gmayor
09-16-2015, 04:33 AM
Hmmm This is even more complicated than I thought.

So you have images like

"1393_1_FOTO","1393_1_PHOTO", "1393_1_ST", "1393_1_DS". (Document 1)
"1393_2_FOTO","1393_2_PHOTO", "1393_2_ST", "1393_2_DS". (Document 2)
"1393_3_FOTO","1393_3_PHOTO", "1393_3_ST", "1393_3_DS". (Document 3)
"1393_4_FOTO","1393_4_PHOTO", "1393_4_ST", "1393_4_DS". (Document 4)
etc?
In that case it should not be quite as bad as I anticipated. Can you confirm that is correct before I return to it.

gmayor
09-16-2015, 05:00 AM
If the images are as described in my last post, the following should work (provided only those files are in the folder as I have not added any error trapping for incorrect file sequences).


Option Explicit

Sub InsertImages2()
'Graham Mayor
Const strFile As String = "C:\Path\ImageTemplate.docx" 'The template
Dim oBM As Bookmark
Dim oDoc As Document
Dim strPath As String, strImage As String
Dim strId As String
Dim vImage As Variant
Dim i As Long
strPath = BrowseForFolder("Select the folder containing the graphics files")
If Not strPath = "" Then
strImage = Dir$(strPath & "*.*")
i = 0
While Len(strImage) <> 0
On Error GoTo err_Handler
If i Mod 4 = 0 Then
Set oDoc = Documents.Add(strFile)
End If
strId = Left(strImage, Len(strImage) - 4)
vImage = Split(strId, "_")
'Msgbox strImage
Select Case vImage(2)
Case Is = "FOTO"
ImageToBM "bm1", strPath & strImage
Case Is = "PHOTO"
ImageToBM "bm2", strPath & strImage
Case Is = "ST"
ImageToBM "bm3", strPath & strImage
Case Is = "DS"
ImageToBM "bm4", strPath & strImage
End Select
i = i + 1
strImage = Dir$()
Wend
End If
lbl_Exit:
Set oDoc = Nothing
Set oBM = Nothing
Exit Sub
err_Handler:
GoTo lbl_Exit
End Sub

Function BrowseForFolder(Optional strTitle As String) As String
'Graham Mayor
'strTitle is the title of the dialog box
Dim fDialog As FileDialog
On Error GoTo err_Handler
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = strTitle
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then GoTo err_Handler:
BrowseForFolder = fDialog.SelectedItems.Item(1) & Chr(92)
End With
lbl_Exit:
Exit Function
err_Handler:
BrowseForFolder = vbNullString
Resume lbl_Exit
End Function

Private Sub ImageToBM(strBMName As String, strValue As String)
'Graham Mayor
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.InlineShapes.AddPicture _
Filename:=strValue, LinkToFile:=False, _
SaveWithDocument:=True
oRng.End = oRng.End + 2
oRng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub

oskar13
09-16-2015, 05:44 AM
Thk you Graham

Its working hehehe.

This is just what i needed thx for understanding and all the help.