Here's the entire macro; remember it's actually for Word, but the place where it's giving me grief is common to Excel and Access, where I do most of my coding.
Sub Macro1()
'
' Macro1 Macro
'
'
Dim intNextPage As Integer, strFileName As String
intNextPage = 1
following part moves sequentially to each page of the Word document, looks for the Gift Reference on the last line, and captures the actual number at the end of that line for later use.
Do While intNextPage <= wdActiveEndPageNumber
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="" & intNextPage
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Gift Reference"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
[ this line adds the previously captured reference number to what is intended to be a discrete PDF filename
strFileName = "c:\Trusted\TYL" & Mid(Selection.Text, 18, 999)
Following line is where it breaks. If I type in the desired filename as the OutputFileName criterion, as shown below, it works fine, but if I attempt to use strFileName instead it tells me it's a bad file name.
ActiveDocument.ExportAsFixedFormat OutputFileName:="c:\Trusted\TYL1234", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportFromTo, _
From:=intNextPage, To:=intNextPage, Item:=wdExportDocumentContent, IncludeDocProps:=True, _
KeepIRM:=True, CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
intNextPage = intNextPage + 1
Loop
End Sub
If anyone can tell me why, I'd be very grateful. BTW, since I first posted I also tried to save to a default name, then rename it to what I want via FileSystemObject, same error, and then tried copying the default file to a new file with the desired discrete name, same error again. The discrete filename meets all the file naming conventions I know of, so I'm mystified.
Bookmarks