对不起,如果这个问题以前曾做过,但是找不到,并且类似的例子也不起作用。
我想将txt文件的信息放在VBA上的数组中。然后使用该数组填充代码的不同字符串。你能帮助我吗?现在,我无法读取文件并填充数组。
想法是这样的: 每行的客户编号,地址等文件 VBA读取txt并填充一个数组,每个位置是txt的一行。 我使用数组填充模板:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "CLIENTENumber"
.Replacement.Text = ARRAY[Positionwithclient]
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
谢谢!
有关其他信息,关于txt的信息按以下方式排列:
565656
Peter
(etc)
第一个是客户编号,第二个是名称等。然后,我将用565656替换word文档中的“ CLIENTNumber”。替换代码在上面,并且效果很好。
答案 0 :(得分:1)
尝试以下方法:
Sub Demo()
Application.ScreenUpdating = False
Dim SrcDoc As Document, RList, i As Long
Const FList As String = "CLIENTNumber,CLIENTName"
'Load the strings from the reference file into a text string to be used as an array.
Set SrcDoc = Documents.Open("Drive:\FilePath\DataList.txt", ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
With SrcDoc
RList = .Range.Text
.Close False
End With
Set SrcDoc = Nothing
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
For i = 0 To UBound(Split(FList, ","))
.Text = Split(FList, vbCr)(i)
.Replacement.Text = Split(RList, vbCr)(i)
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub
仅提供FList字符串的所有项目,只不过用逗号分隔它们,并以它们在文本文件中出现的顺序,以及指示的文本文件的完整路径和名称。