用VBA获取Word文件中图片的分辨率

问题描述 投票:0回答:1

我有一个问题,经过最后一小时的互联网搜索后未能得到解答。

我需要循环查看文档中的所有图形并检查分辨率是否至少为 600 dpi。

我使用插画设置 dpi = 1000 和尺寸 70 x 40 毫米创建了一个图像。正如预期的那样,图像的尺寸为 2756 x 1575 像素。 然后我把图片导入到word中。它的正确尺寸为 70x40 毫米。如果我使用“将图形另存为”从 word 保存图片,我会得到 2756x1575 像素的原始尺寸。

我运行以下代码:

Sub GetImageResolutions()
    Dim i As Integer
    Dim shape As InlineShape
    Dim resolution As String
    Dim dpi As Double
    Dim width, height As Integer
    
    Debug.Print "Number of images: " & ActiveDocument.InlineShapes.Count
    For i = 1 To ActiveDocument.InlineShapes.Count
        Set shape = ActiveDocument.InlineShapes(i)
        If shape.Type = wdInlineShapePicture Then
            ' Determining original width before scaling
            width = shape.width / shape.ScaleWidth * 100
            height = shape.height / shape.ScaleHeight * 100
            ppi = width / (shape.width / 96)
            Debug.Print "Image " & i & ": "
            Debug.Print shape.width & "x" & shape.height & " points, " & shape.width / 72 * 96 & "x" & shape.height / 72 * 96 & " - " & shape.ScaleWidth & " " & shape.ScaleHeight
            Debug.Print width & " x " & height & " points (scaled)"
            Debug.Print PointsToPixels(width, False) & " x " & PointsToPixels(height, True) & " pixels"
            Debug.Print "resolution: " & ppi
        End If
    Next i
End Sub

我得到的东西很奇怪:

Image 1:
198,35x113,45 points, 264,4667x151,2667 - 100 100
198,35 x 113 points (scaled)
264 x 150 pixels
resolution: 96

宽度和高度以磅为单位(因此 1 英寸有 72 磅)。使用

PointsToPixels
,我手动进行了转换* 72/96。 基本上看来,Word 给出的“像素”尺寸与实际尺寸(70x40 毫米,分辨率为 96)兼容。事实上,最终结果(对于我测试的所有图像)始终是 96。

我错过了什么吗?我期望获得 2756 x 1575 px 或至少 1000 dpi 的分辨率。

编辑: 如果我解压缩 word 文件并浏览到文件夹 word\media,我可以看到具有正确像素大小的所有图像(对于本例,预期为 2756 x 1757)。我想知道是否有办法从 InlineShape 对象检索此信息。

vba ms-word resolution screen-resolution
1个回答
0
投票

如果您已经知道图像的分辨率,则只能将点转换为像素。 VBA 中的

PointsToPixels
函数是一个旧函数,可以追溯到图像存储为 96ppi 时。

根据您使用的 Word 版本,情况可能不再如此。文档中图像的分辨率由每个文档的设置决定,据我所知,该设置在 VBA 中不可用。

image of Word Options dialog showing Image Quality setting

© www.soinside.com 2019 - 2024. All rights reserved.