1、循环在WORD+VBA中的应用循环在WORD VBA 中的应用001在活动文档的开头插入一张 4列3行的表格。For Each.Next结构用于循环遍历表格中的每个单元格。在 For Each.Next结构中,InsertAfter方法用于将文字添至表格单元格(单元格1、单元格2、以此类推)。Sub CreateNewTable()Dim docActive As DocumentDim tblNew As TableDim celTable As CellDim intCount As IntegerSet docActive = ActiveDocumentSet tblNew = _Ra
2、nge:=(Start:=0, End:=0), NumRows:=3, _NumColumns:=4)intCount = 1For Each celTable In Cell & intCountintCount = intCount + 1Next celTableFormat:=wdTableFormatColorful2, _ApplyBorders:=True, ApplyFont:=True, ApplyColor:=TrueEnd Sub002在活动文档中第一张表格的第一个单元格中插入文字。 Cell方法返回单独的 Cell对象。Range属性返回一个 Range对象。Dele
3、te方法用于删除现有的文字, 而InsertAfter方法用于插入文字“ Cell 1,T 。Sub InsertTextInCell()If = 1 ThenWith (1).Cell(Row:=1, Column:=1).Range.Delete.InsertAfter Text:=Cell 1,1End WithEnd IfEnd Sub003返回并显示文档中第一张表格的第一行中每个单元格的内容。Sub ReturnTableText()Dim tblOne As TableDim celTable As CellDim rngTable As RangeSet tblOne = (1)
4、For Each celTable In (1).CellsSet rngTable = (Start:= _End:= - 1)MsgBoxNext celTableEnd SubSub ReturnCellText()Dim celTable As CellDim rngTable As RangeSet tblOne = (1)For Each celTable In (1).CellsSet rngTable =Unit:=wdCharacter, Count:=-1MsgBoxNext celTableEnd Sub004在活动文档的开头插入用制表符分隔的文本,然后将这些文本转换为表
5、格。Sub ConvertExistingText()With .InsertBefore one & vbTab & two & vbTab & three & vbCr.ConvertToTable Separator:=Chr(9), NumRows:=1, NumColumns:=3 End WithEnd Sub005定义一个数组,该数组的元素个数等于文档中第一张表格(假定为 Option Base 1)中的单元格数。For Each.Next结构用于返回每个表格单元格的内容,并将文字指定给相应的 数组元素。Sub ReturnCellContentsToArray()Dim int
6、Cells As IntegerDim celTable As CellDim strCells() As StringDim intCount As IntegerDim rngText As RangeIf = 1 ThenWith .Range intCells =. ReDim strCells(intCells) intCount = 1 For Each celTable In .Cells Set rngText =Unit:=wdCharacter, Count:=-1 strCells(intCount) = rngText intCount = intCount + 1Ne
7、xt celTableEnd WithEnd IfEnd Sub006将当前文档中的表格复制到新文档中。Sub CopyTablesToNewDoc()Dim docOld As DocumentDim rngDoc As RangeDim tblDoc As TableIf = 1 ThenSet docOld = ActiveDocumentSet rngDoc = End:=0)For Each tblDoc InWith rngDoc.Paste.Collapse Direction:=wdCollapseEnd .InsertParagraphAfter.Collapse Direc
8、tion:=wdCollapseEndEnd WithNextEnd IfEnd Sub007显示Documents集合中每个文档的名称。Sub LoopThroughOpenDocuments()Dim docOpen As DocumentFor Each docOpen In DocumentsMsgBoxNext docOpenEnd Sub008使用数组存储活动文档中包含的所有书签的名称。Sub LoopThroughBookmarks()Dim bkMark As BookmarkDim strMarks() As StringDim intCount As IntegerIf 0
9、 ThenReDim strMarks - 1)intCount = 0For Each bkMark InstrMarks(intCount)=intCount = intCount + 1Next bkMarkEnd IfEnd Sub009更新活动文档中的 DATE域。Sub UpdateDateFields()Dim fldDate As FieldFor Each fldDate InIf InStr(1, , Date, 1) ThenNext fldDateEnd Sub010如果名为Filename 的词条是 AutoTextEntries集合中的一部分,则以下示例显示条消息。
10、Sub FindAutoTextEntry()Dim atxtEntry As AutoTextEntryFor Each atxtEntry In If = Filename Then _MsgBox The Filename AutoText entry exists.Next atxtEntryEnd Sub011在第一个表格中添加一行,然后将文本 Cell插入该行。Sub CountCells()Dim tblNew As TableDim celTable As CellDim intCount As IntegerintCount = 1Set tblNew = (1)Set ro
11、wNew = For Each celTable InText:=Cell & intCountintCount = intCount + 1Next celTableEnd Sub012向新文档中添加一个 3行5列的表格,然后在表格的每个单元格中插入数据。Sub NewTable()Dim docNew As DocumentDim tblNew As TableDim intX As IntegerDim intY As IntegerSet docNew =Set tblNew = 3, 5)With tblNewFor intX = 1 To 3For intY = 1 To 5.Ce
12、ll(intX, intY). Cell: R & intX & , C & intYNext intYNext intXEnd WithEnd Sub013将Blue变量的值设为 6,如果该变量不存在, 本示例将该变量添加至文档, 并将值设为6。For Each aVar InIf = Blue Then num =Next aVarIf num = 0 ThenName:=Blue”, Value:=6Else(num).Value = 6End If014在文档关闭以前提示用户保存文档。Sub PromptToSaveAndClose()Dim doc As DocumentFor Ea
13、ch doc In DocumentsSaveChanges:=wdPromptToSaveChangesNextEnd Sub015若要确定文档是否处于打开状态,可使用For Each.Next语句列举Documents集合中 的元素。如果文档 是打开的,则下列示例激活该文档, 如果没有打开文档, 则将该文档打 开。Sub ActivateOrOpenDocument()Dim doc As DocumentDim docFound As BooleanFor Each doc In DocumentsIf InStr(1, , , 1) ThendocFound = TrueExit Fo
14、rElsedocFound = FalseEnd IfNext docIf docFound = False Then FileName:=End Sub016第三个多级符号列表模板创建另一种编号样式。Set myTemp = ListGalleries(wdOutlineNumberGallery).ListTemplates(3)For i = 1 to 9If i Mod 2 = 0 Then(i).NumberStyle = _wdListNumberStyleUppercaseRomanElse(i).NumberStyle = _wdListNumberStyleLowercaseRomanEnd IfNext i017 将活动文档中每个多级符号列表的编号样式更改为大写字母。For Each lt InFor Each ll In=wdListNumberStyleUppercaseLetterNext llNext lt018将活动文档页脚中的页码格式设置为小写罗马数字。For Each sec In(wdHeaderFooterPrimary).PageNumbers _.NumberStyle = wdPageNumberStyleLowercaseRomanNext sec019显示活动文档各列表的项数。For Each li InMsgBox
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1