Office Word 中的宏

📂 365bet体育在线游戏 ⏳ 2025-09-19 20:30:17 👽 admin 👁️ 2108 💾 279
Office Word 中的宏

Office Word 中的宏

简介使用宏之前的需要进行的设置宏的使用将自定义创建的宏放入文档标题栏中的“自定义快速访问工具栏”宏的录制word文字编辑中的宏操作插入指定格式、内容的字符选中word中的指定文字查找word中的指定文字A,并替换为指定文字B插入文本框并向内插入文字

word 表格中的宏操作批量删除所有表格的第一列批量调整表中各列的宽度在第一行前插入n行在表格指定格内填入文字查找表格中含有指定内容的单元格选中word文档中的所有表格遍历表格中的所有内容批量设置表格中所有内容文字为居中对齐取消表格中指定格子四周的边框合并单元格

简介

宏是一个批量处理程序命令,微软的office软件允许用户自己编写,叫VBA的脚本来增加其灵活性,进一步扩充它的能力,具体做法是在“工具”菜单“宏”-“宏”弹出的对话框输入宏名,然后按“创建”按钮会打开visual basic编辑器,你就可以编程了,这个就是宏。

使用宏之前的需要进行的设置

宏的使用

将自定义创建的宏放入文档标题栏中的“自定义快速访问工具栏”

如下图所示。 若需要修改宏的图标,可以在添加该宏后 在右侧“自定义快速访问工具栏”中 选中该宏,然后点击下方的修改按钮,如下图所示。 点击确定后,完成宏在快速访问工具栏中的添加。

宏的录制

待验证、补入此博客

若要给宏设置使用的快捷键,则选择“键盘”,如下所示。

word文字编辑中的宏操作

插入指定格式、内容的字符

Sub InsertWords()

'

' InsertWords 宏

'

' FormatText 宏1 //在注释中插入自己想要描述的内容

'

Selection.Font.Bold = True //若在插入文字的代码前设置是否为粗体,则对接下来插入的字体起到设置作用

Selection.Font.Italic = True //设置是否为斜体

Selection.Font.Name = "Calibri"

Selection.Font.Size = 16

//此段代码会在当前光标的位置插入"Hello World!"

Selection.TypeText Text:="Hello World!" //设置要插入字体

//Selection.Font.Bold = True //若在插入文字的代码后设置是否为粗体,则对下一次插入的字体起到设置作用;请注意,一旦鼠标点击将会导致【加粗下一次插入字体的设置取消】

End Sub

选中word中的指定文字

Sub 宏1()

'

' 宏1 宏

'

' 选取Word文档中的文本

Dim WordDoc As Word.Document

Set WordDoc = ActiveDocument

Dim WordRange As Word.Range

Set WordRange = WordDoc.Range(Start:=0, End:=10)

WordRange.Select

//自认

Selection.。。。//针对上一步已选取的文本进行操作

//显示对话框,对话框中的内容为“完成!!”

MsgBox "完成!!"

End Sub

查找word中的指定文字A,并替换为指定文字B

Sub WordExchanger() //此处定义的函数名需与添加按钮时一致

1: Selection.Find.ClearFormatting

2: Selection.Find.Replacement.ClearFormatting

3: With Selection.Find //此处需要分行才能连续调用“.方法名”,即左侧的3:、4:

4: .Text = "home"

5: .Replacement.Text = "替换后的字符"

6: .Forward = True

7: .Wrap = wdFindContinue

8: .Format = Flase

9: .MatchCase = False

10: .MatchWholeWord = False

11: .MatchWildcards = False

12: .MatchSoundsLike = False

13: .MatchAllWordForms = False

14: End With //使用With时,需要使用End With结束

15: Selection.Find.Execute Replace:=wdReplaceAll

End Sub

插入文本框并向内插入文字

Sub InsertWords()

' 插入文本框 宏

Dim myTextbox As Shape

With ActiveDocument

Set myTextbox = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _

Left:=50, Top:=50, Width:=100, Height:=50)

With myTextbox.TextFrame

.TextRange.Text = "Hello, World"

.HorizontalAnchor = msoAnchorCenter

.VerticalAnchor = msoAnchorMiddle

End With

With myTextbox.Line

.Visible = msoTrue

.ForeColor.RGB = RGB(0, 0, 255)

.Weight = 1

End With

End With

End Sub

word 表格中的宏操作

批量删除所有表格的第一列

Sub 删除()

' 删除 宏

For i = 1 To ActiveDocument.Tables.Count

