Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-28-2022, 04:18 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 Word - position document

Hello
Is it possible to save the value of a fixed document position in a variable?
I want to store a 4 digit value that always comes out in the same position.


Thank you
Reply With Quote
  #2  
Old 10-28-2022, 06:24 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

Hi,
Are you asking if you can save a variable in a document?
If so, I was looking into that a while ago, and although haven't used it yet, I did record the following notes on how to store a document variable, and retrieve it again.
Hopefully this helps get you on the right path if that's what you're looking to do.

Code:
Sub varTest()
  ' Testing how to store and retrieve a variable saved with the doc.

  ActiveDocument.Variables.Add Name:="Value1", Value:="1"
  
  MsgBox ActiveDocument.Variables("Value1") + 3

  For Each myVar In ActiveDocument.Variables
    Debug.Print ("Name = " & myVar.Name & vbCr & "Value = " & myVar.Value)
  Next myVar

End Sub
Reply With Quote
  #3  
Old 10-29-2022, 02:35 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

What I want is to take a value in a specific position and store it in a variable.
For example (the attached document) know the position of the number 1151 and save in a variable.
Attached Images
File Type: jpg 29-10-2022_11-29-41.jpg (25.4 KB, 17 views)
Reply With Quote
  #4  
Old 10-29-2022, 04:55 AM
Italophile Italophile is offline Word - position document Windows 11 Word - position document Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

There are several ways you could do this.
  1. Insert a content control where you want the value to appear and map it to a custom xml part. You then write the value to the xml.
  2. Write the value to a document variable and insert a DocVariable field where you want it to appear.
  3. Mark the location in the document with a bookmark and write the value to the bookmarked location.
Reply With Quote
  #5  
Old 10-29-2022, 06:11 AM
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

If you're looking for the character/range position in the document, this might help. If you're looking for the X,Y position on the screen, that's a different problem.
Italophile's solutions may work better, as mine would of course change if you changed the document, moving things around. But this stores the current position of your text as the Range.Start attribute.

What exactly are you trying to do with this position once you store it? Just curious as I have never needed to know the position of something in a document. I have needed to know positions when building a UI, or dealing with images, etc. Understanding more of what your bigger goal is may help us help you get there.

Code:
Sub position()
  ' Find characters 1150 and store position in a variable.
  
  Dim docRange As Range
  Set docRange = ActiveDocument.Range
  Dim str_text As String
  str_text = "1150"
  Dim position As Long
  Dim str_message As String
  
  With docRange.Find
    .ClearFormatting
    .Text = str_text
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  
  docRange.Find.Execute
  ' Store range
  position = docRange.Start
  ' Define message for msgBox
  str_message = (str_text & " is at position: " & position & ".")
  
  Call MsgBox(prompt:=str_message, Buttons:=vbOKOnly)
  
End Sub
Attached Images
File Type: png positionSnap.png (7.6 KB, 15 views)
Reply With Quote
  #6  
Old 10-29-2022, 01:09 PM
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

Sorry for my english, it's poor.
I have a document with inserted bookmarks.
The document is created by a VB6 program.
I have tried to read the value of the bookmark, but there is no way.
I have used a code that was sent to me in this forum. It detects the bookmark but does not read the value.

Quote:
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
Reply With Quote
  #7  
Old 10-29-2022, 02:27 PM
Guessed's Avatar
Guessed Guessed is offline Word - position document Windows 10 Word - position document 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

A bookmark can enclose a text range or it can be 'collapsed' in that its end is in the same position as its start. This is the equivalent as having a selection or just a cursor position.

So oBM.Range.Text will only show content if the range's end is greater than its start. You can either modify the contents of the bookmark's range or use the earlier suggestions as more bulletproof methods of avoiding this problem.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 10-30-2022, 01:41 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 Guessed View Post
A bookmark can enclose a text range or it can be 'collapsed' in that its end is in the same position as its start. This is the equivalent as having a selection or just a cursor position.

So oBM.Range.Text will only show content if the range's end is greater than its start. You can either modify the contents of the bookmark's range or use the earlier suggestions as more bulletproof methods of avoiding this problem.
Hello
can you give me an example?
thanks
Reply With Quote
  #9  
Old 10-30-2022, 03:47 AM
Italophile Italophile is offline Word - position document Windows 11 Word - position document Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

When you next ask a question please include as much detail as possible. It is very hard to help someone when they haven't provided vital details, i.e. "I have a document with inserted bookmarks" and "The document is created by a VB6 program"

The attached screenshot shows the difference between a collapsed and enclosing bookmark, and how to show bookmarks in your document.

