PDA

View Full Version : [SOLVED:] Insert the image into the corresponding position in word



hattieucoi91
08-13-2023, 04:11 PM
I have an exercise file (n sentences) and a folder containing (n images). I need to find a way to insert n images under each corresponding sentence.
Hope your help. Thanks somuch

sorry I can't put the rar file, here is example link
https://drive.google.com/file/d/1O3MfclxL65mM663mlkSypg0ZOkL-hVtE/view?usp=sharing

Aussiebear
08-13-2023, 06:41 PM
Welcome to VBAX hattieucoi91. Hopefully one of the word specialists can assist you here.

fouad
08-13-2023, 09:17 PM
Welcome

gmayor
08-13-2023, 11:20 PM
Based on your example
Sub InsertImages()
Dim i As Integer, j As Integer
Dim oRng As Range
Dim sName As String
Dim sPath As String
j = 0
sPath = BrowseForFolder("Select folder containing images")
For i = 1 To ActiveDocument.Range.Paragraphs.Count
If i Mod 2 = 0 Then
j = j + 1
sName = sPath & j & ".png"
Set oRng = ActiveDocument.Range.Paragraphs(i).Range
oRng.Collapse 0
oRng.InlineShapes.AddPicture sName
End If
Next i
lbl_Exit
Set oRng = Nothing
Exit Sub
End Sub

Private 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
should work.

hattieucoi91
08-14-2023, 02:12 AM
thank you verymuch, but it doesn't work.
30981

hattieucoi91
08-14-2023, 02:16 AM
Based on your example
Sub InsertImages()
Dim i As Integer, j As Integer
Dim oRng As Range
Dim sName As String
Dim sPath As String
j = 0
sPath = BrowseForFolder("Select folder containing images")
For i = 1 To ActiveDocument.Range.Paragraphs.Count
If i Mod 2 = 0 Then
j = j + 1
sName = sPath & j & ".png"
Set oRng = ActiveDocument.Range.Paragraphs(i).Range
oRng.Collapse 0
oRng.InlineShapes.AddPicture sName
End If
Next i
lbl_Exit
Set oRng = Nothing
Exit Sub
End Sub

Private 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
should work.
thankyou verymuch but it doesn't work
30982

gmayor
08-14-2023, 05:47 AM
lbl_Exit should have a colon after it. (See the function below it).

hattieucoi91
08-14-2023, 06:00 AM
lbl_Exit should have a colon after it. (See the function below it).
it's done, thank for your help! Great.

hattieucoi91
08-14-2023, 03:13 PM
I have two problems
1-The answer has many pictures, will be named 3a, 3b, 3c...
2-If there is no image corresponding to the question number, the question will be highlighted
hope the helping



Based on your example
Sub InsertImages()
Dim i As Integer, j As Integer
Dim oRng As Range
Dim sName As String
Dim sPath As String
j = 0
sPath = BrowseForFolder("Select folder containing images")
For i = 1 To ActiveDocument.Range.Paragraphs.Count
If i Mod 2 = 0 Then
j = j + 1
sName = sPath & j & ".png"
Set oRng = ActiveDocument.Range.Paragraphs(i).Range
oRng.Collapse 0
oRng.InlineShapes.AddPicture sName
End If
Next i
lbl_Exit
Set oRng = Nothing
Exit Sub
End Sub

Private 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
should work.