① 使用VBA編程實現word公式編輯代碼
sub 1722187970()
dim m,n
m=inputbox("請輸入m的值")
n=inputbox("請輸入n的值")
msgbox "m+n的結果為" & m+n
end sub
② word如何用vba編程將重復同一內容替換為不同內容
Sub 替換()
On Error GoTo xxx:
Dim Rng As Range, n&, arr, a$
a = ActiveDocument.Content.Text
Set Rng = ActiveDocument.Content
With Rng.Find
.ClearFormatting
.Replacement.ClearFormatting
arr = Array("白日依山盡", "黃河入海流", "欲窮千里目", "更上一層樓")
Do While .Execute(findtext:="白日依山盡")
Rng = arr(n)
Rng.SetRange Rng.End, ActiveDocument.Content.End
n = n + 1
Loop
End With
GoTo yyy:
xxx:
MsgBox "文檔中詞條「白日依山盡」數目超出4個!"
yyy:
MsgBox "轉換完成!"
End Sub
③ 如何在Word中打開VBA編程窗口
一、編製程序
1.啟動Word,連續點擊「工具」,「宏」,「錄制新宏...」,在彈出的對話框中填寫宏名(此例為「作文稿紙」),選擇宏要保存的位置(可以保存在模板中,也可以保存在當前文檔中),如圖1所示。
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