@vba代碼
' 將圖片進行居中操作
Sub ChangePictureFormate()Dim oPara As ParagraphDim oRange As RangeDim i As LongDim beforeIsPicture As BooleanbeforesIsPicture = False' 確保文檔中至少有圖片If ActiveDocument.InlineShapes.Count = 0 ThenMsgBox "沒有找到圖片。"Exit SubEnd If' 遍歷所有段落For Each oPara In ActiveDocument.Paragraphs' 檢查段落是否包含圖片'wdAlignParagraphLeft: 左對齊
'wdAlignParagraphCenter: 居中對齊
'wdAlignParagraphRight: 右對齊
'wdAlignParagraphJustify: 兩端對齊
'wdAlignParagraphDistribute:分散對齊(文本均勻分布在行的左右兩邊'所有的圖片居右操作If oPara.Range.InlineShapes.Count > 0 ThenoPara.Alignment = wdAlignParagraphRightEnd IfNext oParaMsgBox "程序運行完成!"
End Sub' 更改圖片標題
Sub ChangePictureLabelFormate()Dim oPara As ParagraphDim oRange As RangeDim i As LongDim beforeParaIsPicture As BooleanbeforeParaIsPicture = False' 確保文檔中至少有圖片If ActiveDocument.InlineShapes.Count = 0 ThenMsgBox "沒有找到圖片。"Exit SubEnd If' 遍歷所有段落For Each oPara In ActiveDocument.ParagraphsDebug.Print oPara.Range.TextDebug.Print beforeParaIsPictureIf (beforeParaIsPicture = True) Then'Debug.Print 1oPara.Range.Font.Bold = TrueoPara.Alignment = wdAlignParagraphLeftEnd IfIf oPara.Range.InlineShapes.Count > 0 Then'選中下一個自然段'Set oRange = oPara.Range'oRange.Collapse Direction:=wdCollapseEnd'oRange.Move Unit:=wdParagraph, Count:=1'oRange.Font.Bold = True'MsgBox oRange.TextbeforeParaIsPicture = TrueElsebeforeParaIsPicture = FalseEnd IfNext oPara'MsgBox "程序運行完成!"
End Sub‘’對所有的表格進行批量化的操作
Sub ChangeAllTables()Dim tbl As TableDim selectionRange As Range' 創建一個新的 Range 對象以存儲所有表格的合并范圍Set selectionRange = ActiveDocument.Range' 遍歷每個表格并擴展選擇的范圍For Each tbl In ActiveDocument.TablesWith tbl.PreferredWidthType = wdPreferredWidthPercent.PreferredWidth = 100.Range.Rows.Alignment = wdAlignRowCenterEnd WithFor Each Cell In tbl.Range.Cells' 更改單元格內所有文本的字體大小With Cell.Range.Font.Name = "宋體".Font.Name = "Times New Roman".Font.Size = 12 ' 設置為24號字.ParagraphFormat.Alignment = wdAlignParagraphRightEnd WithNext CellNext tblDebug.Print "所有表格調整完畢"End Sub‘’更改所有表格的圖例
Sub SetFontSizeAboveTables()Dim tbl As TableDim rng As Range' 遍歷文檔中的所有表格For Each tbl In ActiveDocument.Tables' 設置 rng 為表格上方的段落Set rng = tbl.Rangerng.MoveStart wdParagraph, -1 ' 移動到表格的前一個段落rng.MoveEnd wdParagraph, 1 ' 種回到表格的結尾' 設置上方段落的字體大小為 12If rng.Paragraphs.Count > 0 Thenrng.Paragraphs(1).Range.Font.Size = 28rng.Paragraphs(1).Range.Font.Name = "宋體"rng.Paragraphs(1).Range.Font.Name = "Times New Roman"End IfNext tbl
End Sub