PDA

View Full Version : [SOLVED:] Word AddPics macro mod help



belmartin
03-24-2021, 01:46 PM
Hello guys! Hope everybody is OK :)


I wanted to ask you for some help with modifications, since I'm a total newbie. Got a macro from an old thread solved by macropod, this one:



Sub AddPics()
Application.ScreenUpdating = False
Dim i As Long, j As Long, c As Long, r As Long, NumCols As Long
Dim oTbl As Table, TblWdth As Single, StrTxt As String, RwHght As Single
On Error GoTo ErrExit
NumCols = CLng(InputBox("How Many Columns per Row?"))
RwHght = CSng(InputBox("What row height for the pictures, in inches (e.g. 1.5)?"))
On Error GoTo 0
'Select and insert the Pics
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select image files and click OK"
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
.FilterIndex = 2
If .Show = -1 Then
'Add a 2-row by NumCols-column table to take the images
Set oTbl = Selection.Tables.Add(Range:=Selection.Range, NumRows:=2, NumColumns:=NumCols)
With ActiveDocument.PageSetup
TblWdth = .PageWidth - .LeftMargin - .RightMargin - .Gutter
End With
With oTbl
.AutoFitBehavior (wdAutoFitFixed)
.Columns.Width = TblWdth / NumCols
End With
CaptionLabels.Add Name:="Picture"
For i = 1 To .SelectedItems.Count Step NumCols
r = ((i - 1) / NumCols + 1) * 2 - 1
'Format the rows
Call FormatRows(oTbl, r, RwHght)
For c = 1 To NumCols
j = j + 1
'Insert the Picture
ActiveDocument.InlineShapes.AddPicture _
FileName:=.SelectedItems(j), LinkToFile:=False, _
SaveWithDocument:=True, Range:=oTbl.Cell(r, c).Range
'Get the Image name for the Caption
StrTxt = Split(.SelectedItems(j), "\")(UBound(Split(.SelectedItems(j), "\")))
StrTxt = ": " & Split(StrTxt, ".")(0)
'Insert the Caption on the row below the picture
With oTbl.Cell(r + 1, c).Range
.InsertBefore vbCr
.Characters.First.InsertCaption _
Label:="Foto", Title:=StrTxt, _
Position:=wdCaptionPositionBelow, ExcludeLabel:=False
.Characters.First = vbNullString
.Characters.Last.Previous = vbNullString
End With
'Exit when we're done
If j = .SelectedItems.Count Then Exit For
Next
'Add extra rows as needed
If j < .SelectedItems.Count Then
oTbl.Rows.Add
oTbl.Rows.Add
End If
Next
Else
End If
End With
ErrExit:
Application.ScreenUpdating = True
End Sub
'
Sub FormatRows(oTbl As Table, x As Long, Hght As Single)
With oTbl
With .Rows(x)
.Height = InchesToPoints(Hght)
.HeightRule = wdRowHeightExactly
.Range.Style = "Normal"
End With
With .Rows(x + 1)
.Height = CentimetersToPoints(0.5)
.HeightRule = wdRowHeightExactly
.Range.Style = "Caption"
End With
End With
End Sub



It helped me a lot, but I need to make some modifications, and don't know where to begin.
I work with botany, so my reports are sometimes huge, with hundreds of images. I wanted to change the caption, from "Image number - file name" to "Image number - tree number".
I'm now doing this line by line, very very boring, with fields:


It's always the same, at left photo of the tree, at right photo of the number tag. Sometimes, the specimens are not numbered begining with "1". See example:
28166


The number of specimen is repeated, but the photo number is continuous... I am using fields now: "SEQ Step \r #" in first caption cell, "SEQ Step \c" in number tag cell; the following cells are all "SEQ Step \n" in next specimen photo, and repeat "SEQ Step \c" in number tags.

Thank you in advance for your atention and, if possible, help!! : pray2:

macropod
03-24-2021, 02:49 PM
So where would the macro get the tree number from?

belmartin
03-26-2021, 12:42 PM
I could number the first one, others would be sequel

macropod
03-26-2021, 03:00 PM
Try the following, which is based on a more recent version of the code than what you've been using. Note the changes in bold


Sub AddPics()
Application.ScreenUpdating = False
Dim Stl As Style, i As Long, j As Long, c As Long, r As Long, NumCols As Long
Dim oTbl As Table, TblWdth As Single, StrTxt As String, RwHght As Single
On Error GoTo ErrExit
NumCols = CLng(InputBox("How Many Columns per Row?"))
RwHght = CSng(InputBox("What row height for the pictures, in inches (e.g. 1.5)?"))
With ActiveDocument
On Error Resume Next
Set Stl = .Styles("TblPic")
If Stl Is Nothing Then Set Stl = .Styles.Add(Name:="TblPic", Type:=wdStyleTypeParagraph)
CaptionLabels.Add Name:="Foto"
On Error GoTo 0
End With
With ActiveDocument.Styles("TblPic").ParagraphFormat
.Alignment = wdAlignParagraphCenter
.SpaceAfter = 0
.SpaceBefore = 0
End With
'Select and insert the Pics
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Select image files and click OK"
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
.FilterIndex = 2
If .Show = -1 Then
'Add a 2-row by NumCols-column table to take the images
Set oTbl = Selection.Tables.Add(Range:=Selection.Range, NumRows:=2, NumColumns:=NumCols)
With ActiveDocument.PageSetup
TblWdth = .PageWidth - .LeftMargin - .RightMargin - .Gutter
End With
With oTbl
.AutoFitBehavior (wdAutoFitFixed)
.Columns.Width = TblWdth / NumCols
End With
CaptionLabels.Add Name:="Picture"
For i = 1 To .SelectedItems.Count Step NumCols
r = ((i - 1) / NumCols + 1) * 2 - 1
'Format the rows
Call FormatRows(oTbl, r, RwHght)
For c = 1 To NumCols
j = j + 1
'Insert the Picture
ActiveDocument.InlineShapes.AddPicture _
FileName:=.SelectedItems(j), LinkToFile:=False, _
SaveWithDocument:=True, Range:=oTbl.Cell(r, c).Range
'Add descriptive text for the Caption
StrTxt = ": Exemplar " & ChrW(8470) & " "
'Insert the Caption on the row below the picture
With oTbl.Cell(r + 1, c).Range
.InsertBefore vbCr
.Characters.First.InsertCaption _
Label:="Foto", Title:=StrTxt, _
Position:=wdCaptionPositionBelow, ExcludeLabel:=False
.Characters.First = vbNullString
'.Characters.Last.Previous = vbNullString
.Fields.Add Range:=.Characters.Last.Previous, Type:=wdFieldEmpty, _
Text:="SEQ Nr \c", PreserveFormatting:=False
End With
'Exit when we're done
If j = .SelectedItems.Count Then Exit For
Next
'Add extra rows as needed
If j < .SelectedItems.Count Then
oTbl.Rows.Add
oTbl.Rows.Add
End If
Next
Else
End If
End With
ErrExit:
Application.ScreenUpdating = True
End Sub
'
Sub FormatRows(oTbl As Table, x As Long, Hght As Single)
With oTbl
With .Rows(x)
.Height = InchesToPoints(Hght)
.HeightRule = wdRowHeightExactly
.Range.Style = "TblPic"
.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End With
With .Rows(x + 1)
.Height = InchesToPoints(0.25)
.HeightRule = wdRowHeightExactly
.Range.Style = "Caption"
End With
End With
End Sub

belmartin
03-30-2021, 02:39 PM
Hi Macropod, my hero hahahah

Tested here, didn't work for me, guess Word isn't recognizing numeral symbol ChrW(2116), it returns "Exemplar 0ࡄ".
28221
With your initial huge step, I'll try to change that part here, if it works, let you know!

Thanks a lot :)

