拆分文档并将每个部分保存为文件

编程入门 行业动态 更新时间:2024-10-23 17:31:47
本文介绍了拆分文档并将每个部分保存为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含多人及其详细信息的 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 I End Sub Sub Test() 'delimiter SplitNotes "Name:" End Sub

The 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 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.

解决方案

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.

更多推荐

拆分文档并将每个部分保存为文件

本文发布于:2023-05-29 23:11:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/355120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   保存为   文档   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!