在Word文档中存储多个位置

问题描述 投票:0回答:1

我有一个希望很简单的问题。我简化了下面的代码,它需要在两个不同的场合运行。这可能吗?我该怎么做?文件通常超过 200 页。

Sub findAndStore()
Dim pgf As Paragraph
For Each pgf In ActiveDocument.Paragraphs
    If pgf.Range.text = "[table]" & vbCr Then
        'store place in document
    End If
Next
End Sub

Sub addTables()
for each stored place in document do
    Call AddTable("place in document")
Loop
End Sub
vba ms-word
1个回答
0
投票

你可以尝试这样的事情:

Option Explicit

Dim col As Collection 'global to store ranges

Sub findAndStore()
    Dim pgf As Paragraph
    Set col = New Collection
    For Each pgf In ActiveDocument.Paragraphs
        If pgf.Range.Text = "[table]" & vbCr Then
            col.Add pgf.Range.Duplicate
        End If
    Next
End Sub

Sub addTables()
    Dim rng As Range
    For Each rng In col
        AddTable rng
    Next rng
End Sub

'dummy sub
Sub AddTable(rng As Range)
    ActiveDocument.Tables.Add rng, 2, 5
End Sub


© www.soinside.com 2019 - 2024. All rights reserved.