VBA Advanced Techniques

The total number of documents currently open

Application.Documents.Count 

Current version of Word

Application.Version 

Current build of Word

Application.Build 

StyleAreaWidth ??


Getting the Position of the cursor

x = Selection.Information(wdInformation.wdHorizontalPositionRelativeToPage) 
y = Selection.Information(wdInformation.wdVerticalPositionRelativeToPage)

72pts = 1 inch = 2.54cm
This only works when you are in Page view.
If you are in Normal view it returns the distance from the top margin.
Always set the view to Page view and the magnificationt o 100% before using the Information proeprty to get the cursor position.


Switch to Print Preview

This changes the view to print preview

Application.PrintPreview = True 

Define the size of the window on the screen

With Application 
   .WindowState = wdWindowStateNormal
   .Height = 400
   .Width = 300
End With

Check the File Exists before opening

With Application.FileSearch 
   .FileName = "document.doc"
   .Lookin = "C:\Temp\"
   .Execute
   If .FoundFiles.Count > 1 Then
'file exists so it can be opened
   End If
End Sub

Is there a document currently open ?

If (Documents.Count >= 1) Then 
'a document is currently open
Else
'no document are open
End If

SharePoint

Detect whether SharePoint is present
You can use this code to avoid using SharePoint features in documents that are not part of a SharePoint shared workspace.

If ActiveDocument.SharedWorkspace.Connected then 
'SharePoint is present
Else
'SharePoint is not present
End If

Document.CanCheckIn
For this to be true, you must have the document checked out from the SharePoint server.


Document.CheckIn
Checks the document back into the SharePoint server. Use the CanCheckIn property to determine if this can be done or not.


Document.DeleteAllEditableRanges
Useful for resetting the users work area on some document types.


Document.EnforceStyle
Whether to allow manual formatting or force the use of styles only.


Document.GotoEditableRange
Range.GoToEditableRange


Useful for programmatically addressing the available content
Document.SelectAllEditableRanges


Limited uses on some document types for saving the edits elsewhere.
Document.Sync


Updates the local copy of the document from the SharePoint server. Triggers the two sync events after the sync.
View.ShadeEditableRanges





© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext