PDA

View Full Version : macro to save as pdf with name from bookmark



pamtupac
06-05-2017, 09:51 AM
hi all,

i'm trying to create a command button in word and attach a macro that automatically saves the doc as a pdf to a specified folder with file with the name coming from 2 bookmark references with a space and dash in between name.

here is what i have so far:

Private Sub CommandButton1_Click()

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"C:\Users\sesa460426\Box Sync\My Projects\Quotes" & ClientName = .Bookmarks("ClientName").Range.Text&.Bookmarks("Qu oteNumber").Range.Text & (".pdf"), _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ChangeFileOpenDirectory "C:\Users\sesa460426\Box Sync\My Projects\Quotes"
End Sub


i keep getting an invalid or unqualified reference error and it highlights the .bookmarks part. i'm very very new to vba so if someone can please help me.

ps. i've also tried adding activedocument before the .bookmarks part but then i get a different error.

thanks in advance

gmaxey
06-05-2017, 10:57 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strName As String
strName = "C:\Users\sesa460426\Box Sync\My Projects\Quotes\" & ActiveDocument.Bookmarks("ClientName").Range.Text & " " & ActiveDocument.Bookmarks("QuoteNumber").Range.Text & ".pdf"
MsgBox strName
lbl_Exit:
Exit Sub
End Sub

pamtupac
06-05-2017, 12:20 PM
hey greg, thanks for the reply but this just pops up a box with info but does not save it as a pdf. i don't want any pop up i simply want it to save a copy as a pdf file with the name being 2 bookmarks which are hyperlinked to an excel

thanks

gmaxey
06-05-2017, 05:26 PM
Saving to PDF is up to you. I just showed you how to define your FileName string from bookmarks in the document.

Dim strName As String
strName = "C:\Users\sesa460426\Box Sync\My Projects\Quotes\" & ActiveDocument.Bookmarks("ClientName").Range.Text & " " & ActiveDocument.Bookmarks("QuoteNumber").Range.Text & ".pdf"

OutputFileName:= strName

gmayor
06-05-2017, 10:59 PM
Duplicate post at http://www.msofficeforums.com/word-vba/35747-macro-save-pdf-name-bookmark.html GRRRR!:banghead:

gmaxey
06-06-2017, 04:27 AM
Yes. Can I have a fish too?

pamtupac
06-06-2017, 09:02 AM
thanks greg, i'm still getting a name not valid error i think its because my bookmarks are pointing to hyperlinks linked to excel, i can't seem to find a workaround so it looks like i'm going to have to save the doc manually or try to save it from my excel workbook, if you could help me with that?
basically i have an excel with a command button that opens a word doc with hyperlinks to data in the excel so it can update, the user then modifies the word doc to add a few more details such as a description, one they are done i want them to click the "finish" button on my excel so it can save a copy of the word as a pdf with the name in a cell.

sorry Graham, i wasn't getting any responses in this forum for a bit so i posted to 2 more, the one you mentioned and mr exel forums.

gmayor
06-07-2017, 05:43 AM
Hyperlinks? If you paste a link from Excel to Word and bookmark the result, then the content of that bookmark is the text result from the link.
What EXACTLY is the value of the bookmark. i.e. What is the result of


ActiveDocument.Bookmarks("ClientName").Range.Text & " " & ActiveDocument.Bookmarks("QuoteNumber").Range.Text

pamtupac
06-07-2017, 07:05 AM
the bookmark value is the link value which in this case is a formula in excel displaying values from 2 cells together (one a mix of various cells and one user typed). the vba error keeps saying name not valid.

i just want to export the word as a pdf with the name value coming from the active sheet on excel (from excel vba) and then close the document without saving it as it is a template that i don't want disturbed. if you can help me with that instead please? it would actually work much better since my users are using the original excel sheet to open the word doc template with a button

thanks in advance

gmayor
06-07-2017, 11:01 PM
In that case you cannot use the content of the bookmarks to name the document as they contain illegal filename characters.
It is impossible to suggest an alternative approach without seeing the document and the worksheet (and the macro) to establish what you have.