none
Auto Name a file RRS feed

  • Question

  • I am developing a Certificate of Compliance template and want to use a content control value to become the filename in a save as PDF file type. I would like to replace the ActiveDocument.Name with the contents of the Date Code Field but cant figure out how to get it there.  I am not a programmer and lifted the print to PDF from another forum.  It works very well but names the document the same as my template file every time.

    This is the subroutine I am using and the content control is a rich text control with the title DateCode.

    Code:

    Sub Convert_2_PDF()

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _

    ActiveDocument.Path & "\" & ActiveDocument.Name & ".pdf", ExportFormat:= _

    wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _

    wdExportOptimizeForPrint, Range:=wdExportAllDocument, _

    Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _

    CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _

    BitmapMissingFonts:=True, UseISO19005_1:=False

    End Sub

    Thanks for your help.

    Ric Evans

    Friday, November 30, 2012 7:04 PM

Answers

  • Basically, you need to retrieve the content control's value, which you can do with code like:

    Dim i As Long, StrTxt As String
    With ActiveDocument
      For i = 1 To .ContentControls.Count
        If ContentControls(i).Title = "SomeTitle" Then
          StrTxt = ContentControls(i).Range.Text
          Exit For
        End If
      Next
    End With

    where 'SomeTitle' is your control's title. You can then replace '& ActiveDocument.Name &' with '& StrTxt &'.

    An even better approach would be to use a 'ContentControlOnExit' macro in the Document's 'ThisDocument' module to capture the prospective filename when its content control is updated. For example:

    Option Explicit
    Dim StrTxt As String

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    If ContentControl.Title = "SomeTitle" Then StrTxt = ContentControl.Range.Text
    End Sub

    Note: You'll only be able to use 'Dim StrTxt As String' if your 'Convert_2_PDF' macro is also in the Document's 'ThisDocument' module; otherwise you'll need to use 'Public StrTxt As String' in a normal code module.


    Cheers
    Paul Edstein
    [MS MVP - Word]



    Saturday, December 1, 2012 7:50 AM