Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 10-31-2022, 11:51 AM
bugy bugy is offline Word - position document Windows 10 Word - position document Office 2010
Novice
Word - position document
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

Quote:
Originally Posted by Italophile View Post
Looping through the bookmarks collection is unnecessary when there is a built-in function to check that a bookmark exists.

So the simplified code should be:
Code:
Sub SaveAsBM()
    Dim sPath As String: sPath = "C:\Server"
    Dim bmName As String: bmName = "CodiClient"
  
    Dim bm As Bookmark
    Dim bmValue As String
    Dim rng_bm As Range
      
    If ActiveDocument.Bookmarks.Exists(bmName) Then
        Set rng_bm = bm.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
Hello
I get this error, do you know what it could be?


"Object variable or With block variable not set"
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 = bm.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
Private Function CreateFolders(strPath As String)
Dim strTempPath As String
Dim lng_Path As Long
Dim VPath As Variant
Dim oFSO As Object
Dim i As Integer
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    VPath = Split(strPath, "\")
    If Left(strPath, 2) = "\\" Then
        strPath = "\\" & VPath(2) & "\"
        For lng_Path = 3 To UBound(VPath)
            strPath = strPath & VPath(lng_Path) & "\"
            If Not oFSO.FolderExists(strPath) Then MkDir strPath
        Next lng_Path
    Else
        strPath = VPath(0) & "\"
        For lng_Path = 1 To UBound(VPath)
            strPath = strPath & VPath(lng_Path) & "\"
            If Not oFSO.FolderExists(strPath) Then MkDir strPath
        Next lng_Path
    End If
lbl_Exit:
    Set oFSO = Nothing
    Exit Function
End Function
Attached Images
File Type: jpg 31-10-2022_19-46-50.jpg (173.9 KB, 9 views)
Reply With Quote
  #17  
Old 10-31-2022, 08:47 PM
BrianHoard BrianHoard is offline Word - position document Windows 10 Word - position document 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
  #18  
Old 11-02-2022, 10:42 AM
bugy bugy is offline Word - position document Windows 10 Word - position document Office 2010
Novice
Word - position document
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

Quote:
Originally Posted by Italophile View Post
Looping through the bookmarks collection is unnecessary when there is a built-in function to check that a bookmark exists.

So the simplified code should be:
Code:
Sub SaveAsBM()
    Dim sPath As String: sPath = "C:\Server"
    Dim bmName As String: bmName = "CodiClient"
  
    Dim bm As Bookmark
    Dim bmValue As String
    Dim rng_bm As Range
      
    If ActiveDocument.Bookmarks.Exists(bmName) Then
        Set rng_bm = bm.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
Thank you very much !!! it has worked perfectly for me
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word - position document Can I make Word remember the window size and position for each document? Jennifer Murphy Word 4 02-20-2023 06:36 PM
Changing Caption Position on Whole Document at once Alexandarale Word Tables 1 11-15-2020 06:56 PM
Word - position document Copy selected text from one document and paste to new document in same position on the page gasparik Word VBA 1 05-11-2020 05:41 AM
Starting a document, returning to last position glennnall Word 0 10-08-2017 10:09 AM
Word - position document VBA: how can I know the position on a document? tinfanide Excel Programming 3 02-27-2012 03:24 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:36 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft