Quote:
Originally Posted by BrianHoard
This example displays the numbers for all bookmarks in your document as a msgBox. Since it appears your numbers vary in size, I'm extending the range as long as their are numbers in it. In the code below, you can see the line where your bookmark value from the document is being stored as the var, bookmarkValue.
It was being stored in the previous example as "code", so as Italophile mentioned, you are already storing it as a variable.
Next question for you - What are you going to do with the variable once you have it?
Posting a sample Word document (not screen shots) with the actual bookmarks you're trying to work with, and details about your end goal would be helpful.
This is like a mystery that we're learning a little more with each iteration
Code:
Sub bookmarkValues()
Dim rng As Range
Dim bm As Bookmark
Dim rng_expanded As Range
Dim bookmarkValue As String
For Each bm In ActiveDocument.Bookmarks
Set rng = bm.Range
If rng.Start = rng.End Then
Set rng_expanded = rng
rng_expanded.MoveEndWhile cset:="0123456789"
' Store bookmark document text as a variable
bookmarkValue = rng_expanded.Text
Call MsgBox(prompt:=("Bookmark value: " & bookmarkValue), buttons:=vbOKOnly)
End If
Next bm
End Sub
|
the bookmar that I want to save in a variable is the one that I attach in the image. It is called "CodiClient".
I want to save the variable because I have a program that saves the document in the directory of that code when I close the document.
I put the code that I have (I want the value that I enter through the inputbox, variable "oBM" to be taken from the bookmark)
Code:
Sub SaveAsBM()
Dim oBM As String
Dim sPath As String
oBM = InputBox("RECEPTE", "ENTRA EL CODI DEL PACIENT", "")
sPath = "C:\Server\" & oBM & "\"
CreateFolders sPath
ActiveDocument.SaveAs sPath & "recepte" & Format(Now, "yyyymmdd hhnnss") & ".pdf"
bFound = True
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