Consulting

Results 1 to 10 of 10

Thread: macro to save as pdf with name from bookmark

  1. #1
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location

    macro to save as pdf with name from bookmark

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location
    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

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    Yes. Can I have a fish too?
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location
    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.

  8. #8
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location
    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

  10. #10
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •