一. 方法: Word打开方法时调用的方法:Document_Open() Word关闭时调用的方法:Document_Close() 调用subInt过程:Call subInt Function fun() As Integer ‘代码 Fun=1 End Function 调用fun函数:在其他调用的方法或函数中直接用函数(因为函数要返回一个值),比如: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 '创建FSO对象 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中的内容的回车和换行都替换为空。 Strings.Trim(Strings.Replace(Strings.Replace(ThisDocument.TextBox1.text, Chr(13), ""), Chr(10), "")) 1.访问标签lbel1的值:thisdocument.label1.caption 2.textbox1的值.textbox1.text 3.combobox1的值:combobox1.text 五:窗体 Userform中的textbox中光标可能无法停留在textbox框的前面,应该设置它的属性 selecttionmargin=false 二.选中table表中的行列的值, 1.首先定义表量,如果当前的table在word中的位置是第一张table,则n=1,是第几张表(必须去数没有table表的标示),n就为几。 set mytable=activedocument.tables(n) 第一种方法:(不适用表中有合并单元格的表) mytable.rows(2).cells(2).select '选中表的2行2列的值 selection.typetext textbox1.text '将选中的区域的值修改为当前textbox1的值, 第二种方法:(适用于所有的表,包括表中有合并的单元格) myTable.Cell(2, 2)=textbox1.text 三.类型转化 Cint():强制转化为整形 CStr():强制转化为字符串型 2.提取时间的年月日 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. 四.弹出对话框 MsgBox("信息不能为空"),vbOKOnly+vbExclamation 五.控件 1.textbook textbox获得焦点 TextBox1.SetFocus 2.ComboBox 2.1combobox1的下拉列表加的内容 Private Sub ComboBox1_DropButtonClick() Dim oldValue As String oldValue = ComboBox1.text ComboBox1.Clear ComboBox1.AddItem ("北京地区") ComboBox1.AddItem ("沈阳地区") 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关联的combobox3的下拉列表将会显示的内容 Private Sub ComboBox3_DropButtonClick() Dim oldValue As String oldValue = ComboBox3.text ComboBox3.Clear Select Case ComboBox1.value Case "数学" ComboBox3.AddItem ("数学史") ComboBox3.AddItem ("数理逻辑与数学基础") ComboBox.AddItem ("数论") Case "统计学" ComboBox3.AddItem ("统计学其他学科") End Select initComboValue ComboBox3, oldValue End Sub 2.4如果有“请填写”选项的话,需要改变ComboBox的样式 Private Sub ComboBox2_Click() If ComboBox2.text <> "请填写" And ComboBox2.text <> "" Then ComboBox2.Style = fmStyleDropDownList Else ComboBox2.Style = fmStyleDropDownCombo End If End Sub 六.关闭窗体 1.关闭窗体: Unload me '在方法里写这句话就行 七:菜单栏生成按钮 在菜单栏创建按钮控件,比如打开word文档时生成按钮。 Public Sub Load_button() Set mybut = CommandBars.FindControl(Type:=msoControlButton, Tag:="完成") '添加按钮 Set mybar = CommandBars.Add(Name:="学术研讨控件", 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 ******************************************************************************* 当点击次“完成”按钮时,调用过程Produces_xml Private Sub Produces_xml() ‘代码 End Sub 八:生成xml文件 在当前目录下生成一个xml Public Sub Produces_xml() Dim txtMy As String '文件的内容变量 ‘文件名 txtFlname = ThisDocument.Path + "/" + Strings.Replace(f1, Chr(13), "") + ".xml" ‘文件内容 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 '写入文件内容 Close #1 MsgBox ("信息已经生成!"), vbOKOnly + vbExclamation End Sub
九.文档保护:当程序写完以后要进行word文档保护,工具-文档保护-编辑限制-填写窗体-启动文档强制保护。 但用户要调用后天的程序,所以要在用户调用程序时,自动取消文档保护,离开程序时,再自动保护(密码是yjt),所以一般主调方法里要加入此段代码: *********************************************************** '取消文档保护 If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Password:="yjt" ‘你要写的程序,放在中间 '打开文档保护 ActiveDocument.Protect Password:="yjt", NoReset:=False, Type:= _ wdAllowOnlyFormFields **************************************************************** 在设计模式下不能保护,要退出设计模式后才可以保护。
十.Textbook框填写最大的内容,屏蔽回车和自动换行,使Textbook框中填写的内容达到最大值时不能自动换行。 Dim h As Integer Dim y As Integer Dim oldText As String
Private Sub main() ‘现在不太清楚main方法是否在程序的一开始自动调用的,好像不用也可以,只是对oldText的初始化 oldText = "" End Sub ‘调用过程,当textbox23改变时调用下面的方法 Private Sub TextBox23_Change() Call LineStr(TextBox23) End Sub ‘当textbox23获得焦点时,oldText的赋值为空,隔断它与上面textbox框的连接,因为oldText可能会保存上一个textbox的内容 Private Sub TextBox23_GotFocus() oldTextt=”” End Sub ‘主要过程 Private Sub LineStr(text As TextBox) h = text.LineCount ‘记录textbox中用户输入的总行数 y = CInt(text.Height / 11) ‘求textbox能容纳的总行数,一种字体对应不同的参数,例如小四是11 If (h > y) Then text.EnterKeyBehavior = False ‘使回车键失效 MsgBox ("字数或行数超过限制 !") text.value = oldText Else text.EnterKeyBehavior = True text.MultiLine = True oldText = text.value End If End Sub 十一.系统时间 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中的表格数: ActiveDocument.Tables.Count 13,过滤字符串 '过滤字符串函数 '去掉回车,换行<>等特殊字符, 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 '初始化变量,来表明是否可以关闭窗体 For Each i In Me.Controls '遍历当前窗体下的子控件 If TypeName(i) = "TextBox" Then i.value = Strings.Trim(i.value) '去掉多余的空格 If i.value = "" Then checkmess = 0 '不能关闭窗体 MsgBox ("信息不能为空!"), vbOKOnly + vbExclamation '如果此控件可以编辑,则可以获得焦点,否则不行 If i.Enabled = True Then i.SetFocus End If Exit For End If End If Next End Function 15.word中textbox框的样式 '在程序员设计表格的时候可以调用此方法,设置textbox的样式 '一般将其放在限定字数的函数内,这样每在textbox框中输入一次就会调用此方法,省去了繁琐的劳动 '程序员将页面设计完成后,此方法就没用了,将其删除掉 private sub textboxstyle(text as TextBox) text.BorderColor = &H80000005 text.EnterKeyBehavior = True text.MultiLine = True text.BorderStyle = fmBorderStyleSingle end sub |
|