0

I'm trying to create a macro that executes when a file is saved from Word (or Excel) 2007+. The macro needs ot check the name/location of the file to decide whether to execute, and then if the filename checks out (probably because it has '_temp' appended to it, or exists in the \temp folder) then as well as saving the file, Word also saves to PDF with the same name but obviously with the .pdf extension instead. Preferably I'd like the PDFing to happen prior to the save, but I'm not fussed. The Word client already has the SaveAsPDForXPS plug-in installed.

So far, I've managed to figure out that I need a macro with FileSave() handler in it, and that (from recording a test macro) the save bit might look like:

Sub FileSave()
'
' FileSave Macro
'
'
  ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        "C:\Documents and Settings\rdyce\Desktop\Doc1.pdf", 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


End Sub
2
  • 1
    You will need to enter the "Visual Basic Editor" from within the application and add the code to the "BeforeSave" event, unfortunately you would need to do this on each user's machine unless you could develop a standalone addin for user's to import themselves. Feb 16, 2012 at 10:12
  • Beforesave! great stuff. I'll havea scout for the Addin thing...
    – Dycey
    Feb 16, 2012 at 10:24

1 Answer 1

2

OK, I think that this does the jjob, but any pointers to glaring errors still gratefully received. Also, how to turn it into an Addin is still proving puzzling:

Sub FileSave()
'
' FileSave Macro
'

    ActiveDocument.Save

    Dim StrFile As String
    Dim StrPath As String
    Dim StrName As String
    Dim StrPDFName As String

    StrPath = ActiveDocument.Path 'Get document path
    StrFile = ActiveDocument.Name 'Get document name

    If InStr(StrFile, "_tempkey") Then 'Check if this is a special file

        If InStr(StrFile, ".") Then 
            StrName = Left(StrFile, (InStr(StrFile, ".") - 1))
        Else
            StrName = StrFile
        End If

        StrPDFName = StrPath + "\" + StrName + ".pdf"

        ActiveDocument.ExportAsFixedFormat OutputFileName:=StrPDFName, _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
            OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False

    End If

End Sub
1
  • Actually, addin was fairly straightforward. Essentially, just save it as a .dotm file and stick in the Word startup folder. That seems to do the trick.
    – Dycey
    Feb 16, 2012 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.