本文記錄,如何使用 Word VBA,把文件中,長圖切割并拆分到多個頁中去。
問題背景:
最近在處理一個 Word 文檔,發(fā)現(xiàn)里面有特別長的圖片,超過了頁面大小,導(dǎo)致打印的時候,根本無法打印整張圖片;然后發(fā)現(xiàn),Word 中,根本沒有辦法,設(shè)置“圖片跨頁顯示”;而且在網(wǎng)上查了半天,也沒有好辦法;于是,只能自己動手豐衣足食了,寫段 VBA 代碼,專門用于處理這個問題;下面分享給大家;
解決思路:
解決思路很簡單,就是根據(jù)頁面高度,對比圖片高度,要是圖片高度大于頁面高度,就按照頁面高度,把圖片切成一段一段的,放回去!
不足之處:
代碼只能按照頁面高度,自動去切分,但是無法確定,切圖片的位置,恰好是你想要的位置;切完之后,要是需要微調(diào)切分位置,就用 Word 里的 Crop 選項,自己手調(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
就這些,希望對大家有幫助,(^_^)b