PDA

View Full Version : [SOLVED:] Word (VBA) Automatic saving of the file using the content from the bookmark to pdf



Bbronx
08-13-2022, 10:44 AM
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
30050


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

macropod
08-13-2022, 09:14 PM
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")