macropod
03-31-2021, 04:23 AM
I've revised the № character reference. Try the code now.

belmartin
03-31-2021, 09:40 AM
Now the "Nš" is working fine!

But I didn't understand how this field is going to work, since they all have the same code.

The 1st caption has to be "SEQ Nr \r #", being # the number of the first tree.
The 2nd caption, as every other captions in right column, "SEQ Nr \c".

Now the 3rd, and all of the following left column captions, gotta be "SEQ Nr \n", sequel following the first tree number.Don't know if that is possible with vba...

gmaxey
03-31-2021, 12:59 PM
Paul, I'm missing something. When I use your code to insert say four photos, the captions read Foto 1, Exemplar No0 Foto 2, Exemplar No0 Foto 3, Exemplar No0 Foto 4, Exemplar No0

and it appears you Seq field {SEQ Nr \c} would display the number of the nearest of the Seq field.

macropod
03-31-2021, 03:55 PM
But I didn't understand how this field is going to work, since they all have the same code.

The 1st caption has to be "SEQ Nr \r #", being # the number of the first tree.
The 2nd caption, as every other captions in right column, "SEQ Nr \c".

Now the 3rd, and all of the following left column captions, gotta be "SEQ Nr \n", sequel following the first tree number.Don't know if that is possible with vba...
I previously asked:

So where would the macro get the tree number from?
to which you replied:

I could number the first one, others would be sequel
Your reply suggested that you would simply edit the SEQ fields to supply whatever number is appropriate for the tree for any given series of photos, which the SEQ would repeat until you edit another SEQ field to supply another tree number. Naturally, you would need to refresh the field display after doing this.

belmartin
04-05-2021, 08:10 AM
Sorry, guess I wasn't clear.

OK, thank you anyway! Gonna try to make the changes I need.

:hi: