View Single Post
 
Old 10-31-2022, 08:47 PM
BrianHoard BrianHoard is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

That error is from "bm.Range" not being valid. That was being used in my original code which was looping through all the bookmarks.
So now, you can use this instead. Notice the difference in how this is setting rng_bm.
Code:
Sub SaveAsBM()
    Dim sPath As String: sPath = "C:\Server"
    Dim bmName As String: bmName = "CodiPacient"
  
    Dim bm As Bookmark
    Dim bmValue As String
    Dim rng_bm As Range
      
    If ActiveDocument.Bookmarks.Exists(bmName) Then
        Set rng_bm = ActiveDocument.Bookmarks(bmName).Range
        If rng_bm.Start = rng_bm.End Then
            rng_bm.MoveEndWhile cset:="0123456789"
            bmValue = rng_bm.Text
        End If
        sPath = (sPath & "\" & bmValue & "\")
        Debug.Print ("Creating folder: " & sPath)
        CreateFolders sPath
        ActiveDocument.SaveAs sPath & "recepte" & Format(Now, "yyyymmdd hhnnss") & ".pdf"
    Else
        Call MsgBox(prompt:=("Unable to find bookmark, " & bmName), buttons:=vbCritical)
    End If
End Sub
Reply With Quote