Save file as PDF by file name using content from current document with Word VBA
Greeting everyone,
So i am trying to make macro to save from word document (many pages) to separate PDF file. However, I want the second line of paragraph as my file name accordingly. Here is my code that i have tried but instead it saving with replace of each other due to same file name.
Code:
Sub AllSectionsToSubDoc()
Dim x As Long
Dim Sections As Long
Dim Doc As Document
Dim FirstPara As String
Dim r As Range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Doc = ActiveDocument
Sections = Doc.Sections.Count
For x = Sections - 1 To 1 Step -1
FirstPara = Paragraphs(2).Range.Text
FirstPara = Left(FirstPara, Len(FirstPara) - 1)
Doc.Sections(x).Range.Copy
Documents.Add
ActiveDocument.Range.Paste
ActiveDocument.SaveAs (Doc.Path & "\" & FirstPara & ".pdf"), (wdExportFormatPDF)
ActiveDocument.Close False
Next x
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Another question: I try to trim the paragraph. example: I have my second paragraph "Dear Mr. John Adam". I want my file name "John Adam.pdf" however in my code it gave me full "Dear Mr. John Adam.pdf" instead. I tried using Right(FirstPara, Len(FirstPara) - 1) but i got error command.