新手求救——VB如何获取插入的BuildingBlock内容并删除
大家好,我是刚接触word开发与VBA的菜鸟,手头上有个关于改写现有模板的紧急任务,目的是把原来模板中重复调用的内容用Buildingblock的形式存放并在宏中进行调用。
原来代码如下:
Public MyCollapseArry(100) As Table
Public CollapseNumber As Integer
...
Sub Insert_CollapsePart(CollapseTitle As String, CurrentCollapseStyle As String)
Dim tblNew As Table
Dim PreWidth
Dim IfinTable As Boolean
Dim TableStyle
Dim Current_CollapseTitle As String
IfinTable = Selection.Information(wdWithInTable)
If IfinTable = True Then
PreWidth = Selection.Cells.Width
TableStyle = Selection.Tables(1).Style
Selection.TypeParagraph
Set tblNew = ActiveDocument.Tables.Add(Selection.Range, 2, 2)
If CollapseTitle = "" Then
Current_CollapseTitle = "Input content summary Here"
Else
Current_CollapseTitle = CollapseTitle
End If
With tblNew
.Style = CurrentCollapseStyle
.Columns(1).Width = InchesToPoints(0.5)
.Columns(2).Width = PreWidth - InchesToPoints(0.7)
.Cell(1, 1).Range.InlineShapes.AddPicture FileName:=ThisDocument.Path & "\images\Small_White_Collapse.png", LinkToFile _
:=False, SaveWithDocument:=True
.Cell(1, 2).Range.InsertAfter Current_CollapseTitle
.Cell(1, 2).Shading.BackgroundPatternColor = wdColorGray15
.Cell(1, 2).Range.Style = ActiveDocument.Styles("CollapseTitle")
.Cell(2, 2).Shading.BackgroundPatternColor = wdColorGray15
.Cell(2, 2).Range.InsertAfter "Input content that you want to collapse"
.Cell(2, 2).Range.Style = ActiveDocument.Styles("GWT_CollapseContent")
End With
Set MyCollapseArry(CollapseNumber) = tblNew
CollapseNumber = CollapseNumber + 1
Else
MsgBox ("You can only add collapse content into Input content or Input Additional information part of the page, or embed into another Collapse part.")
End If
End Sub
现在我已经在word菜单栏中创建了buildingblock(InsertExpandCollapseSetion),category为"MyTask",类型为“QuickParts",并且能够顺利插入这些buildingblock到文档中,代码如下
Public objTemplate As Template
Public objBB As BuildingBlock
Sub InsertExpandCollapseSection
' Set the template to store the building block
Set objTemplate = ActiveDocument.AttachedTemplate
' Access the building block through the type and category
Set objBB = objTemplate.BuildingBlockTypes(wdTypeQuickParts).Categories("MyTask").BuildingBlocks("InsertExpandCollapseSection")
' Insert the building block into the document
objBB.Insert Selection.Range
Set MyCollapseArry(CollapseNumber) = ?
CollapseNumber = CollapseNumber + 1
End Sub
但还有个宏是撤销之前插入的buildingblock(InsertExpandCollapseSection),类似于word本身的undo功能,我大概知道原来代码中的数组MyCollapseArry(CollapseNumber)就是作为中间变量去存储插入的内容,但不知道改为buildingblock后怎么给这个数组赋值,代码该怎么实现?主要不知道该用什么类型的数组以及如何取得插入的buildingblock的内容,请大虾们指点,小女子感激不尽!