Log in

View Full Version : Insert Image Before Paragraph



l0aded
07-23-2014, 03:25 PM
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 :think:. 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!

mart.potter
07-24-2014, 12:28 AM
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

macropod
07-24-2014, 05:03 AM
For a different approach, see: http://www.vbaexpress.com/forum/showthread.php?44473-Insert-Multiple-Pictures-Into-Table-Word-With-Macro&p=306321&viewfull=1#post306321. The code in this link adds the chosen pictures and adds their names to the document.

l0aded
07-24-2014, 01:49 PM
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!