wps 宏 自動排版
Sub 自動程序()
? ? ' 設置頁面布局和格式
? ? With ActiveDocument.PageSetup
? ? ? ? .Orientation = wdOrientPortrait ' 設置為縱向頁面方向
? ? ? ? .TopMargin = CentimetersToPoints(2) ' 頂部頁邊距為2厘米
? ? ? ? .BottomMargin = CentimetersToPoints(2) ' 底部頁邊距為2厘米
? ? ? ? .LeftMargin = CentimetersToPoints(2) ' 左側頁邊距為2厘米
? ? ? ? .RightMargin = CentimetersToPoints(2) ' 右側頁邊距為2厘米
? ? End With
? ??
? ? ' 設置段落格式
? ? With ActiveDocument.Content.ParagraphFormat
? ? ? ? .Alignment = wdAlignParagraphJustify ' 對齊方式為兩端對齊
? ? ? ? .LineSpacingRule = wdLineSpace1pt5 ' 行間距為1.5倍
? ? ? ? .SpaceAfter = 0 ' 段后間距為0
? ? End With
? ??
? ? ' 設置字體和字號
? ? With ActiveDocument.Content.Font
? ? ? ? .Name = "宋體" ' 設置字體為宋體
? ? ? ? .Size = 12 ' 設置字號為12磅
? ? End With
? ??
? ? ' 執(zhí)行自動排版
? ? ActiveDocument.AutoFormat
? ??
MsgBox "宏指令執(zhí)行完成", vbInformation
End Sub
Sub 圖片排版()
? ? Dim shp As Shape
? ??
? ? ' 設置頁面布局和格式
? ? With ActiveDocument.PageSetup
? ? ? ? .Orientation = wdOrientPortrait ' 設置為縱向頁面方向
? ? ? ? .TopMargin = CentimetersToPoints(2) ' 頂部頁邊距為2厘米
? ? ? ? .BottomMargin = CentimetersToPoints(2) ' 底部頁邊距為2厘米
? ? ? ? .LeftMargin = CentimetersToPoints(2) ' 左側頁邊距為2厘米
? ? ? ? .RightMargin = CentimetersToPoints(2) ' 右側頁邊距為2厘米
? ? End With
? ??
? ? ' 設置段落格式
? ? With ActiveDocument.Content.ParagraphFormat
? ? ? ? .Alignment = wdAlignParagraphJustify ' 對齊方式為兩端對齊
? ? ? ? .LineSpacingRule = wdLineSpace1pt5 ' 行間距為1.5倍
? ? ? ? .SpaceAfter = 0 ' 段后間距為0
? ? End With
? ??
? ? ' 設置字體和字號
? ? With ActiveDocument.Content.Font
? ? ? ? .Name = "宋體" ' 設置字體為宋體
? ? ? ? .Size = 12 ' 設置字號為12磅
? ? End With
? ??
? ? ' 遍歷文檔中的每個形狀對象
? ? For Each shp In ActiveDocument.Shapes
? ? ? ? Select Case shp.Type
? ? ? ? ? ? Case msoTextBox ' 文本框對象
? ? ? ? ? ? ? ? ' 設置文本框的段落格式
? ? ? ? ? ? ? ? With shp.TextFrame.TextRange.ParagraphFormat
? ? ? ? ? ? ? ? ? ? .Alignment = wdAlignParagraphJustify ' 對齊方式為兩端對齊
? ? ? ? ? ? ? ? ? ? .LineSpacingRule = wdLineSpace1pt5 ' 行間距為1.5倍
? ? ? ? ? ? ? ? ? ? .SpaceAfter = 0 ' 段后間距為0
? ? ? ? ? ? ? ? End With
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ' 設置文本框的字體和字號
? ? ? ? ? ? ? ? With shp.TextFrame.TextRange.Font
? ? ? ? ? ? ? ? ? ? .Name = "宋體" ' 設置字體為宋體
? ? ? ? ? ? ? ? ? ? .Size = 12 ' 設置字號為12磅
? ? ? ? ? ? ? ? End With
? ? ? ? ? ??
? ? ? ? ? ? Case msoPicture ' 圖片對象
? ? ? ? ? ? ? ? ' 調整圖片的大小和位置
? ? ? ? ? ? ? ? shp.LockAspectRatio = msoFalse ' 可以調整寬高比
? ? ? ? ? ? ? ? shp.Width = CentimetersToPoints(10) ' 設置圖片寬度為10厘米
? ? ? ? ? ? ? ? shp.Height = CentimetersToPoints(10) ' 設置圖片高度為10厘米
? ? ? ? ? ? ? ? shp.Left = CentimetersToPoints(2) ' 設置圖片左側位置為2厘米
? ? ? ? ? ? ? ? shp.Top = CentimetersToPoints(2) ' 設置圖片頂部位置為2厘米
? ? ? ? End Select
? ? Next shp
? ??
? ? ' 執(zhí)行自動排版
? ? ActiveDocument.AutoFormat
? ??
? ? ' 顯示消息框
? ? MsgBox "自動排版已完成!", vbInformation
End Sub
下面其他都可以就是表格不ok
Sub AutoLayout()
? ? Dim shp As Shape
? ? Dim tbl As Table
? ??
? ? ' 解除頁面設置的鎖定
? ? ActiveDocument.Sections(1).PageSetup.SectionStart = wdSectionContinuous
? ??
? ? ' 設置頁面布局和格式
? ? With ActiveDocument.PageSetup
? ? ? ? .Orientation = wdOrientPortrait ' 設置為縱向頁面方向
? ? ? ? .TopMargin = CentimetersToPoints(2) ' 頂部頁邊距為2厘米
? ? ? ? .BottomMargin = CentimetersToPoints(2) ' 底部頁邊距為2厘米
? ? ? ? .LeftMargin = CentimetersToPoints(2) ' 左側頁邊距為2厘米
? ? ? ? .RightMargin = CentimetersToPoints(2) ' 右側頁邊距為2厘米
? ? End With
? ??
? ? ' 設置段落格式
? ? With ActiveDocument.Content.ParagraphFormat
? ? ? ? .Alignment = wdAlignParagraphJustify ' 對齊方式為兩端對齊
? ? ? ? .LineSpacingRule = wdLineSpace1pt5 ' 行間距為1.5倍
? ? ? ? .SpaceAfter = 0 ' 段后間距為0
? ? End With
? ??
? ? ' 設置字體和字號
? ? With ActiveDocument.Content.Font
? ? ? ? .Name = "宋體" ' 設置字體為宋體
? ? ? ? .Size = 12 ' 設置字號為12磅
? ? End With
? ??
? ? ' 遍歷文檔中的每個形狀對象
? ? For Each shp In ActiveDocument.Shapes
? ? ? ? Select Case shp.Type
? ? ? ? ? ? Case msoTextBox ' 文本框對象
? ? ? ? ? ? ? ? ' 設置文本框的段落格式
? ? ? ? ? ? ? ? With shp.TextFrame.TextRange.ParagraphFormat
? ? ? ? ? ? ? ? ? ? .Alignment = wdAlignParagraphJustify ' 對齊方式為兩端對齊
? ? ? ? ? ? ? ? ? ? .LineSpacingRule = wdLineSpace1pt5 ' 行間距為1.5倍
? ? ? ? ? ? ? ? ? ? .SpaceAfter = 0 ' 段后間距為0
? ? ? ? ? ? ? ? End With
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ' 設置文本框的字體和字號
? ? ? ? ? ? ? ? With shp.TextFrame.TextRange.Font
? ? ? ? ? ? ? ? ? ? .Name = "宋體" ' 設置字體為宋體
? ? ? ? ? ? ? ? ? ? .Size = 12 ' 設置字號為12磅
? ? ? ? ? ? ? ? End With
? ? ? ? ? ??
? ? ? ? ? ? Case msoPicture ' 圖片對象
? ? ? ? ? ? ? ? ' 調整圖片的大小和位置
? ? ? ? ? ? ? ? shp.LockAspectRatio = msoFalse ' 可以調整寬高比
? ? ? ? ? ? ? ? shp.Width = CentimetersToPoints(10) ' 設置圖片寬度為10厘米
? ? ? ? ? ? ? ? shp.Height = CentimetersToPoints(10) ' 設置圖片高度為10厘米
? ? ? ? ? ? ? ? shp.Left = CentimetersToPoints(2) ' 設置圖片左側位置為2厘米
? ? ? ? ? ? ? ? shp.Top = CentimetersToPoints(2) ' 設置圖片頂部位置為2厘米
? ? ? ? End Select
? ? Next shp
? ??
? ? ' 遍歷文檔中的每個表格對象
? ? For Each tbl In ActiveDocument.Tables
? ? ? ? ' 調整表格的寬度為頁面寬度
? ? ? ? tbl.PreferredWidthType = wdPreferredWidthPercent
? ? ? ? tbl.PreferredWidth = 100
? ? Next tbl
? ??
? ? ' 執(zhí)行自動排版
? ? ActiveDocument.AutoFormat
? ??
? ? ' 顯示消息框
? ? MsgBox "自動排版已完成!", vbInformation
End Sub
下面是刪除表格所有的表格的宏
以下是刪除 Word 文檔中所有表格的 WPS 宏示例:
```vba
Sub DeleteAllTables()
? ? Dim tbl As Object
? ??
? ? ' 確保當前文檔不是只讀模式
? ? If ActiveDocument.ReadOnly Then
? ? ? ? MsgBox "無法編輯只讀文檔。", vbExclamation
? ? ? ? Exit Sub
? ? End If
? ??
? ? ' 確認是否刪除所有表格
? ? If MsgBox("是否確定刪除文檔中的所有表格?", vbQuestion + vbYesNo) = vbNo Then
? ? ? ? Exit Sub
? ? End If
? ??
? ? ' 循環(huán)刪除所有表格
? ? For Each tbl In ActiveDocument.Tables
? ? ? ? tbl.Delete
? ? Next tbl
? ??
? ? ' 顯示消息框
? ? MsgBox "已成功刪除文檔中的所有表格。", vbInformation
End Sub
```
請按照以下步驟使用上述宏:
1. 打開你想要刪除表格的 Word 文檔。
2. 按下 `Alt+F11` 進入 VBA 編輯器。
3. 創(chuàng)建一個新的宏模塊,將以上代碼粘貼到模塊中。
4. 保存宏并關閉 VBA 編輯器。
5. 使用快捷鍵 `Alt+F8` 打開宏列表。
6. 選擇 `DeleteAllTables` 宏并點擊 "運行" 按鈕。
7. 宏將刪除文檔中的所有表格,并顯示一個消息框確認刪除結果。
請注意,在運行宏之前,請確保已經保存好文檔的備份,以防意外刪除了不希望刪除的內容。