代码之家  ›  专栏  ›  技术社区  ›  sheridan1685

使用Word VBA SaveAs2时文件名中出现的问号

  •  0
  • sheridan1685  · 技术社区  · 2 年前

    我有一个VBA宏,它获取Word文档,并将其逐页拆分为单独的Word文档。它主要基于以下内容: http://www.vbaexpress.com/kb/getarticle.php?kb_id=727

    文档的每一页都有一个段落,以“职务:”开头,然后是职务。我修改了宏,从那一行提取了职位标题,并将其复制到文件名中。它确实做到了,但在文件名中的职称后面会出现一个问号。

    我认为这是一个非打印字符,所以我尝试了修剪,也尝试替换^p、^m和其他不可见的字符。那没用。如果我通过MsgBox运行jobTitle或strNewFileName,则不会出现问号。

    Option Explicit
     
     
    Sub SplitIntoPages()
        
        Dim docMultiple As Document
        Dim docSingle As Document
        Dim rngPage As Range
        Dim iCurrentPage As Integer
        Dim iPageCount As Integer
        Dim strNewFileName As String
        Dim rngForFindSelect As Range
        Dim jobTitle As String
        Dim jobTitleForFilename As String
         
        Application.ScreenUpdating = False
    
        Set docMultiple = ActiveDocument 'Work on the active document (the one currently containing the Selection)
        
        Set rngPage = docMultiple.Range 'instantiate the range object
        
        iCurrentPage = 1
       
        iPageCount = 5 'limiting to 5 pages for testing
        'docMultiple.Content.ComputeStatistics(wdStatisticPages)  'get the document's page count
        
        Do Until iCurrentPage > iPageCount
            
            If iCurrentPage = iPageCount Then
                
                rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
            
            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
                
                rngPage.End = Selection.Start
                
            End If
            
            rngPage.Copy 'copy the page into the Windows clipboard
            
            Set docSingle = Documents.Add 'create a new document
            
            docSingle.Range.Paste 'paste the clipboard contents to the new document
             
             'remove any manual page break to prevent a second blank
            docSingle.Range.Find.Execute FindText:="^b", ReplaceWith:="^p"
    
            
            Set rngForFindSelect = docSingle.Range
            
            With rngForFindSelect.Find
                .Execute FindText:="Job Title"
                
                If .Found = True Then
                .Parent.Expand Unit:=wdSentence
                jobTitle = .Parent.Text
                End If
                
            End With
             
            jobTitle = Replace(jobTitle, "Job Title: ", "")
            jobTitle = Trim(jobTitle)
    
             'build a new sequentially-numbered file name based on the original multi-paged file name and path
            strNewFileName = Replace(docMultiple.FullName, ".docm", "_" & jobTitle & "_" & Right$("000" & iCurrentPage, 4) & ".docx")
            
            MsgBox strNewFileName
            
            docSingle.SaveAs2 FileName:=strNewFileName, FileFormat:=wdFormatDocumentDefault
            
            docSingle.Close SaveChanges:=wdSaveChanges 'close the new document
            
            iCurrentPage = iCurrentPage + 1 'move to the next page
            
            rngPage.Collapse wdCollapseEnd 'go to the next page
        
        Loop 'go to the top of the do loop
        
        Application.ScreenUpdating = True 'restore the screen updating
         
         'Destroy the objects.
        Set docMultiple = Nothing
        Set docSingle = Nothing
        Set rngPage = Nothing
    
    End Sub
    

    screenshot of filenames

    screenshot of Word document

    1 回复  |  直到 2 年前
        1
  •  0
  •   taller    2 年前
    • 末尾有一个非打印格式标记(段落)。添加代码行以将其从中删除 jobTitle .
            With rngForFindSelect.Find
                .Execute FindText:="Job Title"
                If .Found Then
                    .Parent.Expand Unit:=wdSentence
                    .Parent.End = .Parent.End - 1
                    jobTitle = .Parent.Text
                End If
            End With
    

    或者

            With rngForFindSelect.Find
                .Execute FindText:="Job Title"
                If .Found Then
                    .Parent.Expand Unit:=wdSentence
                    jobTitle = .Parent.Text
                    jobTitle = Left(jobTitle, Len(jobTitle) - 1)
                End If
            End With