ActiveDocument.Tables(i).Columns(1).Delete

Next i

End Sub

批量调整表中各列的宽度

Sub changeTableWidth()

' 批量调整表中各列的宽度

On Error Resume Next

Dim t As Table

For Each t In ActiveDocument.Tables

t.Select

Selection.Cells.DistributeWidth

Selection.Columns.PreferredWidthType = wdPreferredWidthPoints

' 第1列宽度值(单位:厘米)设置为1,需要通过CentimetersToPoints方法。

' 若直接1~6列全部依次为 Selection.Columns(1).PreferredWidth = 1、2、3、4、5、6,运行结果无效、所有表格均变为同等大小的小宽度表格。

Selection.Columns(1).PreferredWidth = CentimetersToPoints(1)

' 第2列宽度值(单位:厘米)

Selection.Columns(2).PreferredWidth = CentimetersToPoints(2)

' 第3列宽度值(单位:厘米)

Selection.Columns(3).PreferredWidth = CentimetersToPoints(3)

' 第4列宽度值(单位:厘米)

Selection.Columns(4).PreferredWidth = CentimetersToPoints(4)

' 第5列宽度值(单位:厘米)

Selection.Columns(5).PreferredWidth = CentimetersToPoints(5)

' 第6列宽度值(单位:厘米)

Selection.Columns(6).PreferredWidth = CentimetersToPoints(6)

Next

Selection.HomeKey Unit:=wdStory

End Sub

在第一行前插入n行

n=1时,即在第一行前插入一行

Sub InsertOneLinesBeforeFirstLine()

'

' 第一行前插入1行 宏

For i = 1 To ActiveDocument.Tables.Count

Dim oDoc As Document

Set oDoc = Word.ActiveDocument

Dim oT As Table

Dim oRow As Row

Dim oColumn As Column

With oDoc

Set oT = .Tables(i)

With oT

'设置要在第几行前面插入行,这里是第1行

Set oRow = .Rows(1)

'插入行

.Rows.Add oRow

End With

End With

Next i

End Sub

n=8时,即在第一行前插入八行

Sub InsertEightLinesBeforeFirstLine()

' 在第一行前插入8行 宏

For i = 1 To ActiveDocument.Tables.Count

'设置要在第几行前面插入行,这里是第1行

ActiveDocument.Tables(i).Rows(1).Select

Selection.InsertRowsAbove 8

Next i

End Sub

在表格指定格内填入文字

在表格每列第一个格子(逐行)填入指定文字(格中已有文字不删)

Sub 填充()

' 填充 宏

For i = 1 To ActiveDocument.Tables.Count

'1行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(1, 1).Range.InsertAfter "" & "名称"

'2行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(2, 1).Range.InsertAfter "" & "注释"

'3行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(3, 1).Range.InsertAfter "" & "XX"

'4行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(4, 1).Range.InsertAfter "" & "XX"

'5行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(5, 1).Range.InsertAfter "" & "XX"

'6行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(6, 1).Range.InsertAfter "" & "XX"

'7行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(7, 1).Range.InsertAfter "" & "XX"

'8行1列 直接插入指定文字

ActiveDocument.Tables(i).Cell(8, 1).Range.InsertAfter "" & "XX"

Next i

End Sub

在表格每列第一个格子(逐行)填入指定文字(覆盖格中已有文字)

Sub 填充()

' 填充 宏

For i = 1 To ActiveDocument.Tables.Count

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(1, 1).Range.Delete

ActiveDocument.Tables(i).Cell(1, 1).Range.InsertAfter "" & "名称"

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(2, 1).Range.Delete

ActiveDocument.Tables(i).Cell(2, 1).Range.InsertAfter "" & "注释"

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(3, 1).Range.Delete

ActiveDocument.Tables(i).Cell(3, 1).Range.InsertAfter "" & "XX"

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(4, 1).Range.Delete

ActiveDocument.Tables(i).Cell(4, 1).Range.InsertAfter "" & "XX"

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(5, 1).Range.Delete

ActiveDocument.Tables(i).Cell(5, 1).Range.InsertAfter "" & "XX"

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(6, 1).Range.Delete

ActiveDocument.Tables(i).Cell(6, 1).Range.InsertAfter "" & "XX"

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(7, 1).Range.Delete

ActiveDocument.Tables(i).Cell(7, 1).Range.InsertAfter "" & "XX"

'先删除当前格内文字,再插入

ActiveDocument.Tables(i).Cell(8, 1).Range.Delete

