Consulting

Results 1 to 9 of 9

Thread: Insert the image into the corresponding position in word

  1. #1

    Insert the image into the corresponding position in word

    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/1O3M...ew?usp=sharing

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Welcome to VBAX hattieucoi91. Hopefully one of the word specialists can assist you here.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Welcome

  4. #4
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    thank you verymuch, but it doesn't work.
    bao loi.jpg

  6. #6
    Quote Originally Posted by gmayor View Post
    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
    bao loi.jpg

  7. #7
    lbl_Exit should have a colon after it. (See the function below it).
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    Quote Originally Posted by gmayor View Post
    lbl_Exit should have a colon after it. (See the function below it).
    it's done, thank for your help! Great.

  9. #9
    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


    Quote Originally Posted by gmayor View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •