I want to use 2 different arrays to put 2 different data points in 2 different fields in a word document. I can run a single "For Each" loop to get one of the fields populated, but I don't know how to run 2 "For Each" loops together to get both fields populated before saving out the document. Here is my current code:
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
The desired outcome would be to have 20T5555 in one field and Branch 1 in the other on the first output document, 20T3333 and Branch 2 on the second, etc.
For
loop can be used to get the items with same index:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.