Use VBA in Word to insert all the. jpg images in a folder

Source: Internet
Author: User
Tags goto

Every four pictures on one page , the first row is the file name of four pictures

Insert Picture Resize Reference

Sub final () Dim FN as String, N%, w#, h#, pw#, Ph#with activedocument.pagesetup ' Set the paper type and margins (A4, margin 2cm) and calculate the width of the image. Orientation = wdorientportrait. TopMargin = centimeterstopoints (2). BottomMargin = centimeterstopoints (2). LeftMargin = centimeterstopoints (2). RightMargin = centimeterstopoints (2). Gutter = centimeterstopoints (0). PageWidth = centimeterstopoints (21). PageHeight = centimeterstopoints (29.7) PW = (. PageWidth-. LeftMargin-. RightMargin)/2 PH =. PageHeight-. TopMargin-.    Leftmarginend WITHFN = Dir ("f:\*.jpg") ' Loop ' in the current file directory for each jpg file Dim F (3) as String B = 0Do while FN <> "" ' Continuous loop when file name is not empty Selection.InlineShapes.AddPicture "f:\" & FN ' Insert current loop to jpg file a = b Mod 4 F (a) = FN with activedocument.i Nlineshapes (b + 1) W =. Width ' gets wide and high H =. Height. Lockaspectratio = msoTrue if w/h >= pw/ph then ' if the width-to-height ratio of the picture is greater than the aspect ratio that should be set. width = PW * 0.99 ' Adjusts the width to the width that should be set, and the height is scaled by the width ratio before and after the adjustment. Height = H * pw/w Else ' If the width-to-height ratio of the picture is less than the aspect ratio that should be set. Height = PH * 0.99 ' Adjusts the height to the height that should be set, and the width is scaled by the height ratio before and after adjustment. Width = W * ph/h End If End with FN = Dir ' loops to the next file If a = 3 then Selection.insertbreak Type:=wdpageb Reak Selection.goto what:=wdgotoline, which:=wdgotoprevious, count:=3 with Selection.Range. Text = f (0) & "," & F (1) & "," & F (2) & "," & F (3) & VbCrLf. " Font.Name = "Times New Roman". Font.Size = 12. ParagraphFormat.Alignment = wdAlignParagraphCenter End with Selection.goto what:=wdgotopage, Which:=wdGoToNe XT, Count:=1 End If B = B + 1Loop If a <> 3 then Select case a case 0 Sele Ction. GoTo What:=wdgotoline, which:=wdgotoprevious, Count:=1 Selection.goto what:=wdgotopage, Which:=wdGoToNext, Count:=1 with Selection.Range.                Text = f (0) & VbCrLf    . Font.Name = "Times New Roman". Font.Size = 12.  ParagraphFormat.Alignment = wdAlignParagraphCenter End with Selection.goto what:=wdgotopage, Which:=wdgotonext, count:=1 Case 1 Selection.goto what:=wdgotoline, which:=wdgotoprevious, Coun                    T:=1 Selection.goto what:=wdgotopage, Which:=wdgotonext, count:=1 with Selection.Range . Text = f (0) & "," & F (1) & VbCrLf. Font.Name = "Times New Roman". Font.Size = 12.  ParagraphFormat.Alignment = wdAlignParagraphCenter End with Selection.goto what:=wdgotopage, Which:=wdgotonext, Count:=1 case 2 Selection.goto what:=wdgotoline, which:=wdgotoprevious, Coun                  t:=2 Selection.goto what:=wdgotopage, Which:=wdgotonext, count:=1 with Selection.Range  . Text = f (0) & "," & F (1) & "," & F (2) & VbCrLf. Font.Name = "Times New Roman". Font.Size = 12.  ParagraphFormat.Alignment = wdAlignParagraphCenter End with Selection.goto what:=wdgotopage, Which:=wdgotonext, Count:=1 end Select End IfEnd Sub

Use VBA in Word to insert all. jpg images in a folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.