Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-09-2022, 03:23 AM
bugy bugy is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2010
Novice
Word save in folder bookmark
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default Word save in folder bookmark

Hello
I have a word document with a bookmark called "code"
I want the document to be saved in the c:\server\ directory
but in the code subdirectory
example
If the document have the bookmark code 6855, save on
c:\server\6855\


If the document have the bookmark code 6856, save on
c:\server\6856\
it's possible?
very thanks
Reply With Quote
  #2  
Old 03-09-2022, 05:19 AM
gmayor's Avatar
gmayor gmayor is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It is certainly possible, but you haven't said what you wish to use as the document name. In the example below the document will be saved in a folder named from a numeric value in the bookmark with the name filename.docx.
Code:
Sub SaveAsBM()

'Graham Mayor - https://www.gmayor.com - Last updated - 09 Mar 2022 

Dim oBM As Bookmark
Dim sPath As String
Dim bFound As Boolean
    For Each oBM In ActiveDocument.Bookmarks
        If LCase(oBM.Name) = "code" Then
            If IsNumeric(oBM.Range.Text) = True Then
                sPath = "C:\Server\" & oBM.Range.Text & "\"
                CreateFolders sPath
                ActiveDocument.SaveAs sPath & "filename.docx" 'change filename as required
                bFound = True
            Else
                MsgBox "Bookmark content is not numeric", vbCritical
                GoTo lbl_Exit
            End If
            Exit For
        End If
    Next oBM
    If Not bFound = True Then
        MsgBox "Bookmark not present", vbCritical
    End If
lbl_Exit:
    Set oBM = Nothing
    Exit Sub
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-09-2022, 10:11 AM
bugy bugy is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2010
Novice
Word save in folder bookmark
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

Hello
I have bookmark error "Bookmark Not present"
The bookmark exists ....
what can be the error?
thanks
Reply With Quote
  #4  
Old 03-09-2022, 09:59 PM
gmayor's Avatar
gmayor gmayor is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The code loops through all the bookmarks in the active document and if it finds a bookmark named 'code' (without the quotes) it will create the folder if missing and save the document in that folder.
If the bookmark is not found, as you have suggested, it reports that it is not found.
From the 'Insert Tab', click 'Bookmark' and see if 'code' is listed. Ensure that the name has no leading or trailing space.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 03-09-2022, 11:46 PM
Guessed's Avatar
Guessed Guessed is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You don't need to loop through bookmarks to see if it exists
Code:
Sub TestBookmark()
  Dim oBM As Bookmark
  If ActiveDocument.Bookmarks.Exists("code") Then
    Set oBM = ActiveDocument.Bookmarks("code")
    MsgBox oBM.Range.Text
  Else
    MsgBox "This bookmark doesn't exist" & vbCr & "code"
  End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 03-10-2022, 01:41 AM
gmayor's Avatar
gmayor gmayor is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by Guessed View Post
You don't need to loop through bookmarks to see if it exists
Indeed, but either way, if the bookmark exists the code will address it.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 07-07-2022, 12:57 AM
bugy bugy is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2010
Novice
Word save in folder bookmark
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

Sorry I couldn't test it.
I changed the code and it doesn't work for me.
It tells me that the bookmark does not exist
But in test bookmark it works
Code:
Sub SaveAsBM()
Dim oBM As Bookmark
Dim sPath As String
Dim bFound As Boolean
    For Each oBM In ActiveDocument.Bookmarks
        If LCase(oBM.Name) = "CodiPacient" Then
            If IsNumeric(oBM.Range.Text) = True Then
                sPath = "C:\Proves\" & oBM.Range.Text & "\"
                CreateFolders sPath
                ActiveDocument.SaveAs sPath & "recepte.doc" 'change filename as required
                bFound = True
            Else
                MsgBox "Bookmark content is not numeric", vbCritical
                GoTo lbl_Exit
            End If
            Exit For
        End If
    Next oBM
    If Not bFound = True Then
        MsgBox "Bookmark not present", vbCritical
    End If
lbl_Exit:
    Set oBM = Nothing
    Exit Sub
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
End Function
Sub TestBookmark()
  Dim oBM As Bookmark
  If ActiveDocument.Bookmarks.Exists("CodiPacient") Then
    Set oBM = ActiveDocument.Bookmarks("CodiPacient")
    MsgBox oBM.Range.Text
  Else
    MsgBox "This bookmark doesn't exist" & vbCr & "CodiPacient"
  End If
End Sub
Reply With Quote
  #8  
Old 07-07-2022, 03:31 PM
Guessed's Avatar
Guessed Guessed is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The first thing that stands out is this line
If LCase(oBM.Name) = "CodiPacient" Then
The LCase function makes everything lowercase so you will never get a hit with this unless you change it to
If LCase(oBM.Name) = "codipacient" Then
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 07-08-2022, 12:19 AM
bugy bugy is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2010
Novice
Word save in folder bookmark
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

The change has worked, but it keeps telling me that the bookmark is not numeric, when it is.
Attached Images
File Type: png 08-07-2022_09-13-45.png (4.6 KB, 17 views)
Reply With Quote
  #10  
Old 07-08-2022, 05:02 PM
macropod's Avatar
macropod macropod is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Your bookmark is empty - the number appears after the bookmark, not within it.

PS: Please attach documents/images here instead of just posting links to files stored elsewhere. You can do this via the paperclip symbol on the Advanced tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 07-10-2022, 03:30 AM
bugy bugy is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2010
Novice
Word save in folder bookmark
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

Hello
I attach the document
the bookmark number is "CodiPacient"
thanks
Attached Files
File Type: doc Receptes0.doc (105.0 KB, 6 views)
Reply With Quote
  #12  
Old 07-10-2022, 03:35 AM
macropod's Avatar
macropod macropod is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That's all very well but, as I said:
Quote:
Originally Posted by macropod View Post
Your bookmark is empty - the number appears after the bookmark, not within it.
You can verify that via:
Code:
Sub Test()
  MsgBox ActiveDocument.Bookmarks("CodiPacient").Range.Text
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #13  
Old 07-10-2022, 03:42 AM
bugy bugy is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2010
Novice
Word save in folder bookmark
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

Hello
I don't understand why it doesn't work.
The document is created through a program with VB inserting the information in the bookmarks.
The number is in front of the bookmark, but all the other bookmarks are too.
How should I do it?
Thank you
Reply With Quote
  #14  
Old 07-10-2022, 05:32 AM
macropod's Avatar
macropod macropod is offline Word save in folder bookmark Windows 10 Word save in folder bookmark Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For VBA code showing how to update a bookmark rather than merely inserting content after it, see: https://www.msofficeforums.com/67058-post2.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't save Word 2019 doc to original folder - can you help please? frustratedphil Word 1 01-06-2019 11:11 AM
Word save in folder bookmark Save all docs to pdf in specified folder loki005 Word VBA 4 10-25-2018 02:18 PM
macro to save as pdf with name from bookmark pamtupac Word VBA 3 06-06-2017 08:23 PM
Word save in folder bookmark Macro to save as pdf with ability to choose save as folder rvessio Word VBA 4 07-25-2016 12:37 PM
Word save in folder bookmark Default Save AS File Name from Bookmark xscotsmanx Word VBA 1 07-17-2011 12:23 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:28 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