本文记录,如何使用 Word VBA,把文件中,长图切割并拆分到多个页中去。code
最近在处理一个 Word 文档,发现里面有特别长的图片,超过了页面大小,致使打印的时候,根本没法打印整张图片;而后发现,Word 中,根本没有办法,设置“图片跨页显示”;并且在网上查了半天,也没有好办法;因而,只能本身动手丰衣足食了,写段 VBA 代码,专门用于处理这个问题;下面分享给你们;
orm
解决思路很简单,就是根据页面高度,对比图片高度,要是图片高度大于页面高度,就按照页面高度,把图片切成一段一段的,放回去!
blog
代码只能按照页面高度,自动去切分,可是没法肯定,切图片的位置,刚好是你想要的位置;切完以后,要是须要微调切分位置,就用 Word 里的 Crop 选项,本身手调吧。
图片
VBA 中的 .Crop 方法,所截取的图片的高度,是根据图片原始尺寸计算的;因此,若是你有一个图片,已经缩放了 40% 的高度;若是代码中 .CropTop = 400,但因为图片有缩放,因此实际放到图片上的 CropTop = 40% * 400,就变成了 160 了;这就致使,比你实际要剪裁的高度,少了不少;这就是为何。我在代码中,在剪裁高度上,逆向除回去了,缩放比例。
文档
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
就这些,但愿对你们有帮助,(^_^)bit