Kumarcoolz
02-03-2014, 04:32 AM
Hi Guys,
i have a problem regarding word VBA, i wanted to select and paste a group of pictures in an alligned manner and then create a textbox over the pictures and get the name of the picture in the textbox.
i googled, recorded and maniplated somes macro to select the pictures, paste them on the format as required. Find the code below:
Sub InsertImages()
Dim doc As Word.Document
Dim fd As FileDialog
Dim mg2 As Range
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set doc = ActiveDocument
Dim vrtSelectedItem As Variant
Dim oILShp As InlineShape
Dim Char As Characters
Dim StrPath As String
Dim StrNewPath As String
Dim strDocName As String
' selecting the Picture files
With fd
.Filters.Add "Computer Graphics Metafile", "*.jpg", 1
.FilterIndex = 1
r = 0
If .Show = -1 Then
StrPath = .SelectedItems(1)
For Each vrtSelectedItem In .SelectedItems
Set mg2 = ActiveDocument.Range
mg2.Collapse wdCollapseEnd
doc.InlineShapes.AddPicture FileName:=vrtSelectedItem, LinkToFile:=False, SaveWithDocument:=True, Range:=mg2
r = r + 1
'getting the file names of the pictures
StrPath = vrtSelectedItem
strDocName = Replace(StrPath, "\", Chr(45), 1, (Len(StrPath) - Len(Replace(StrPath, "\", ""))) - 1)
strDocName = Right(strDocName, Len(StrPath) - InStr(strDocName, "\"))
strDocName = Left(strDocName, InStrRev(strDocName, ".", -1) - 1)
Debug.Print strDocName
'allignment of the picture
If r Mod 2 = 0 Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.TypeParagraph
End If
If r Mod 2 = 1 Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText (vbTab)
End If
Next vrtSelectedItem
Else
MsgBox ("No Images Selected")
Exit Sub
End If
End With
Set fd = Nothing
'changing the size of the picture
For Each oILShp In ActiveDocument.InlineShapes
With oILShp
.Height = InchesToPoints(2.5)
.Width = InchesToPoints(3)
End With
Next
End Sub
But i am unable to bring textbox appropriately above the pictures. Help or ideas on this would be highly greatfully.
i have attached the sample document of my final requirement. my document contains generally 100 such pictures to be pasted.
Regards
Kumar
i have a problem regarding word VBA, i wanted to select and paste a group of pictures in an alligned manner and then create a textbox over the pictures and get the name of the picture in the textbox.
i googled, recorded and maniplated somes macro to select the pictures, paste them on the format as required. Find the code below:
Sub InsertImages()
Dim doc As Word.Document
Dim fd As FileDialog
Dim mg2 As Range
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set doc = ActiveDocument
Dim vrtSelectedItem As Variant
Dim oILShp As InlineShape
Dim Char As Characters
Dim StrPath As String
Dim StrNewPath As String
Dim strDocName As String
' selecting the Picture files
With fd
.Filters.Add "Computer Graphics Metafile", "*.jpg", 1
.FilterIndex = 1
r = 0
If .Show = -1 Then
StrPath = .SelectedItems(1)
For Each vrtSelectedItem In .SelectedItems
Set mg2 = ActiveDocument.Range
mg2.Collapse wdCollapseEnd
doc.InlineShapes.AddPicture FileName:=vrtSelectedItem, LinkToFile:=False, SaveWithDocument:=True, Range:=mg2
r = r + 1
'getting the file names of the pictures
StrPath = vrtSelectedItem
strDocName = Replace(StrPath, "\", Chr(45), 1, (Len(StrPath) - Len(Replace(StrPath, "\", ""))) - 1)
strDocName = Right(strDocName, Len(StrPath) - InStr(strDocName, "\"))
strDocName = Left(strDocName, InStrRev(strDocName, ".", -1) - 1)
Debug.Print strDocName
'allignment of the picture
If r Mod 2 = 0 Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.TypeParagraph
End If
If r Mod 2 = 1 Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText (vbTab)
End If
Next vrtSelectedItem
Else
MsgBox ("No Images Selected")
Exit Sub
End If
End With
Set fd = Nothing
'changing the size of the picture
For Each oILShp In ActiveDocument.InlineShapes
With oILShp
.Height = InchesToPoints(2.5)
.Width = InchesToPoints(3)
End With
Next
End Sub
But i am unable to bring textbox appropriately above the pictures. Help or ideas on this would be highly greatfully.
i have attached the sample document of my final requirement. my document contains generally 100 such pictures to be pasted.
Regards
Kumar