ActiveDocument.Tables(i).Cell(8, 1).Range.InsertAfter "" & "XX"

Next i

End Sub

查找表格中含有指定内容的单元格

选中word文档中的所有表格

Sub SelectAllTable()

'

'选中word文档中的所有表格

'

Dim tempTable As Table

Application.ScreenUpdating = False

'判断文档是否被保护'

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then

MsgBox "文档已保护,此时不能选中多个表格!!"

Exit Sub

End If

'删除所有可编辑的区域

ActiveDocument.DeleteAllEditableRanges wdEditorEveryone

'添加可编辑区域

For Each tempTable In ActiveDocument.Tables

tempTable.Range.Editors.Add wdEditorEveryone

Next

'选中所有可编辑区域

ActiveDocument.SelectAllEditableRanges wdEditorEveryone

'删除所有可编辑的区域

ActiveDocument.DeleteAllEditableRanges wdEditorEveryone

Application.ScreenUpdating = True

End Sub

遍历表格中的所有内容

Sub ReadTableData()

Dim wdApp As Word.Application

Dim wdDoc As Word.Document

Dim wdTable As Word.Table

Dim i As Long, j As Long

Set wdApp = New Word.Application

'此处要填写 要读取的word文档中的内容'

Set wdDoc = wdApp.Documents.Open("C:\Data\test.docx")

'选中第一张表格

Set wdTable = wdDoc.Tables(1)

'遍历表格数据

For i = 1 To wdTable.Rows.Count

For j = 1 To wdTable.Columns.Count

Debug.Print wdTable.Cell(i, j).Range.Text

Next j

Next i

wdDoc.Close SaveChanges:=False

wdApp.Quit

End Sub

批量设置表格中所有内容文字为居中对齐

Sub 表格内容文字居中对齐()

Dim biaoge As Table

For Each biaoge In ActiveDocument.Tables

biaoge.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

biaoge.Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter

Next biaoge

End Sub

Sub 居中()

' 居中 宏

For i = 1 To ActiveDocument.Tables.Count

ActiveDocument.Tables(i).Columns(1).Select

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Next i

End Sub

取消表格中指定格子四周的边框

For i = 1 To ActiveDocument.Tables.Count

'取消指定表格的四维边框

ActiveDocument.Tables(i).Cell(1, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(1, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(1, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(1, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(2, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(2, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(2, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(2, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(3, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(3, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(3, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(3, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(4, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(4, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(4, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(4, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(5, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(5, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(5, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(5, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(6, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(6, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(6, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(6, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(7, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(7, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(7, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(7, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(8, 3).Borders.Enable = False

ActiveDocument.Tables(i).Cell(8, 4).Borders.Enable = False

ActiveDocument.Tables(i).Cell(8, 5).Borders.Enable = False

ActiveDocument.Tables(i).Cell(8, 6).Borders.Enable = False

ActiveDocument.Tables(i).Cell(1, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(2, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(3, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(4, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(5, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(6, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(7, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(8, 2).Borders.Enable = True

ActiveDocument.Tables(i).Cell(9, 3).Borders.Enable = True

ActiveDocument.Tables(i).Cell(9, 4).Borders.Enable = True

ActiveDocument.Tables(i).Cell(9, 5).Borders.Enable = True

ActiveDocument.Tables(i).Cell(9, 6).Borders.Enable = True

Next i

End Sub

合并单元格

Sub mergeTable()

Dim tal As Table, i%

For Each tal In ActiveDocument.Tables

For i = 1 To 4

'范围是每行第4列表格的开始 和 每行第5列表格的结束 合并

ActiveDocument.Range(tal.Cell(i, 4).Range.Start, tal.Cell(i, 5).Range.End).Cells.Merge

Next i

Next

End Sub

支持flowchart的流程图:

关于 Flowchart流程图 语法,参考 这儿.

相关数据包

停车位智能管理系统有哪些软件

停车位智能管理系统有哪些软件

📅 08-11 🔗 足球365是什么意思
司马发现这家体育馆,环境不错!——探秘司马健身运动游泳馆
饥荒春季怎么过 饥荒春季生存攻略详解

饥荒春季怎么过 饥荒春季生存攻略详解

📅 07-13 🔗 best365官网登录
阴阳师赤魂哪里多

阴阳师赤魂哪里多

📅 09-12 🔗 足球365是什么意思
← 倍福 | 0 | 入门 《博德之门3》巨龙安苏支线怎么做?巨龙安苏支线攻略 →