Consulting

Results 1 to 2 of 2

Thread: Word (VBA) Automatic saving of the file using the content from the bookmark to pdf

  1. #1
    VBAX Newbie
    Joined
    Aug 2022
    Posts
    1
    Location

    Word (VBA) Automatic saving of the file using the content from the bookmark to pdf

    Hi all,

    I search a way how to save/export a file to pdf and I need use a content from bookmark_1 (a content of bookmark_1 is in at Userform).


    After pressing the green button, Word already saves the file where I need it, but I'm having trouble with the name. I would like to have the content of bookmark_1 displayed after the underscore. Thank you for your help
    Snímek obrazovky 2022-08-13 190047.jpg

    Dim strName As String
        ChangeFileOpenDirectory "C:\Users\coufa\Desktop\1"
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            "C:\Users\coufa\Desktop\1\Konformitätserklärung eines externen Anbieters_186188" & "_" & ActiveDocument.Bookmarks("Bookmark_1").Range.Text & ".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
    Last edited by Aussiebear; 08-13-2022 at 02:09 PM. Reason: Added code tags to supplied code

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    There is nothing wrong with the code you posted. I suspect the real issue is that your userform isn't actually inserting content within the bookmark but is instead inserting the content after the bookmark. In any event, since the datum is available in your userform, why not simply get it from there?

    For code to update a bookmark, use code like:
    Sub UpdateBookmark(StrBkMk As String, StrTxt As String)
    Dim BkMkRng As Range
    With ActiveDocument
      If .Bookmarks.Exists(StrBkMk) Then
        Set BkMkRng = .Bookmarks(StrBkMk).Range
        BkMkRng.Text = StrTxt
        .Bookmarks.Add StrBkMk, BkMkRng
      End If
    End With
    Set BkMkRng = Nothing
    End Sub
    which you would call with code like:
    Call UpdateBookmark("Bookmark_Name", "Bookmark Text")
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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
  •