我想使用2个不同的数组将2个不同的数据点放在word文档的2个不同的字段中。我可以运行单个“For Each”循环来填充其中一个字段,但我不知道如何在保存文档之前一起运行2个“For Each”循环以获取两个字段。这是我目前的代码:
Sub Numbering()
'
' Account Numbering and Naming Macro
'
'
Dim Rng1 As Range
Dim Rng2 As Range
Dim AccountNumbers As Variant, AccountNumber As Variant
AccountNumbers = Array("20T5555", "20T3333", "20T8888", "20T1111")
Dim AccountNames As Variant, AccountName As Variant
AccountNames = Array("Branch 1", "Branch 2", "Branch 3", "Branch 4")
Set Rng1 = ActiveDocument.Bookmarks("AccountNumber").Range
Set Rng2 = ActiveDocument.Bookmarks("AccountName").Range
For Each AccountNumber In AccountNumbers
AccountNumber1 = AccountNumber
Rng1.Delete
Rng1.Text = AccountNumber1
For Each AccountName In AccountNames
AccountName1 = AccountName
Rng2.Delete
Rng2.Text = AccountName1
' Silent Save_to_PDF Macro
'
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
Replace(ActiveDocument.FullName, "template.docx", Trim(AccountNumber1) & ".pdf"), _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
Next AccountName
Next AccountNumber
ActiveDocument.Save
End Sub
期望的结果是在一个字段中有20T5555,在第一个输出文件中有另一个分支1,在第二个输出文件中有20T3333和分支2,等等。
答案 0 :(得分:1)
For
循环可用于获取具有相同索引的项目:
AccountNumbers = Array("20T5555", "20T3333", "20T8888", "20T1111")
AccountNames = Array("Branch 1", "Branch 2", "Branch 3", "Branch 4")
For i = 0 To Ubound(AccountNames)
Debug.Print AccountNumbers(i), AccountNames(i)
Next i