If you have no control over the document and you know that the code is never more than 4 characters you can edit your code as follows:
Code:
Sub TestBookmark()
    Dim rng As Range
    Dim code As String
    If ActiveDocument.Bookmarks.Exists("code") Then
        Set rng = ActiveDocument.Bookmarks("code").Range
        If rng.Start = rng.End Then
            rng.MoveEnd wdCharacter, 4
            code = rng.Text
            Debug.Print code
        Else
            code = rng.Text
            Debug.Print code
        End If
    Else
        MsgBox "This bookmark doesn't exist" & vbCr & "code"
    End If
End Sub
Attached Images
File Type: png Advanced Options.png (46.3 KB, 13 views)
File Type: png Screenshot_20221030_104105.png (5.6 KB, 13 views)
Reply With Quote
  #10  
Old 10-30-2022, 04:19 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
When you next ask a question please include as much detail as possible. It is very hard to help someone when they haven't provided vital details, i.e. "I have a document with inserted bookmarks" and "The document is created by a VB6 program"

The attached screenshot shows the difference between a collapsed and enclosing bookmark, and how to show bookmarks in your document.

If you have no control over the document and you know that the code is never more than 4 characters you can edit your code as follows:
Code:
Sub TestBookmark()
    Dim rng As Range
    Dim code As String
    If ActiveDocument.Bookmarks.Exists("code") Then
        Set rng = ActiveDocument.Bookmarks("code").Range
        If rng.Start = rng.End Then
            rng.MoveEnd wdCharacter, 4
            code = rng.Text
            Debug.Print code
        Else
            code = rng.Text
            Debug.Print code
        End If
    Else
        MsgBox "This bookmark doesn't exist" & vbCr & "code"
    End If
End Sub
attached image of my bookmark
I have executed the code that you have sent me and it has not given any error.
Code:
Sub TestBookmark()
    Dim rng As Range
    Dim code As String
    If ActiveDocument.Bookmarks.Exists("CodiPacient") Then
        Set rng = ActiveDocument.Bookmarks("CodiPacient").Range
        If rng.Start = rng.End Then
            rng.MoveEnd wdCharacter, 4
            code = rng.Text
            Debug.Print code
        Else
            code = rng.Text
            Debug.Print code
        End If
    Else
        MsgBox "This bookmark doesn't exist" & vbCr & "code"
    End If
End Sub
Can I save the value of the bookmark in a variable?
thanks
Attached Images
File Type: jpg 30-10-2022_12-14-47.jpg (8.7 KB, 14 views)
Reply With Quote
  #11  
Old 10-30-2022, 05:14 AM
Italophile Italophile is offline Word - position document Windows 11 Word - position document Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by bugy View Post
Can I save the value of the bookmark in a variable?
thanks
That's what the code already does. If that isn't what you want, you'll have to be more specific about what you need.
Reply With Quote
  #12  
Old 10-30-2022, 05:59 AM
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

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
Reply With Quote
  #13  
Old 10-30-2022, 08:44 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 BrianHoard View Post
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
Attached Images
File Type: jpg 30-10-2022_16-36-05.jpg (38.2 KB, 14 views)
Reply With Quote
  #14  
Old 10-30-2022, 06:39 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

Okay, I think we're getting closer. I modified your SaveAsBM script as below, replacing the InputBox with code for getting the value of the bookmark named "CodiClient"

It tested fine for me, creating the folder named 11241, and saving the document as a PDF. But the PDF wouldn't open for me, but I trust it's working for you, or you know how to make that part work.

Let me know if this is getting closer to your goal.

Code:
Sub SaveAsBM()
  Dim oBM As String
  Dim sPath As String
  
  Dim bm As Bookmark
  Dim bmName As String
  Dim bmValue As String
  Dim rng_bm As Range
  Dim rng_expanded As Range
  Dim bmCheck As Boolean
  
  bmName = "CodiClient"
  sPath = "C:\Server"
      
  '  oBM = InputBox("RECEPTE", "ENTRA EL CODI DEL PACIENT", "")
  
  ' Store bookmark value for new folder name.
  For Each bm In ActiveDocument.Bookmarks
    If bm.Name = bmName Then
      Set rng_bm = bm.Range
      
      If rng_bm.Start = rng_bm.End Then
        Set rng_expanded = rng_bm
        rng_expanded.MoveEndWhile cset:="0123456789"
        bmValue = rng_expanded.Text
        sPath = (sPath & "\" & bmValue & "\")
        bmCheck = True
      End If
    End If
  Next bm
  
  If bmCheck = True Then
    Debug.Print ("Creating folder: " & sPath)
    CreateFolders sPath
    ActiveDocument.SaveAs sPath & "recepte" & Format(Now, "yyyymmdd hhnnss") & ".pdf"
    bFound = True
  Else
    Call MsgBox(prompt:=("Unable to find bookmark, " & bmName), buttons:=vbCritical)
  End If

End Sub
Reply With Quote
  #15  
Old 10-31-2022, 01:25 AM
Italophile Italophile is offline Word - position document Windows 11 Word - position document Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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
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:01 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