简体   繁体   中英

MS Word macro — special handling for top of page

I'm trying to write an MS Word macro to drop down one line and type a certain phrase unless the cursor at the very top of the document or following a "hard" page break, in which case it should only type the message. The code goes pretty much like this --

If <TOP OF PAGE> Then
  Selection.TypeText Text:="top of page"  
Else
  Selection.TypeParagraph
  Selection.TypeText Text:="not top of page"  
End If

-- but I don't know the specific syntax for testing whether the cursor is at the top of a page. Can anyone fill me in? My software version is MS Word 2003. Thanks.

You could use Selection.Information(wdFirstCharacterLineNumber) = 1 .

If Selection.Information(wdFirstCharacterLineNumber) = 1 Then
  Selection.TypeText Text:="top of page"  
Else
  Selection.TypeParagraph
  Selection.TypeText Text:="not top of page"  
End If

Selection.Information(wdFirstCharacterLineNumber) will return the line number of the current selection within that page. See MSDN .

PS: I have tested this with Word 2010 and I don't have access to previous version. So, give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM