使用Word VBA拆分文档并将每个部分另存为文件 [英] Split document and save each part as a file using Word VBA

查看:557
本文介绍了使用Word VBA拆分文档并将每个部分另存为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word文件,其中包含多个人及其详细信息.

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.

我需要用定界符(个人)分割文件.
每个文件都需要使用位于分隔符正下方的ID号来命名.

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 

Word文档的设置如下:

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 

我的问题是获取ID号并在另存为功能中使用它.
我对split函数的工作方式不完全了解.

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.

这篇关于使用Word VBA拆分文档并将每个部分另存为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