本文記錄,如何使用 Word VBA,把文件中,,長(zhǎng)圖切割并拆分到多個(gè)頁(yè)中去,。
問(wèn)題背景:
最近在處理一個(gè) Word 文檔,,發(fā)現(xiàn)里面有特別長(zhǎng)的圖片,,超過(guò)了頁(yè)面大小,導(dǎo)致打印的時(shí)候,,根本無(wú)法打印整張圖片,;然后發(fā)現(xiàn),Word 中,,根本沒(méi)有辦法,,設(shè)置“圖片跨頁(yè)顯示”;而且在網(wǎng)上查了半天,,也沒(méi)有好辦法,;于是,只能自己動(dòng)手豐衣足食了,,寫(xiě)段 VBA 代碼,,專(zhuān)門(mén)用于處理這個(gè)問(wèn)題;下面分享給大家,;
解決思路:
解決思路很簡(jiǎn)單,,就是根據(jù)頁(yè)面高度,對(duì)比圖片高度,,要是圖片高度大于頁(yè)面高度,,就按照頁(yè)面高度,把圖片切成一段一段的,,放回去!
不足之處:
代碼只能按照頁(yè)面高度,,自動(dòng)去切分,但是無(wú)法確定,,切圖片的位置,,恰好是你想要的位置;切完之后,,要是需要微調(diào)切分位置,,就用 Word 里的 Crop 選項(xiàng),自己手調(diào)吧,。
代碼如下:
Sub Split_LongPic()
" Created by: Bitssea (https://www.cnblogs.com/bitssea/)
Set o_InlineShape = ActiveDocument.InlineShapes(1)
o_InlineShape.Select
"Find page height, deduct margin height
Page_TopMargin = ActiveDocument.PageSetup.TopMargin
Page_BottomMargin = ActiveDocument.PageSetup.BottomMargin
Page_Height = ActiveDocument.PageSetup.PageHeight - Page_TopMargin - Page_BottomMargin - 20
"Find Shape Info, Scaled Height, Scale Percentage
Shape_Height = o_InlineShape.Height
Shape_ScalePercent = o_InlineShape.ScaleHeight / 100
If Shape_Height > Page_Height Then
"Find number of copy needed
Split_No = Int(o_InlineShape.Height / Page_Height) + 1
For x = 1 To Split_No
With o_InlineShape.PictureFormat
"Reset Pic Size
.CropTop = 0
.CropBottom = Shape_Height
"Start Crop Pic
.CropBottom = (Shape_Height - x * Page_Height) / Shape_ScalePercent
.CropTop = ((x - 1) * Page_Height) / Shape_ScalePercent
End With
Selection.Copy
Selection.Paste
o_InlineShape.Select
Next
"Delete orignal file, eliminate duplicate
Selection.Delete
End If
End Sub
就這些,,希望對(duì)大家有幫助,(^_^)b