Thread: [Solved] Mass Formatting of Files
View Single Post
 
Old 03-29-2015, 10:41 PM
gmayor's Avatar
gmayor gmayor is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There is a historical issue that filenames should not start with numbers. This is now largely irrelevant, but it seems that it still applies to a number of VBA processes including DocumentsOpen. I am not sure why I had missed this before.

I must investigate further, but in the meantime it can be trapped:

Code:
Sub MassFormatFiles()
'
' MassFormatFiles Macro
' Page Setup dialog box. Thus, things like paper size, margins, header and footer locations, and orientation
'
Dim strFilename As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog

    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

    With fDialog
        .Title = "Select folder and click OK"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , _
                   "List Folder Contents"
            Exit Sub
        End If
        strPath = fDialog.SelectedItems.Item(1)
        If Len(strPath) > 3 Then strPath = strPath & Chr(92)
    End With

    If Left(strPath, 1) = Chr(34) Then
        strPath = Mid(strPath, 2, Len(strPath) - 2)
    End If

    strFilename = Dir$(strPath & "*.doc")
    Application.ScreenUpdating = False
    Do While Len(strFilename) <> 0
        If IsNumeric(Left(strFilename, 1)) Then
            MsgBox "Filenames that begin with numbers will not be processed."
            Exit Do
            strFilename = Dir$()
        End If
        strFilename = Dir$()
    Loop
    While Len(strFilename) <> 0
        If Right(LCase(strFilename), 3) = "doc" And _
           Not IsNumeric(Left(strFilename, 1)) Then
            WordBasic.DisableAutoMacros 1
            Set oDoc = Documents.Open(strPath & strFilename)
            With oDoc.PageSetup
                .LineNumbering.Active = False
                .Orientation = wdOrientPortrait
                .TopMargin = InchesToPoints(0.5)
                .BottomMargin = InchesToPoints(0.5)
                .LeftMargin = InchesToPoints(0.5)
                .RightMargin = InchesToPoints(0.5)
                .Gutter = InchesToPoints(0)
                .HeaderDistance = InchesToPoints(0.5)
                .FooterDistance = InchesToPoints(0.5)
                .PageWidth = InchesToPoints(8.5)
                .PageHeight = InchesToPoints(11)
                .FirstPageTray = wdPrinterDefaultBin
                .OtherPagesTray = wdPrinterDefaultBin
                .SectionStart = wdSectionNewPage
                .OddAndEvenPagesHeaderFooter = False
                .DifferentFirstPageHeaderFooter = False
                .VerticalAlignment = wdAlignVerticalTop
                .SuppressEndnotes = False
                .MirrorMargins = False
                .TwoPagesOnOne = False
                .BookFoldPrinting = False
                .BookFoldRevPrinting = False
                .BookFoldPrintingSheets = 1
                .GutterPos = wdGutterPosLeft
            End With
            With oDoc.Range
                .Font.name = "Times New Roman"
                .Font.Size = 12
                With .Paragraphs(1).Range
                    .Style = ActiveDocument.Styles("Heading 1")
                    .Font.name = "Times New Roman"
                    .Font.Size = 22
                    .Font.Color = 192
                End With
            End With

            oDoc.Close SaveChanges:=wdSaveChanges
            WordBasic.DisableAutoMacros 0
        End If
        strFilename = Dir$()
    Wend
    Application.ScreenUpdating = True
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote