Search code examples
vbamacosms-wordfilenames

Question mark appearing in filenames when using Word VBA SaveAs2


I have a VBA macro that takes a Word document and splits it page by page into separate Word documents. It's based mostly on this: http://www.vbaexpress.com/kb/getarticle.php?kb_id=727

Each page of the document has one paragraph that starts with "Job Title: " followed by the job title. I modified the macro to extract the job title from that line and copy it into the filename. It does it but a question mark appears after the job title in the filename.

I thought it was a nonprinting character so I tried trim and also trying to replace ^p, ^m and other invisible characters. That didn't work. If I run jobTitle or strNewFileName through MsgBox, the question mark doesn't appear.

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


Solution

    • There is a nonprinting formatting mark (paragraph) at the end. Add a code line to remove it from 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
    

    OR

            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