Solved How to get current vertical position in centimeters (relevant to page start)

Capitala

Member
Local time
Today, 07:46
Joined
Oct 21, 2021
Messages
89
Hi great people!
I need to get the current position of the cursor (vertically) as relevant to the start of the page but in CENTIMETER
I tried this code:
Selection.Information(wdVerticalPositionRelativeToPage)
it gives me, as I thought, the position but in points.
To make it clearer: If I'm in the third line for example, I need to know the distance between the current position and the top of the page (say: 3 cm ....etc.)
Is it possible?
Thanks a mile
 
You convert it, surely?
Code:
Sub GetVerticalPositionInCentimeters()
    Dim positionPoints As Single
    Dim positionCm As Single
    
    ' Get vertical position in points (from top of page)
    positionPoints = Selection.Information(wdVerticalPositionRelativeToPage)
    
    ' Convert points to centimeters: 1 point = 1/72 inch; 1 inch = 2.54 cm
    positionCm = positionPoints * 2.54 / 72
    
    MsgBox "Vertical position from top of page: " & Format(positionCm, "0.00") & " cm", vbInformation
End Sub
 
You convert it, surely?
Code:
Sub GetVerticalPositionInCentimeters()
    Dim positionPoints As Single
    Dim positionCm As Single
   
    ' Get vertical position in points (from top of page)
    positionPoints = Selection.Information(wdVerticalPositionRelativeToPage)
   
    ' Convert points to centimeters: 1 point = 1/72 inch; 1 inch = 2.54 cm
    positionCm = positionPoints * 2.54 / 72
   
    MsgBox "Vertical position from top of page: " & Format(positionCm, "0.00") & " cm", vbInformation
End Sub
Thaaaaaaaaaaaaaaaaaanks. working perfect.
But, I can only use (from top of page). Is it possible to be (from bottom of page)?
sorry for any incontinence
 
In Word context, you can use Page.Height to get the page height in points. Therefore, get the position as you got it before, using the Selection.Information(wdVerticalPositionRelativeToPage) call. Then subtract that number from Page.Height. The result will be the position from the bottom, in points. But you already know how to convert points to centimeters, so there you are.

Some useful links:


 
Jeez, make your mind up. :)

This is what ChatgPT offers
Code:
Sub VerticalPositionFromPageBottomCM()
    Dim posFromTopPts As Single
    Dim pageHeightPts As Single
    Dim posFromBottomPts As Single
    Dim posFromBottomCm As Single
   
    ' Get the current position from the top of the page (in points)
    posFromTopPts = Selection.Information(wdVerticalPositionRelativeToPage)
   
    ' Get the height of the current page (in points)
    pageHeightPts = Selection.PageSetup.PageHeight
   
    ' Calculate position from the bottom (in points)
    posFromBottomPts = pageHeightPts - posFromTopPts
   
    ' Convert from points to centimeters (1 point = 0.0352778 cm)
    posFromBottomCm = posFromBottomPts * 0.0352778
   
    ' Output the result
    MsgBox "Vertical position from bottom of page: " & Format(posFromBottomCm, "0.00") & " cm"
End Sub
 
Last edited:
So far it's perfect. I appreciate your efforts.
Now, after the amazing code above, I need to use text.typeparagraph until the cursor reaches a vertical position equals or greater than 18 cm.
Why I do this? I have one sentence which I need to place on an almost the same position at the bottom of the page (at about 18 cm from top of the page or 10 cm from bottom of the page.
Thanks again
 
Do not thank me, thank ChatGPT. :)
I would just put a marker there and replace that with your sentence.
 

Users who are viewing this thread

Back
Top Bottom