word vba代码大全
时间: 2023-06-22 07:25:27 浏览: 215
vba常用代码大全.doc
以下是一些 Word VBA 代码示例:
1. 插入文本
```vb
Sub InsertText()
'在光标位置插入文本
Selection.TypeText "这是要插入的文本。"
End Sub
```
2. 保存文档
```vb
Sub SaveDocument()
'保存文档
ActiveDocument.SaveAs "C:\Documents\MyDocument.docx"
End Sub
```
3. 设置字体样式
```vb
Sub SetFontStyle()
'设置字体样式
With Selection.Font
.Name = "Arial"
.Size = 14
.Bold = True
.Italic = False
End With
End Sub
```
4. 删除选定内容
```vb
Sub DeleteSelection()
'删除选定内容
Selection.Delete
End Sub
```
5. 插入页眉
```vb
Sub InsertHeader()
'插入页眉
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "这是页眉内容"
End Sub
```
6. 插入页脚
```vb
Sub InsertFooter()
'插入页脚
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "这是页脚内容"
End Sub
```
7. 插入图片
```vb
Sub InsertPicture()
'插入图片
Selection.InlineShapes.AddPicture FileName:="C:\Pictures\MyPicture.jpg", _
LinkToFile:=False, SaveWithDocument:=True
End Sub
```
8. 设置页面边距
```vb
Sub SetPageMargins()
'设置页面边距
With ActiveDocument.PageSetup
.TopMargin = CentimetersToPoints(2)
.BottomMargin = CentimetersToPoints(2)
.LeftMargin = CentimetersToPoints(2)
.RightMargin = CentimetersToPoints(2)
End With
End Sub
```
9. 插入表格
```vb
Sub InsertTable()
'插入表格
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=3
End Sub
```
以上是 Word VBA 的一些常用代码示例,可以根据需要进行修改和调整。
阅读全文