![]() |
#1
|
|||
|
|||
![]()
Greetings. Thank you in advance for viewing my question.
The goal is to take 4 digital images from my file and insert onto one page. Preferably with no border. I can insert one image easy enough, but the second image defaults to a second page. Supposedly the image can be moved about with a left drag of the mouse but my efforts result in no movement. |
#2
|
||||
|
||||
![]()
The simplest way is to create a 4-cell table, with the 'automatically resize to fit contents' option 'off' and with the cell width and 'exact' height set to the maximum allowable for each image (eg, in a 2*2 table, the column width would be, say, half the intra-margin width and the row height would be half the intra-margin height). You can turn off the table's border display. With this setup, any image you insert into the table will be constrained so that it fits into the cell whilst maintaining the correct aspect ratio.
The following macro automates the insertion of multiple images into a table (which the macro also creates) in a Word document. As coded, it allows you to specify any number of columns and the picture row height. The column-width is calculated automatically, based on the page print width. Any inserted pictures will be constrained to fit the available cell space, at the correct aspect ratio, by enlarging/reducing them, as appropriate. Below each image is a row for adding a caption. The process begins at wherever the selection/insertion point is. As coded, the macro uses the "Caption" Style for the caption rows. This left-aligns the captions. It also uses a custom "TblPic" Style for the image rows, ensuring the pictures are horizontally centred in their cells and correctly fill the space available. Cells are also centred vertically. You can change any of these parameters. Code:
Sub AddPics() Application.ScreenUpdating = False Dim i As Long, j As Long, c As Long, r As Long, NumCols As Long, iShp As InlineShape Dim oTbl As Table, TblWdth As Single, StrTxt As String, RwHght As Single, ColWdth As Single On Error GoTo ErrExit NumCols = CLng(InputBox("How Many Columns per Row?")) RwHght = CentimetersToPoints(CSng(InputBox("What max height for the pictures, in Centimeters (e.g. 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 'Create a paragraph Style with 0 space before/after & centre-aligned On Error Resume Next With ActiveDocument .Styles.Add Name:="TblPic", Type:=wdStyleTypeParagraph On Error GoTo 0 With .Styles("TblPic").ParagraphFormat .Alignment = wdAlignParagraphCenter .KeepWithNext = True .SpaceAfter = 0 .SpaceBefore = 0 End With End With '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 ColWdth = TblWdth / NumCols End With With oTbl .AutoFitBehavior (wdAutoFitFixed) .TopPadding = 0 .BottomPadding = 0 .LeftPadding = 0 .RightPadding = 0 .Spacing = 0 .Columns.Width = ColWdth .Borders.Enable = True 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 Set iShp = ActiveDocument.InlineShapes.AddPicture( _ FileName:=.SelectedItems(j), LinkToFile:=False, _ SaveWithDocument:=True, Range:=oTbl.Cell(r, c).Range) With iShp .LockAspectRatio = True If (.Width < ColWdth) And (.Height < RwHght) Then .Width = ColWdth If .Height > RwHght Then .Height = RwHght End If End With '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:="Picture", 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 = Hght .HeightRule = wdRowHeightExactly .Range.Style = "TblPic" .Cells.VerticalAlignment = wdCellAlignVerticalCenter End With With .Rows(x + 1) .Height = CentimetersToPoints(0.5) .HeightRule = wdRowHeightExactly .Range.Style = "Caption" End With End With End Sub Code:
NumCols = CLng(InputBox("How Many Columns per Row?")) Code:
NumCols = 4 Likewise, if you want a predetermined row height without the need to respond to a prompt, change: Code:
RwHght = CentimetersToPoints(CSng(InputBox("What max height for the pictures, in Centimeters (e.g. 5)?"))) Code:
RwHght = CentimetersToPoints(5) If you want to use a fixed column-width, you could change: .Columns.Width = TblWdth / NumCols to, say: .Columns.Width = CentimetersToPoints(7.5) where '7.5' is the required width in cm (try not to use a column width & count that exceed your printable area!). If you're concerned to prevent overly-long captions wrapping, you can: 1. Reduce the Caption Style's font size; and/or 2. Apply the 'Fit Text' option to long captions so they fit on one line. This might be accomplished by inserting: Code:
If .Characters.Last.Previous.Information(wdVerticalPositionRelativeToPage) <> _ .Characters.First.Information(wdVerticalPositionRelativeToPage) Then .FitTextWidth = ColWdth End If Code:
.Characters.Last.Previous = vbNullString • Change: .KeepWithNext = True to: .KeepWithNext = False and insert: .Styles("Caption").ParagraphFormat.KeepWithNext = True after: CaptionLabels.Add Name:="Picture" • Change: SaveWithDocument:=True, Range:=oTbl.Cell(r, c).Range to: SaveWithDocument:=True, Range:=oTbl.Cell(r + 1, c).Range • Change: With oTbl.Cell(r + 1, c).Range to: With oTbl.Cell(r, c).Range • Swap: With .Rows(x) and: With .Rows(x + 1) If your situation is one in which the images may change from time to time, but you want to avoid replacing them manually each time, you could change: LinkToFile:=False to: LinkToFile:=True That way, the images will auto-update if you replace them with others of the same name. For a borderless table • Change: .Borders.Enable = True to: .Borders.Enable = False If you don't want the final result to be in a table, insert: oTbl.ConvertToText before: Else Finally, if you prefer to work in imperial units rather than metric units, change all Centimeters references to: Inches For PC macro installation & usage instructions, see: Installing Macros For Mac macro installation & usage instructions, see: Word:mac - Install a Macro
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
I have been looking for this code for so long.
This will literally save me hours and hours of work as I have over 300 photos to tabulate. |
#4
|
|||
|
|||
![]()
Hello everyone, thank you macropod for this macro it is very usefull for me at work. Do you think this macro can be improved, as to autocorrect the captions under the photos inserted if you delete one photo?
What i mean is that if i insert 4 photos and i delete photo 3, the captions under the remaining photos to be in order and photo 4 caption to be automatically deleted. |
#5
|
||||
|
||||
![]()
Since the macro concerns only the insertion of images, not their deletion, the simple answer is: no.
However, if you were to use a multicolumn page layout for wherever you want the pics to appear, together with a specification that there should only be one image per column, deleting an image and its caption will result in all the others updating correctly with no blank cells. Do be aware, though, that this changes the layout from across-then-down to down-then-across.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
Dear Macropod, thank you for the well thought out VBA code. Unfortunately I experienced and error when attempting to run the macro routine. I am hoping that someone might offer advice in diagnosis the cause of the error.
The error message is: Run-time error '4605': This method or property is not available because this command is not available inside math. The command line where this occurred: Set oTbl = Selection.Tables.Add(Range:=Selection.Range, NumRows:=2, NumColumns:=NumCols) Thank you everyone. Cheers/M |
#7
|
||||
|
||||
![]()
NumCols should be an Integer that you entered in an inputbox. It is defined as Long so conceivably you are not putting an Integer in as the input value.
What are you seeing if you hover over the word NumCols when that line is being debugged?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#8
|
|||
|
|||
![]()
Hi! Thanks!
There is a way to use the maximum available page area instead of asking for the maximum picture height? Considering a given number of rows and columns (pictures) per page.
__________________
Backup your original file before doing any modification. |
#9
|
|||
|
|||
![]()
Hi Macropod,
I name my pictures 001, 002, 003, etc. because they have to be in a certain order. When I run the program that you provided it inserts the file name into the row, but adds Picture 1:, Picture 2: etc. in front of the file name. is there a way to remove this part in the macro? |
#10
|
||||
|
||||
![]()
You could change:
Code:
With oTbl.Cell(r + 1, c).Range .InsertBefore vbCr .Characters.First.InsertCaption _ Label:="Picture", Title:=StrTxt, _ Position:=wdCaptionPositionBelow, ExcludeLabel:=False .Characters.First = vbNullString .Characters.Last.Previous = vbNullString End With Code:
oTbl.Cell(r + 1, c).Range.Text =StrTxt
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
Thank you Macropod!
|
#12
|
|||
|
|||
![]()
Your whole solution is utterly elegant. You've just saved me hours. and hours. of work and pain and exasperation. Thank you more than there are words to express.
|
#13
|
|||
|
|||
![]()
Hello, this has saved me soooooo much time so thank you very much!! Can I ask, how would I remove the caption completely? I need the row but to insert my own text. Also, is there a way to number each blank row in every column? So if I have 2 columns per row have them showing as 1. 2. 3. etc?
|
#14
|
|||
|
|||
![]()
Great stuff. Just what I was looking for.
|
#15
|
|||
|
|||
![]()
This is great. I truly thank you for posting this. This will improve my efficiencies in multiple ways.
Can anyone help explain how I'd change the font size and type throughout the photos that this adds (nothing else in the document). If we could justify the captions to wrap a new line at the width of the photos that would be extra great. I will ultimately change the code to always use a vertical picture height of 9 cm. |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Digital signatures | hannu | Word | 0 | 11-18-2012 03:19 PM |
Saving doc as "Web Page, Filtered" reduces some images' quality | GregL | Word | 1 | 08-14-2012 03:27 AM |
![]() |
Colonel Biggs | Drawing and Graphics | 13 | 12-12-2011 08:52 PM |
![]() |
Sonia Sosa | Mail Merge | 8 | 04-22-2011 03:05 PM |
Inline Images Floating BEHIND Text & Off Page | Pennimus | Drawing and Graphics | 0 | 02-22-2010 09:29 AM |