使用 Word VBA 拆分文档并将每个部分另存为文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16792433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split document and save each part as a file using Word VBA
提问by Skitlz
I have a Word file that contains multiple people and their details.
我有一个 Word 文件,其中包含多个人和他们的详细信息。
I need to split this file into single files for each person.
我需要将此文件拆分为每个人的单个文件。
This is the code, most of it is from examples I found.
这是代码,其中大部分来自我找到的示例。
I need to split the file by the delimiter (Personal).
Each file needs to be named by their ID number situated just below the delimiter.
我需要通过分隔符(个人)拆分文件。
每个文件都需要以位于分隔符下方的 ID 号命名。
Sub SplitNotes (delim As String)
Dim sText As String
Dim sValues(10) As String
Dim doc As Document
Dim arrNotes
Dim strFilename As String
Dim Test As String
Dim I As Long
Dim X As Long
Dim Response As Integer
arrNotes = Split(ActiveDocument.Range, delim)
Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections.Do you wish to proceed?", 4)
If Response = 7 Then Exit Sub
For I = LBound(arrNotes) To UBound(arrNotes)
If Trim(arrNotes(I)) <> "" Then
X = X + 1
Set doc = Documents.Add
doc.Range = arrNotes(I)
'Find "EID: "
doc.Range.Find.Text = "EID: "
'Select whole line
Selection.Expand wdLine
'Assign text to variable
sText = Selection.Text
'Remove spaces
sText = Replace(sText, " ", "")
'Split string into values
sValues = Split(sText, ":")
strFilename = "Testing"
doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "Agent")
doc.Close True
End If
Next I
End Sub
Sub Test()
'delimiter
SplitNotes "Name:"
End Sub
The Word document is set out as follows:
Word文档如下:
Personal Name: John Smith EID: Alph4num3r1c (Not a set length as i know of) Details follow on from here
My problem is getting the ID number and using it in the save as function.
I don't have a complete understanding of how the split function works.
我的问题是获取 ID 号并在另存为功能中使用它。
我对 split 函数的工作原理没有完全了解。
回答by Romeo
Split function splits a string into array of strings based on a delimeter. For eg:
Split 函数根据分隔符将字符串拆分为字符串数组。例如:
Dim csvNames, arrNames
csvNames = "Tom,Dick,Harry"
arrNames = split(csvNames,",")
Now arrNames is an array containing 3 elements. You can loop through the elements like this:
现在 arrNames 是一个包含 3 个元素的数组。您可以像这样循环遍历元素:
Dim i
For i = 0 to UBound(arrNames)
response.write arrNames(i) & "<br />"
Next
Now applying split function to solve your problem. Read the line you are interested in into a variable. Lets say we have,
现在应用拆分功能来解决您的问题。将您感兴趣的行读入一个变量。可以说我们有,
Dim lineWithID, arrKeyValuePair
lineWithID = "EID: Alph4num3r1c"
Split it into an array using colon
使用冒号将其拆分为数组
arrKeyValuePair = Split(lineWithID,":")
Now, arrKeyValuePair(1) will contain your EID
现在, arrKeyValuePair(1) 将包含您的 EID
回答by Kazimierz Jawor
If your question is still valid I have some solution regarding file name you search. I didn't check all part of your code (so I did but I don't have your original document to make full analysis). Back to file name- you could use below simple logic to extract name from newly created doc:
如果您的问题仍然有效,我有一些关于您搜索的文件名的解决方案。我没有检查你代码的所有部分(所以我做了,但我没有你的原始文档来进行全面分析)。回到文件名 - 您可以使用以下简单逻辑从新创建的文档中提取名称:
'...beginning of your code here
'next part unchanged >>
For I = LBound(arrNotes) To UBound(arrNotes)
If Trim(arrNotes(I)) <> "" Then
X = X + 1
Set doc = Documents.Add
doc.Range = arrNotes(I)
'<<until this moment
'remove or comment your code here!!
'and add new part of the code to search for the name
Selection.Find.Execute "EID:"
Selection.MoveRight wdWord, 1
Selection.Expand wdWord
strFilename = Trim(Selection.Text)
'and back to your code- unchanged
doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "Agent")
doc.Close True
End If
Next I
'...end of sub and other ending stuff
I check it and works quite ok for me.
我检查了一下,对我来说效果很好。