资源预览内容
第1页 / 共70页
第2页 / 共70页
第3页 / 共70页
第4页 / 共70页
第5页 / 共70页
第6页 / 共70页
第7页 / 共70页
第8页 / 共70页
第9页 / 共70页
第10页 / 共70页
亲,该文档总共70页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
循环在 WORD VBA 中的应用第 1 页 共 70 页循环在 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 = docActive.Tables.Add( _Range:=docActive.Range(Start:=0, End:=0), NumRows:=3, _NumColumns:=4)intCount = 1For Each celTable In tblNew.Range.CellscelTable.Range.InsertAfter Cell & intCountintCount = intCount + 1Next celTabletblNew.AutoFormat Format:=wdTableFormatColorful2, _ApplyBorders:=True, ApplyFont:=True, ApplyColor:=TrueEnd Sub002在活动文档中第一张表格的第一个单元格中插入文字。Cell 方法返回单独的 Cell 对象。Range 属性返回一个 Range 对象。Delete 方法用于删除现有的文字,而 InsertAfter 方法用于插入文字“Cell 1,1”。Sub InsertTextInCell()If ActiveDocument.Tables.Count = 1 ThenWith ActiveDocument.Tables(1).Cell(Row:=1, Column:=1).Range.Delete.InsertAfter Text:=Cell 1,1End WithEnd IfEnd Sub003返回并显示文档中第一张表格的第一行中每个单元格的内容。Sub ReturnTableText()Dim tblOne As TableDim celTable As Cell循环在 WORD VBA 中的应用第 2 页 共 70 页Dim rngTable As RangeSet tblOne = ActiveDocument.Tables(1)For Each celTable In tblOne.Rows(1).CellsSet rngTable = ActiveDocument.Range(Start:=celTable.Range.Start, _End:=celTable.Range.End - 1)MsgBox rngTable.TextNext celTableEnd SubSub ReturnCellText()Dim tblOne As TableDim celTable As CellDim rngTable As RangeSet tblOne = ActiveDocument.Tables(1)For Each celTable In tblOne.Rows(1).CellsSet rngTable = celTable.RangerngTable.MoveEnd Unit:=wdCharacter, Count:=-1MsgBox rngTable.TextNext celTableEnd Sub004在活动文档的开头插入用制表符分隔的文本,然后将这些文本转换为表格。Sub ConvertExistingText()With Documents.Add.Content.InsertBefore one & vbTab & two & vbTab & three & vbCr.ConvertToTable Separator:=Chr(9), NumRows:=1, NumColumns:=3End WithEnd Sub005定义一个数组,该数组的元素个数等于文档中第一张表格(假定为 Option Base 1)中的单元格数。For Each.Next 结构用于返回每个表格单元格的内容,并将文字指定给相应的数组元素。Sub ReturnCellContentsToArray()Dim intCells As IntegerDim celTable As CellDim strCells() As StringDim intCount As IntegerDim rngText As RangeIf ActiveDocument.Tables.Count = 1 ThenWith ActiveDocument.Tables(1).RangeintCells = .Cells.CountReDim strCells(intCells)intCount = 1循环在 WORD VBA 中的应用第 3 页 共 70 页For Each celTable In .CellsSet rngText = celTable.RangerngText.MoveEnd Unit:=wdCharacter, Count:=-1strCells(intCount) = rngTextintCount = intCount + 1Next celTableEnd WithEnd IfEnd Sub006将当前文档中的表格复制到新文档中。Sub CopyTablesToNewDoc()Dim docOld As DocumentDim rngDoc As RangeDim tblDoc As TableIf ActiveDocument.Tables.Count = 1 ThenSet docOld = ActiveDocumentSet rngDoc = Documents.Add.Range(Start:=0, End:=0)For Each tblDoc In docOld.TablestblDoc.Range.CopyWith rngDoc.Paste.Collapse Direction:=wdCollapseEnd.InsertParagraphAfter.Collapse Direction:=wdCollapseEndEnd WithNextEnd IfEnd Sub007显示 Documents 集合中每个文档的名称。Sub LoopThroughOpenDocuments()Dim docOpen As DocumentFor Each docOpen In DocumentsMsgBox docOpen.NameNext docOpenEnd Sub008使用数组存储活动文档中包含的所有书签的名称。Sub LoopThroughBookmarks()Dim bkMark As Bookmark循环在 WORD VBA 中的应用第 4 页 共 70 页Dim strMarks() As StringDim intCount As IntegerIf ActiveDocument.Bookmarks.Count 0 ThenReDim strMarks(ActiveDocument.Bookmarks.Count - 1)intCount = 0For Each bkMark In ActiveDocument.BookmarksstrMarks(intCount) = bkMark.NameintCount = intCount + 1Next bkMarkEnd IfEnd Sub009更新活动文档中的 DATE 域。Sub UpdateDateFields()Dim fldDate As FieldFor Each fldDate In ActiveDocument.FieldsIf InStr(1, fldDate.Code, Date, 1) Then fldDate.UpdateNext fldDateEnd Sub010如果名为“Filename ”的词条是 AutoTextEntries 集合中的一部分,则以下示例显示一条消息。Sub FindAutoTextEntry()Dim atxtEntry As AutoTextEntryFor Each atxtEntry In ActiveDocument.AttachedTemplate.AutoTextEntriesIf atxtEntry.Name = Filename Then _MsgBox The Filename AutoText entry exists.Next atxtEntryEnd Sub011在第一个表格中添加一行,然后将文本 Cell 插入该行。Sub CountCells()Dim tblNew As TableDim rowNew As RowDim celTable As CellDim intCount As IntegerintCount = 1Set tblNew = ActiveDocument.Tables(1)Set rowNew = tblNew.Rows.Add(BeforeRow:=tblNew.Rows(1)For Each celTable In rowNew.CellscelTable.Range.InsertAfter Text:=Cell & intCount循环在 WORD VBA 中的应用第 5 页 共 70 页intCount = intCount + 1Next celTableEnd Sub012向新文档中添加一个 3 行 5 列的表格,然后在表格的每个单元格中插入数据。Sub NewTable()Dim docNew As DocumentDim tblNew As TableDim intX As IntegerDim intY As IntegerSet docNew = Documents.AddSet tblNew = docNew.Tables.Add(Selection.Range, 3, 5)With tblNewFor intX = 1 To 3For intY = 1 To 5.Cell(intX, intY).Range.InsertAfter Cell: R & intX & , C & intYNext intYNext intX.Columns.AutoFitEnd WithEnd Sub013将 Blue 变量的值设为 6,如果该变量不存在,本示例将该变量添加至文档,并将值设为 6。For Each aVar In ActiveDocument.VariablesIf aVar.Name = Blue Then num = aVar.IndexNext aVarIf num = 0 ThenActiveDocument.Variables.Add Name:=Blue, Value:=6ElseActiveDocument.Variables(num).Value = 6End If014在文档关闭以前提示用户保存文档。Sub PromptToSaveAndClose()Dim doc As DocumentFor Each doc In Documentsdoc.Close SaveChanges:=wdPromptToSaveChangesNextEnd Sub循环
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号