frich:
Nedenfor finder du en makro, som jeg har lavet til at løse opgaven. Se kommentarerne i makroen (jeg sender en e-mail direkte til dig).
For hjælp til at installere makroen, så du kan bruge den, se:
https://www.thedoctools.com/word-macros-tips/general/how-to-install-a-macro/Her er makroen:
Sub ConvertToPDF_AllWordDocsInFolder()
'Macro created by Lene Fredborg 20-Apr-2023
'This macro converts all Word documents in folder strPath to PDF
'and adds strPrefix in front of the PDF file names
'The PDF files are saved in the same folder as the Word documents
Dim oDoc As Document
Dim strFile As String
Dim strPath As String
Dim strPDFName As String
Dim lngPos As Long
Dim strExtension As String
Dim strPrefix As String
Dim blnMakeNicePDFNamesForWeb As Boolean
'=========================
'CORRECT THE VALUES BELOW AS NEEDED
'=========================
'Path to folder with Word documents
strPath = "C:\Test\"
'String to add in start of PDF file name - set to "" if nothing is to be added
strPrefix = "Test-"
'If PDFs are to be published on the web, you should set the following to True
'to have spaces in file names changed to hyphens + æøå characters replaced
blnMakeNicePDFNamesForWeb = True
'=========================
'=========================
'Check if folder exists
If Dir(strPath, vbDirectory) = "" Then
MsgBox "Folder " & strPath & " not found!"
Exit Sub
End If
'Loop through all Word documents in folder
strFile = Dir(strPath & "*.doc*")
Do While strFile <> ""
'Open the Word document
Set oDoc = Documents.Open(strPath & strFile)
'Get extension of Word document (could have different lengths, e.g. .doc versus .docx)
lngPos = InStrRev(strFile, ".")
strExtension = Right(strFile, Len(strFile) - lngPos + 1)
'Create PDF filename
strPDFName = strPath & strPrefix & Replace(strFile, strExtension, ".pdf")
'Adjust PDF file name if selected
If blnMakeNicePDFNamesForWeb = True Then
strPDFName = Replace(strPDFName, " ", "-")
'Also replace æ, ø, å with ae, oe, aa
strPDFName = Replace(strPDFName, "æ", "ae")
strPDFName = Replace(strPDFName, "ø", "oe")
strPDFName = Replace(strPDFName, "å", "aa")
End If
'Export as PDF
oDoc.ExportAsFixedFormat OutputFileName:=strPDFName, ExportFormat:=wdExportFormatPDF
'Close the Word document without saving
oDoc.Close False
'Move to next file
strFile = Dir()
Loop
MsgBox "Conversion to PDF finished."
End Sub