一. 方法:
Word打開方法時(shí)調(diào)用的方法:Document_Open()
Word關(guān)閉時(shí)調(diào)用的方法:Document_Close()
調(diào)用subInt過程:Call subInt
Function fun() As Integer
‘代碼
Fun=1
End Function
調(diào)用fun函數(shù):在其他調(diào)用的方法或函數(shù)中直接用函數(shù)(因?yàn)楹瘮?shù)要返回一個(gè)值),比如:if(fun=1)then….end if
1.1操作文件:
Private Sub deleteXML()
‘判斷文件是否存在,然后刪除文件
If Dir(ThisDocument.Path + "/" + filename1) <> "" Then
Kill (ThisDocument.Path + "/" + filename1) ‘
End If
End Sub
Private Sub rename()
‘重命名文件
If Dir(ThisDocument.Path + "/" + "temp.xml") <> "" Then
Name ThisDocument.Path & "/" & "temp.xml" As ThisDocument.Path & "/" & filename1
End If
End Sub
‘取得同目錄下文件的名字
Private Sub HX_FileName()
Dim fso As Object, folder As Object, subfolder As Object, objFile As Scripting.file '創(chuàng)建FSO對(duì)象
Set fso = CreateObject("scripting.filesystemobject")
Set folder = fso.GetFolder(Path)
For Each objFile In folder.Files
If Strings.Right(objFile.Name, 8) = "gplx.xml" And Len(objFile.Name) > 8 Then filename1 = objFile.Name
Next
End Sub
二. 聲明變量
Dim a as Integer
Public mybut As CommandBarButton
Public mybar As CommandBar
三. 常用語句
1.If(a=1) then
Else
End if
2.do while a<26
Loop
3. For ID = 0 To count - 1 Step 1
pidtemp = “123”
exit for ‘如果要跳出for語句,就加上此句
Next ID
四:字符串
字符串的替換:將textbox1中的內(nèi)容的回車和換行都替換為空。
Strings.Trim(Strings.Replace(Strings.Replace(ThisDocument.TextBox1.text, Chr(13), ""), Chr(10), ""))
1.訪問標(biāo)簽lbel1的值:thisdocument.label1.caption
2.textbox1的值.textbox1.text
3.combobox1的值:combobox1.text
五:窗體
Userform中的textbox中光標(biāo)可能無法停留在textbox框的前面,應(yīng)該設(shè)置它的屬性
selecttionmargin=false
二.選中table表中的行列的值,
1.首先定義表量,如果當(dāng)前的table在word中的位置是第一張table,則n=1,是第幾張表(必須去數(shù)沒有table表的標(biāo)示),n就為幾。
set mytable=activedocument.tables(n)
第一種方法:(不適用表中有合并單元格的表)
mytable.rows(2).cells(2).select '選中表的2行2列的值
selection.typetext textbox1.text '將選中的區(qū)域的值修改為當(dāng)前textbox1的值,
第二種方法:(適用于所有的表,包括表中有合并的單元格)
myTable.Cell(2, 2)=textbox1.text
三.類型轉(zhuǎn)化
Cint():強(qiáng)制轉(zhuǎn)化為整形
CStr():強(qiáng)制轉(zhuǎn)化為字符串型
2.提取時(shí)間的年月日
DateTime.Year(2008-10-9)
DateTime.Month()
DateTime.Day()
Label1.Caption = DateTime.Year(DateTime.Date)
Label2.Caption = DateTime.Month(DateTime.Date)
Label3.Caption = DateTime.Day(DateTime.Date)
3.
四.彈出對(duì)話框
MsgBox("信息不能為空"),vbOKOnly+vbExclamation
五.控件
1.textbook
textbox獲得焦點(diǎn)
TextBox1.SetFocus
2.ComboBox
2.1combobox1的下拉列表加的內(nèi)容
Private Sub ComboBox1_DropButtonClick()
Dim oldValue As String
oldValue = ComboBox1.text
ComboBox1.Clear
ComboBox1.AddItem ("北京地區(qū)")
ComboBox1.AddItem ("沈陽地區(qū)")
initComboValue ComboBox1, oldValue
End Sub
2.2initCombovalue方法
Private Sub initComboValue(ByRef comBox As ComboBox, ByVal oldValue As String)
Dim i As Integer
For i = 0 To comBox.ListCount - 1
If comBox.List(i) = oldValue Then
comBox.ListIndex = i
Exit Sub
End If
Next i
End Sub
2.3 與combobox1關(guān)聯(lián)的combobox3的下拉列表將會(huì)顯示的內(nèi)容
Private Sub ComboBox3_DropButtonClick()
Dim oldValue As String
oldValue = ComboBox3.text
ComboBox3.Clear
Select Case ComboBox1.value
Case "數(shù)學(xué)"
ComboBox3.AddItem ("數(shù)學(xué)史")
ComboBox3.AddItem ("數(shù)理邏輯與數(shù)學(xué)基礎(chǔ)")
ComboBox.AddItem ("數(shù)論")
Case "統(tǒng)計(jì)學(xué)"
ComboBox3.AddItem ("統(tǒng)計(jì)學(xué)其他學(xué)科")
End Select
initComboValue ComboBox3, oldValue
End Sub
2.4如果有“請(qǐng)?zhí)顚憽边x項(xiàng)的話,需要改變ComboBox的樣式
Private Sub ComboBox2_Click()
If ComboBox2.text <> "請(qǐng)?zhí)顚? And ComboBox2.text <> "" Then
ComboBox2.Style = fmStyleDropDownList
Else
ComboBox2.Style = fmStyleDropDownCombo
End If
End Sub
六.關(guān)閉窗體
1.關(guān)閉窗體:
Unload me '在方法里寫這句話就行
七:菜單欄生成按鈕
在菜單欄創(chuàng)建按鈕控件,比如打開word文檔時(shí)生成按鈕。
Public Sub Load_button()
Set mybut = CommandBars.FindControl(Type:=msoControlButton, Tag:="完成")
'添加按鈕
Set mybar = CommandBars.Add(Name:="學(xué)術(shù)研討控件", Position:=msoBarTop)
mybar.Visible = True
Set mybut = mybar.Controls.Add(Type:=msoControlButton)
mybut.Caption = "完成"
mybut.Style = msoButtonCaption
mybut.Tag = "完成"
mybut.OnAction = "Produces_xml"
End Sub
*******************************************************************************
當(dāng)點(diǎn)擊次“完成”按鈕時(shí),調(diào)用過程Produces_xml
Private Sub Produces_xml()
‘代碼
End Sub
八:生成xml文件
在當(dāng)前目錄下生成一個(gè)xml
Public Sub Produces_xml()
Dim txtMy As String '文件的內(nèi)容變量
‘文件名
txtFlname = ThisDocument.Path + "/" + Strings.Replace(f1, Chr(13), "") + ".xml"
‘文件內(nèi)容
txtMy = "<?xml version=""1.0"" encoding=""gb2312""?>" + vbCrLf + "<informations>" + vbCrLf + "<information>"
txtMy = txtMy + vbCrLf + "</information>" + vbCrLf + "</informations>"
Open txtFlname For Output As #1
Print #1, txtMy 'dText '寫入文件內(nèi)容
Close #1
MsgBox ("信息已經(jīng)生成!"), vbOKOnly + vbExclamation
End Sub
九.文檔保護(hù):當(dāng)程序?qū)懲暌院笠M(jìn)行word文檔保護(hù),工具-文檔保護(hù)-編輯限制-填寫窗體-啟動(dòng)文檔強(qiáng)制保護(hù)。
但用戶要調(diào)用后天的程序,所以要在用戶調(diào)用程序時(shí),自動(dòng)取消文檔保護(hù),離開程序時(shí),再自動(dòng)保護(hù)(密碼是yjt),所以一般主調(diào)方法里要加入此段代碼:
***********************************************************
'取消文檔保護(hù)
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="yjt"
‘你要寫的程序,放在中間
'打開文檔保護(hù)
ActiveDocument.Protect Password:="yjt", NoReset:=False, Type:= _
wdAllowOnlyFormFields
****************************************************************
在設(shè)計(jì)模式下不能保護(hù),要退出設(shè)計(jì)模式后才可以保護(hù)。
十.Textbook框填寫最大的內(nèi)容,屏蔽回車和自動(dòng)換行,使Textbook框中填寫的內(nèi)容達(dá)到最大值時(shí)不能自動(dòng)換行。
Dim h As Integer
Dim y As Integer
Dim oldText As String
Private Sub main()
‘現(xiàn)在不太清楚main方法是否在程序的一開始自動(dòng)調(diào)用的,好像不用也可以,只是對(duì)oldText的初始化
oldText = ""
End Sub
‘調(diào)用過程,當(dāng)textbox23改變時(shí)調(diào)用下面的方法
Private Sub TextBox23_Change()
Call LineStr(TextBox23)
End Sub
‘當(dāng)textbox23獲得焦點(diǎn)時(shí),oldText的賦值為空,隔斷它與上面textbox框的連接,因?yàn)閛ldText可能會(huì)保存上一個(gè)textbox的內(nèi)容
Private Sub TextBox23_GotFocus()
oldTextt=””
End Sub
‘主要過程
Private Sub LineStr(text As TextBox)
h = text.LineCount ‘記錄textbox中用戶輸入的總行數(shù)
y = CInt(text.Height / 11) ‘求textbox能容納的總行數(shù),一種字體對(duì)應(yīng)不同的參數(shù),例如小四是11
If (h > y) Then
text.EnterKeyBehavior = False ‘使回車鍵失效
MsgBox ("字?jǐn)?shù)或行數(shù)超過限制 !")
text.value = oldText
Else
text.EnterKeyBehavior = True
text.MultiLine = True
oldText = text.value
End If
End Sub
十一.系統(tǒng)時(shí)間
Private Sub UserForm_Activate()
d1 = CStr(DateTime.Year(DateTime.Date))
d2 = CStr(DateTime.Month(DateTime.Date))
d3 = CStr(DateTime.Day(DateTime.Date))
TextBox7.Text = d1 + "-" + d2 + "-" + d3
End Sub
12.得到word中的表格數(shù):
ActiveDocument.Tables.Count
13,過濾字符串
'過濾字符串函數(shù)
'去掉回車,換行<>等特殊字符,
Function xmlStr(str As String) As String
str1 = Strings.Trim(Strings.Replace(Strings.Replace(str, Chr(13), ""), "", ""))
str2 = Strings.Replace(Strings.Replace(str1, "&", "&"), ">", ">")
xmlStr = Strings.Replace(Strings.Replace(str2, "<", "<"), Chr(10), "")
End Function
13.處理字符串
Private Function checkmess() As Integer
Dim i As Object, t As Integer
checkmess = 1 '初始化變量,來表明是否可以關(guān)閉窗體
For Each i In Me.Controls '遍歷當(dāng)前窗體下的子控件
If TypeName(i) = "TextBox" Then
i.value = Strings.Trim(i.value) '去掉多余的空格
If i.value = "" Then
checkmess = 0 '不能關(guān)閉窗體
MsgBox ("信息不能為空!"), vbOKOnly + vbExclamation
'如果此控件可以編輯,則可以獲得焦點(diǎn),否則不行
If i.Enabled = True Then
i.SetFocus
End If
Exit For
End If
End If
Next
End Function
15.word中textbox框的樣式
'在程序員設(shè)計(jì)表格的時(shí)候可以調(diào)用此方法,設(shè)置textbox的樣式
'一般將其放在限定字?jǐn)?shù)的函數(shù)內(nèi),這樣每在textbox框中輸入一次就會(huì)調(diào)用此方法,省去了繁瑣的勞動(dòng)
'程序員將頁面設(shè)計(jì)完成后,此方法就沒用了,將其刪除掉
private sub textboxstyle(text as TextBox)
text.BorderColor = &H80000005
text.EnterKeyBehavior = True
text.MultiLine = True
text.BorderStyle = fmBorderStyleSingle
end sub
聯(lián)系客服