Consulting

Results 1 to 4 of 4

Thread: Insert Image Before Paragraph

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    14
    Location

    Insert Image Before Paragraph

    Hey guys sorry I am fairly new to VBA and have searched around but cannot figure out how to code what I am trying to do.
    Basically I want to add an image with the same name as the first word in the paragraph before that word; this macro will be to add images into an inventory list.

    So far I have something like this:
    =========
    Sub FAXP()
    n = 20
    While counter <> "-----------------------------------------------------------------------------"

    counter = ActiveDocument.Paragraphs(n). _
    Range.Words(1)

    If counter <> "-----------------------------------------------------------------------------" Then
    ActiveDocument.Paragraphs(n). _
    Range.InsertBefore "Picture "

    End If

    n = n + 1
    Wend

    End Sub
    =========

    I found a macro to resize all the pictures after they are inserted but I do not know how to insert images correctly yet, especially with a variable filename . Can someone provide some direction so that instead of inserting the words "Picture " it will insert the picture with the same name as the counter variable? I hope I am explaining it correctly.

    I imagine I would need to made a separate macro to be called, something like this:

    =========
    Sub InsertImage()
    Dim rng As Range
    Dim vShape As InlineShape

    Set vShape = Selection.InlineShapes.AddPicture(FileName:="C:\Users\Person\Desktop" & VARIABLEcounter?? & ".png", LinkToFile:= _
    False, SaveWithDocument:=True)

    With vShape
    .LockAspectRatio = True
    .Height = InchesToPoints(1)
    End With
    End Sub
    =========

    Thanks!
    Last edited by l0aded; 07-23-2014 at 04:10 PM.

  2. #2
    VBAX Regular mart.potter's Avatar
    Joined
    Jul 2014
    Location
    Tallinn, Estonia (EU)
    Posts
    9
    Location
    Sub FAXP()
        n = 1
        TotalP = ActiveDocument.Paragraphs.count
        Do While n < TotalP
            counter = ActiveDocument.Paragraphs(n).Range.Words(1)
        
           If counter <> "-----------------------------------------------------------------------------" Then
               ActiveDocument.Paragraphs(n).Range.Words(1).Select
               Selection.MoveLeft Unit:=wdCharacter, count:=1
               InsertImage Trim(counter)
           Else
               Exit Sub
           End If
        
           n = n + 1
    Loop
    
    
    End Sub
    
    
    Sub InsertImage(VARIABLEcounter)
        On Error Resume Next
        Selection.InlineShapes.AddPicture FileName:= _
            "C:\Users\Person\Desktop\" & VARIABLEcounter & ".JPG", LinkToFile:=False, _
            SaveWithDocument:=True
        End Sub
    Also remember, that counter in your defintion will not cut the the word only by the space, but also by the "_" for instance. So alternative would be to split the paragarph to an array by by space:

    Sub FAXP()
        n = 1
        TotalP = ActiveDocument.Paragraphs.count
        Do While n < TotalP
            vParagraphText = ActiveDocument.Paragraphs(n).Range
            vParagraphWords = Split(vParagraphText, " ")
            counter = vParagraphWords(0)
        
            If ActiveDocument.Paragraphs(n).Range.Words(1) <> "-----------------------------------------------------------------------------" Then
                ActiveDocument.Paragraphs(n).Range.Words(1).Select
                Selection.MoveLeft Unit:=wdCharacter, count:=1
                InsertImage Trim(counter)
            Else
              Exit Do
            End If
        
            n = n + 1
        Loop
    
    
    End Sub
    
    
    Sub InsertImage(VARIABLEcounter)
        On Error Resume Next
        Selection.InlineShapes.AddPicture FileName:= _
            "C:\Users\Person\Desktop\" & VARIABLEcounter & ".jpg", LinkToFile:=False, _
            SaveWithDocument:=True
    End Sub

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For a different approach, see: http://www.vbaexpress.com/forum/show...l=1#post306321. The code in this link adds the chosen pictures and adds their names to the document.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Regular
    Joined
    Jul 2014
    Posts
    14
    Location
    Quote Originally Posted by mart.potter View Post
    Sub FAXP()
        n = 1
        TotalP = ActiveDocument.Paragraphs.count
        Do While n < TotalP
            counter = ActiveDocument.Paragraphs(n).Range.Words(1)
        
           If counter <> "-----------------------------------------------------------------------------" Then
               ActiveDocument.Paragraphs(n).Range.Words(1).Select
               Selection.MoveLeft Unit:=wdCharacter, count:=1
               InsertImage Trim(counter)
           Else
               Exit Sub
           End If
        
           n = n + 1
    Loop
    
    
    End Sub
    
    
    Sub InsertImage(VARIABLEcounter)
        On Error Resume Next
        Selection.InlineShapes.AddPicture FileName:= _
            "C:\Users\Person\Desktop\" & VARIABLEcounter & ".JPG", LinkToFile:=False, _
            SaveWithDocument:=True
        End Sub
    Also remember, that counter in your defintion will not cut the the word only by the space, but also by the "_" for instance. So alternative would be to split the paragarph to an array by by space:

    Sub FAXP()
        n = 1
        TotalP = ActiveDocument.Paragraphs.count
        Do While n < TotalP
            vParagraphText = ActiveDocument.Paragraphs(n).Range
            vParagraphWords = Split(vParagraphText, " ")
            counter = vParagraphWords(0)
        
            If ActiveDocument.Paragraphs(n).Range.Words(1) <> "-----------------------------------------------------------------------------" Then
                ActiveDocument.Paragraphs(n).Range.Words(1).Select
                Selection.MoveLeft Unit:=wdCharacter, count:=1
                InsertImage Trim(counter)
            Else
              Exit Do
            End If
        
            n = n + 1
        Loop
    
    
    End Sub
    
    
    Sub InsertImage(VARIABLEcounter)
        On Error Resume Next
        Selection.InlineShapes.AddPicture FileName:= _
            "C:\Users\Person\Desktop\" & VARIABLEcounter & ".jpg", LinkToFile:=False, _
            SaveWithDocument:=True
    End Sub
    The code worked like a charm thanks!
    Last edited by l0aded; 07-24-2014 at 02:35 PM.

Posting Permissions

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