VBA宏数组文件命名查询

时间:2019-02-01 15:17:50

标签: vba ms-word

一个同事正在准备一个邮件合并的单词,有7个收件人,我有以下代码将每个页面打印为pdf,并将其另存为pdf,从数组中唯一命名。虽然数组似乎只允许这么多文件名,但是如果数组代码进入两行,似乎会引起混乱。我对所有这些都是很新的,所以请哈哈!我已附上以下代码: Sub SplitIntoSeparatePDFs()

Dim oDocMultiple As Document
Dim oDocSingle As Document
Dim oRngPage As Range
Dim iCurrentPage As Long, iPageCount As Long
Dim strNewFileName As Variant
Dim arrFileNames() As Variant
Dim i As Long

Application.ScreenUpdating = False

' path to original (long) document
Set oDocMultiple = Documents.Open("C:\Users\lwal4\Desktop\Word Version of OP res care mail merge.docx")
'instantiate the range object
Set oRngPage = oDocMultiple.Range
' put all file names in an array
arrFileNames = Array("adl plc.pdf", "advent estates limited.pdf", "albemarle rest home ltd.pdf""avery homes (nelson) limited.pdf", "avery homes nuneaton limited.pdf", "b and e thorpe-smith.pdf", "barchester healthcare homes ltd.pdf", "ben - motor & allied trades benevolent fund.pdf", "bentley house limited.pdf", "benvarden residential care homes limited.pdf", "berkley care (warwick) limited.pdf", "bm care warwick limited.pdf", "bupa care homes (ans) limited.pdf", "butts croft limited.pdf", "care uk community partnerships ltd.pdf", "caring homes healthcare group limited.pdf", "central england healthcare limited.pdf", "clarendon manor limited.pdf", , "coate water care company (church viewnursing home) limited.pdf", "cow lees care home ltd.pdf", "craighaven limited.pdf", "crosscrown ltd.pdf", "culpeper care limited.pdf",

“ adl plc.pdf”,“ Advent Estates Limited.pdf”,“ albemarle Rest Home ltd.pdf”,“ avery homes(nelson)limited.pdf”,“ avery homes nuneaton limited.pdf”,“ b和e thorpe-smith.pdf”,“ barchester保健之家有限公司”,“ ben-汽车及相关行业慈善基金” .pdf”,“ bentley house limited.pdf”,“ benvarden住宅护理之家有限公司.pdf”,“ berkley护理” (warwick)limited.pdf”,“ bm Care warwick limited.pdf”,“ bupa care homes(ans)limited.pdf”,“ butts croft limited.pdf”,“ care uk community partnerships ltd.pdf”,“爱心之家” Healthcare Group limited.pdf”,“中央英格兰医疗保健有限公司.pdf”,“ clarendon庄园有限公司.pdf”,“科特沃特水疗护理公司(教堂看护所)limited.pdf”,“牛李护理之家有限公司.pdf”, “ craighaven limited.pdf”,“ crosscrown ltd.pdf”,“ culculper care limited.pdf”)

iCurrentPage = 1
'get the original document's page count
iPageCount = oDocMultiple.Content.ComputeStatistics(wdStatisticPages)

Do Until iCurrentPage > iPageCount
    If iCurrentPage = iPageCount Then
        'last page (there won't be a next page)
        oRngPage.End = ActiveDocument.Range.End
    Else
        'Find the beginning of the next page
        'Must use the Selection object. The Range.Goto method will not work on a page
        Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1
        'Set the end of the range to the point between the pages
        oRngPage.End = Selection.Start
    End If

    'copy the page into the Windows clipboard
    oRngPage.Copy

    'create a new document
    Set oDocSingle = Documents.Add

    'paste the clipboard contents to the new document
    oDocSingle.Range.Paste

    'remove any manual page break to prevent a second blank
    oDocSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""

    'build a new file name based on the original multi-paged path and name from the array
    strNewFileName = oDocMultiple.Path & "\" & arrFileNames(i)


    Debug.Print strNewFileName

    'save the new single-paged document
    oDocSingle.SaveAs FileName:=strNewFileName, FileFormat:=wdFormatPDF, AddToRecentFiles:=False

    'move to the next document (1 pages later)
    iCurrentPage = iCurrentPage + 1

    'close the new document without saving
    oDocSingle.Close SaveChanges:=wdDoNotSaveChanges

    'go to the next page
    oRngPage.Collapse wdCollapseEnd

    i = i + 1
Loop

Application.ScreenUpdating = True

结束子

0 个答案:

没有答案