Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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, 17 views)
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 05:40 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft