使用VBA,操作word文档中的所有图片,让图片重置为原始大小。

要用好ChatGPT关键是准确描述自己的需求,和对它生成代码的纠正、完善。不管怎么说,它真的节省了我很多时间。

需求:calibre将电子书文件转成doc格式后,里面的图片大小不一样,有些很小。我想将图片统一设置为原始大小。VBA处理最快,但为了写一点代码,不熟悉VBA语法、查vba文档很浪费时间,搜索引擎受代码不一定找得到好的。这时ChatGPT好用。

以下是ChatGPT生成某一次的代码,标黄的地方还是有错误。

Sub ResetAllPictures()
    Dim shp As Shape
    For Each shp In ActiveDocument.Shapes
        If shp.Type = msoPicture Then
            shp.Height = shp.Height / shp.ScaleHeight
            shp.Width = shp.Width / shp.ScaleWidth
        End If
    Next shp
    Dim pic As InlineShape
    For Each pic In ActiveDocument.InlineShapes
        pic.Reset
    Next pic
End Sub

 

但是ChatGPT知错能改,下面是能在2016word上面执行的代码

Sub ResetAllPictures()
    Dim shp As Shape
    For Each shp In ActiveDocument.Shapes
        If shp.Type = msoPicture Then
            shp.ScaleHeight 1, msoTrue
            shp.ScaleWidth 1, msoTrue
        End If
    Next shp
    Dim pic As InlineShape
    For Each pic In ActiveDocument.InlineShapes
        pic.Reset
    Next pic
End Sub

 

posted @ 2023-03-07 10:50  Randall_J  阅读(792)  评论(0)    收藏  举报