【办公自动化】2. Word中插入图片后自动设定尺寸大小

pexels-pixabay-355948.jpg

一步到位

之前有分享过Word中批量设定图片大小的方法,这种方法是插入图片后再去运行相应的VBA代码从而批量调整图片大小格式。始终会有两步,即先插入图片,再执行相应的功能。

那可能有人会想,有没有一种只需一个步骤的方法,插入图片后不需要其他操作,图片就已经是要求的大小格式呢?

方法肯定是有的。

下面分享两种我自己工作中用到的方法。

其实在我个人的日常工作中,涉及到Word中需要大量插入图片的操作时,这种插入图片后即自动调整好格式的方法可能会更加便捷。

之前的批量调整的方法可能在检查核对时更能发挥作用,当文档编辑完后只需要一键就能将文档中所有的图片调整到要求的格式。

两种方法

1. 表格中插入

在表格中插入图片,表格的大小根据需要设定的图片的大小来调整,这里需要多试几次,直到表格的大小使插入的图片大小符合要求。

后续就直接保存好表格,下次插入图片时就不用再重新设置了。

表格框线设置为无框线,取消勾选【自动重调尺寸以适应内容】,这样相当于用表格的大小来限制图片的大小。

2021-06-02_131035.png
打开表格属性-选项
2021-06-02_131142.png
取消勾选

这种方法有个前提就是图片本来的尺寸比表格大,因为是用表格的大小限制图片,如果图片尺寸大小本身比表格小就起不到作用。只能将图片按自身比例缩放,用表格的宽或者高其中一个来限制图片的大小。

例如,需要将图片的宽度限制在12cm ,那么将表格的宽度设置为12.4cm,插入后的图片宽度就变成12cm了,具体表格的宽度值根据要设定的图片的宽度来调整。

同样,如果要将图片的高度限制在5cm,则将表格的高度设定为5cm,行高值选择【固定值】,插入图片,图片位置设置为中间居中。

2021-06-03_083129.png
用表格高度、宽度来调整图片大小
2021-06-02_134225.png
高度设定为固定值
2021-06-03_083556.png
图片位置调整为中间居中

2. VBA命令

另一种方法是用VBA代码来实现插入图片后立即调整大小格式。

Sub 插入图片自动设置大小()

Dim aRange As Range
Dim aFileName, Pic As Variant

With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "插入图片"
    .InitialView = msoFileDialogViewLargeIcons
      
    If .Show Then
        Set aRange = Selection.Range
        
        For Each aFileName In .SelectedItems
            Selection.InlineShapes.AddPicture FileName:=aFileName
        Next
     
        aRange.SetRange Start:=aRange.Start, End:=Selection.End
        aRange.Select
                     
        For Each Pic In Selection.InlineShapes
            With Pic
                .LockAspectRatio = msoFalse '取消锁定图片纵横比
                .Height = 9 * 28.3378 '设置高度为9cm,1cm = 28.3378 pt
                .Width = 12 * 28.3378 '设置宽度为12cm
            End With
        Next Pic  
    
    End If

End With

End Sub

按快捷键Alt+F11打开VB编辑器,在代码编辑界面粘贴上述代码。

选择【开发工具】,打开【宏】,这里可以看到刚才粘贴的代码,点击运行即可。

同样可以添加到自定义工具栏,指定一个图标按钮,或者指定到快捷键。

自定义按钮.gif
添加到自定义工具栏
2021-06-03_102625.png
点击自定义工具栏上的图标即可
自定义快捷键.gif
添加快捷键方便操作,这里设置为Alt + P

最终效果演示:

实际效果.gif
运用VBA操作效果演示

本文中的代码参考了Office帮助文档中关于FileDialog 对象中如下的这段代码:

Sub Main() 

 'Declare a variable as a FileDialog object. 
 Dim fd As FileDialog 
 
 'Create a FileDialog object as a File Picker dialog box. 
 Set fd = Application.FileDialog(msoFileDialogFilePicker) 
 
 'Declare a variable to contain the path 
 'of each selected item. Even though the path is aString, 
 'the variable must be a Variant because For Each...Next 
 'routines only work with Variants and Objects. 
 Dim vrtSelectedItem As Variant 
 
 'Use a With...End With block to reference the FileDialog object. 
 With fd 
 
     'Add a filter that includes GIF and JPEG images and make it the second item in the list. 
     .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 2 

     'Sets the initial file filter to number 2. 
     .FilterIndex = 2 

     'Use the Show method to display the File Picker dialog box and return the user's action. 
     'If the user presses the button... 
         If .Show = -1 Then 

         'Step through each string in the FileDialogSelectedItems collection. 
             For Each vrtSelectedItem In .SelectedItems 

                 'vrtSelectedItem is aString that contains the path of each selected item. 
                 'You can use any file I/O functions that you want to work with this path. 
                 'This example displays the path in a message box. 
                 MsgBox "Selected item's path: " & vrtSelectedItem 

             Next vrtSelectedItem 
         'If the user presses Cancel... 
         Else 
         End If 
 End With 
 
 'Set the object variable to Nothing. 
 Set fd = Nothing 
 
End Sub

结语

不管用哪种方法本质上都是为了减少重复性的操作,合理运用工具方法提高效率是优先原则。

你可能感兴趣的:(【办公自动化】2. Word中插入图片后自动设定尺寸大小)