本文介绍了拆分文档并将每个部分保存为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个包含多人及其详细信息的 Word 文件.我需要将此文件拆分为每个人的单个文件.这是代码,大部分来自我找到的例子.我需要通过分隔符(个人)分割文件.每个文件都需要以位于分隔符下方的 ID 号命名.Sub SplitNotes (delim As String)将文本变暗为字符串Dim sValues(10) 作为字符串昏暗的文档作为文档昏暗的笔记Dim strFilename As StringDim 测试作为字符串昏暗的我Dim X 长Dim 响应为整数arrNotes = Split(ActiveDocument.Range, delim)Response = MsgBox("这会将文档拆分为 " & UBound(arrNotes) + 1 & " 部分.您要继续吗?", 4)如果 Response = 7 然后退出 Sub对于 I = LBound(arrNotes) 到 UBound(arrNotes)如果 Trim(arrNotes(I)) "然后X = X + 1设置 doc = Documents.Adddoc.Range = arrNotes(I)'找到EID:"doc.Range.Find.Text = "EID: "'选择整行选择.展开 wdLine'将文本分配给变量sText = Selection.Text'删除空格sText = Replace(sText, " ", "")'将字符串拆分为值sValues = Split(sText, ":")strFilename = "测试"doc.SaveAs ThisDocument.Path &\" &str文件名 &格式(X,代理")doc.Close True万一接下来我结束子子测试()'分隔符SplitNotes名称:"结束子Word文档如下:个人的姓名:约翰·史密斯EID:Alph4num3r1c(不是我所知道的固定长度)详情从这里开始我的问题是获取 ID 号并在另存为功能中使用它.我对 split 函数的工作原理没有完全了解. 解决方案 如果您的问题仍然有效,我有一些关于您搜索的文件名的解决方案.我没有检查你代码的所有部分(所以我做了,但我没有你的原始文档来进行全面分析).回到文件名 - 您可以使用以下简单逻辑从新创建的文档中提取名称:'...从这里开始你的代码'下一部分不变>>对于 I = LBound(arrNotes) 到 UBound(arrNotes)如果 Trim(arrNotes(I)) "然后X = X + 1设置 doc = Documents.Adddoc.Range = arrNotes(I)'<<直到这一刻'在此处删除或评论您的代码!!'并添加新的代码部分以搜索名称选择.查找.执行EID:"Selection.MoveRight wdWord, 1选择.展开 wdWordstrFilename = Trim(Selection.Text)'然后回到你的代码 - 不变doc.SaveAs ThisDocument.Path &\" &str文件名 &格式(X,代理")doc.Close True万一接下来我'...结尾和其他结尾的东西我检查了一下,对我来说效果很好.I have a Word file that contains multiple people and their details.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.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 IEnd SubSub Test() 'delimiter SplitNotes "Name:"End SubThe Word document is set out as follows: Personal Name: John Smith EID: Alph4num3r1c (Not a set length as i know of) Details follow on from hereMy 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. 解决方案 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 IfNext I'...end of sub and other ending stuffI check it and works quite ok for me. 这篇关于拆分文档并将每个部分保存为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 04:50