Tuesday, May 3, 2011

Word 2010 Macro to automatically save current txt file as pdf file

   
Here’s a word 2010 macro I created for my self which does the following things:

1. select all

2. Change font size to 24

3. Change page Margins to narrow

4. Save the current file as pdf (change current file name from .txt to .pdf, and save to the same directory)

5. put the marco to the quick access toolbar

image

The marco:

Sub MacroGenerateKindlePdf()
'
' MacroGenerateKindlePdf Macro
'
'
    Selection.WholeStory
    Selection.Font.Size = 24
    With Selection.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
        .LayoutMode = wdLayoutModeDefault
    End With
   
    Dim name As String
    name = ActiveDocument.name
    name = ActiveDocument.Path + "\" + Strings.Replace(name, ".txt", ".pdf")
   
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        name, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
       
    SendKeys ("{HOME}")
   
End Sub

No comments:

Post a Comment