VBA在单词中选择一系列页面

时间:2017-08-17 10:44:03

标签: vba ms-word word-vba

我需要在文档中选择多个页面,然后将它们复制到新的.docx文件中。 选择应该是根据包含页码的CSV文件,例如,我需要选择页面9-13然后将其复制到新文件。 我设法创建一个复制和创建新文件的系统,但我找不到一种方法来做选择部分。 我到目前为止复制和粘贴系统的代码:

Sub DocSplit2()
'
' DocSplit2 Macro
'
'
' loading CSV file as file #1
   FolderPath = "C:\Users\Oz Eran\Desktop\HP Split\HP 2 split\HP2 table of content section.csv"
   Dim Count As Integer
   Open FolderPath For Input As #1
   'Do Until EOF(1)
        Line Input #1, Line_FromFile
        Line_Items = Split(Line_FromFile, ",")
        Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Name:=CDec(Line_Items(0))
        'Selection.GoTo What:=wdGoToSection, Which:=wdGoToAbsolute, Count:=CDec(Line_Items(0))
        Selection.find.ClearFormatting
        With Selection.find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        ' this is how it selected the text to copy
        N_pages = CInt(Line_Items(1)) - CInt(Line_Items(0))
        Selection.MoveDown Unit:=wdSection, Count:= N_pages, Extend:=wdExtend
        Selection.Copy
        Set docSingle = Documents.Add 'create a new document
        Selection.PasteAndFormat (wdFormatOriginalFormatting)
        ActiveDocument.Save
        ChangeFileOpenDirectory "C:\Users\Oz Eran\Desktop\HP Split\HP 2 split\"
        strNewFileName = replace(docMultiple.FullName, ".docx", "_" & Right$("000" & Line_Items(0), 4) & ".docx")
        ActiveWindow.Close
    'Loop
    Close #1
    MsgBox " DocSplit: Done"
End Sub

0 个答案:

没有答案