
一步到位
之前有分享過Word中批量設定圖片大小的方法,這種方法是插入圖片后再去運行相應的VBA代碼從而批量調(diào)整圖片大小格式。始終會有兩步,即先插入圖片,再執(zhí)行相應的功能。
那可能有人會想,有沒有一種只需一個步驟的方法,插入圖片后不需要其他操作,圖片就已經(jīng)是要求的大小格式呢?
方法肯定是有的。
下面分享兩種我自己工作中用到的方法。
其實在我個人的日常工作中,涉及到Word中需要大量插入圖片的操作時,這種插入圖片后即自動調(diào)整好格式的方法可能會更加便捷。
之前的批量調(diào)整的方法可能在檢查核對時更能發(fā)揮作用,當文檔編輯完后只需要一鍵就能將文檔中所有的圖片調(diào)整到要求的格式。
兩種方法
1. 表格中插入
在表格中插入圖片,表格的大小根據(jù)需要設定的圖片的大小來調(diào)整,這里需要多試幾次,直到表格的大小使插入的圖片大小符合要求。
后續(xù)就直接保存好表格,下次插入圖片時就不用再重新設置了。
表格框線設置為無框線,取消勾選【自動重調(diào)尺寸以適應內(nèi)容】,這樣相當于用表格的大小來限制圖片的大小。

打開表格屬性-選項

取消勾選
這種方法有個前提就是圖片本來的尺寸比表格大,因為是用表格的大小限制圖片,如果圖片尺寸大小本身比表格小就起不到作用。只能將圖片按自身比例縮放,用表格的寬或者高其中一個來限制圖片的大小。
例如,需要將圖片的寬度限制在12cm ,那么將表格的寬度設置為12.4cm,插入后的圖片寬度就變成12cm了,具體表格的寬度值根據(jù)要設定的圖片的寬度來調(diào)整。
同樣,如果要將圖片的高度限制在5cm,則將表格的高度設定為5cm,行高值選擇【固定值】,插入圖片,圖片位置設置為中間居中。

用表格高度、寬度來調(diào)整圖片大小

高度設定為固定值

圖片位置調(diào)整為中間居中
2. VBA命令
另一種方法是用VBA代碼來實現(xiàn)插入圖片后立即調(diào)整大小格式。
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編輯器,在代碼編輯界面粘貼上述代碼。
選擇【開發(fā)工具】,打開【宏】,這里可以看到剛才粘貼的代碼,點擊運行即可。
同樣可以添加到自定義工具欄,指定一個圖標按鈕,或者指定到快捷鍵。

添加到自定義工具欄

點擊自定義工具欄上的圖標即可

添加快捷鍵方便操作,這里設置為Alt + P
最終效果演示:

運用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
結(jié)語
不管用哪種方法本質(zhì)上都是為了減少重復性的操作,合理運用工具方法提高效率是優(yōu)先原則。