![]() |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
Hello
I have bookmark error "Bookmark Not present" The bookmark exists .... what can be the error? thanks |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
||||
|
||||
|
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 |
|
#7
|
|||
|
|||
|
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
|
|
#8
|
||||
|
||||
|
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 |
|
#9
|
|||
|
|||
|
The change has worked, but it keeps telling me that the bookmark is not numeric, when it is.
|
|
#10
|
||||
|
||||
|
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] |
|
#11
|
|||
|
|||
|
Hello
I attach the document the bookmark number is "CodiPacient" thanks |
|
#12
|
||||
|
||||
|
That's all very well but, as I said:
Quote:
Code:
Sub Test()
MsgBox ActiveDocument.Bookmarks("CodiPacient").Range.Text
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#13
|
|||
|
|||
|
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 |
|
#14
|
||||
|
||||
|
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] |
|
| Thread Tools | |
| Display Modes | |
|
|
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 |
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 |
Macro to save as pdf with ability to choose save as folder
|
rvessio | Word VBA | 4 | 07-25-2016 12:37 PM |
Default Save AS File Name from Bookmark
|
xscotsmanx | Word VBA | 1 | 07-17-2011 12:23 AM |