返回目录:word文档
手头电脑没装2007.记得是要打开“开发模式”,zhidao办法是:
(窗口左上角的按扭)单击版-->属性(或准备)-->选项-->(勾选)开发模式-->(界面菜单位置新增一项“开发模式”)点击选择(里面有宏和VB编辑器)
---个别词语可能权不准确,大概是没错.
一、编制程序
1.启动Word,连续点击“工具”,“宏”,“录制新宏...”,在弹出的对话框中填写宏名(此例为“作文7a686964616fe78988e69d83330稿纸”),选择宏要保存的位置(可以保存在模板中,也可以保存在当前文档中),如图1所示。
2.为了方便操作,可以将此宏以按钮的形式指定在工具栏中。单击“录制宏”对话框中的“工具栏”按钮,在弹出的“自定义”对话框的右侧,选中宏命令“Normal.NewMacros.作文稿纸”,用鼠标将其拖动复制到“常用”工具栏,在其上单击右键,选择相应的命令修改名称,为其编辑一个小图标,结果如图2所示。
3.单击“录制宏”对话框中的“关闭”按钮,屏幕上将出现一个“录制宏”工具栏,单击停止按钮停止录制。
4.依次选择“工具”、“宏”、“宏…”,在对话框中选择宏名“作文稿纸”,单击“编辑”按钮,打开vba编程窗口。录入如下代码:
Sub 作文稿纸()
UserForm1.CommandButton1.Enabled = True
UserForm1.Show
End Sub
5.插入一个窗体UserForm1,在其中插入4个标签,4个文本框,一个按钮。分别设置它们的相关属性,其中,“所需行数”文本框的“text”属性值设为25,“所需列数”文本框的“text”属性值设为20,“行间距”文本框的“text”属性值设为0.5,“首尾空行高度”文本框的“text”属性值设为0.4,如图3所示。
6.双击命令按钮CommandButton1,录入以下代码:
Private Sub CommandButton1_Click()
Dim n As Integer '定义一个变量为整数型
n = 1
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=Val(TextBox1.Text) * 2 + 1, NumColumns _
:=Val(TextBox2.Text), DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
Selection.EndKey Unit:=wdRow, Extend:=True
Selection.Cells.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
Selection.Tables(1).Rows.HeightRule = wdRowHeightExactly
'设定表格行高为固定值
Selection.Tables(1).Rows.Height = CentimetersToPoints(Val(TextBox3.Text))
'设置表格行高为设置值,作为行间距
Selection.Tables(1).Rows(1).Height = CentimetersToPoints(Val(TextBox4.Text))
'设置第一行行高为设置值
Do While n < Val(TextBox1.Text) + 1
Selection.EndKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=2
'将插入点移至下一行
Selection.Tables(1).Rows(2 * n).Height = Selection.Tables(1).Columns(1).PreferredWidth
'设行高等于列宽
Selection.EndKey Unit:=wdRow, Extend:=True
Selection.EndKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=2
'将插入点移至下一行
Selection.EndKey Unit:=wdRow, Extend:=True
Selection.Cells.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
'去除此行的内部框线,只余边框
n = n + 1
Loop
Selection.Tables(1).Rows(Val(TextBox1.Text) * 2 + 1).Height = CentimetersToPoints(Val(TextBox4.Text))
'设置末行高为设置值
Selection.EndKey Unit:=wdRow, Extend:=True
Selection.Cells.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
'表格居中
With Selection.Tables(1)
.Borders(wdBorderLeft).LineWidth = wdLineWidth150pt
.Borders(wdBorderRight).LineWidth = wdLineWidth150pt
.Borders(wdBorderTop).LineWidth = wdLineWidth150pt
.Borders(wdBorderBottom).LineWidth = wdLineWidth150pt
'设定表格边框为粗线
End With
Selection.EndKey Unit:=wdLine
Unload Me
End Sub
在Word2010文档窗口显示“开发工具”选项卡
“开发工具”功能区中包括VBA代码、宏代码、模板和控件等Word2010开发工具,默认情况下,“开发工具”选项卡并未显示在百Word2010窗口中,用户需要手动设置使其显示,操作步骤如下所述:
第度1步,打开Word2010文档窗口,依次单击“文件”→“选项”按钮,如图1所示。
图1 单击“选项”按钮
第2步,在打开的“回Word选项”对话框中切换到“自定义功能区”选项卡,在“自定义功能区”区域的“主选项卡”列表中选中“开发工具”复选框,并单击“确定”按钮,如图2所示。
图2 选中“开发工具”复选框
第3步,返回Word2010文档窗口,可以在功能区看到答“开发工具”选项卡,如图3所示。
图3 “开发工具”选项卡
怎么在知word2007中使用VBA的方法:
道打开word2007
如果功能区有开发工具选项,跳到步骤6,如果没有,继续
打开文件选项
点版击word选项
选择“在功能区显示‘权开发工具’选项”
点击“开发工具”选项
点击“Visual Basic”选项
出现Visual Basic页面