Microsoft Word Selection Object with code example

Microsoft Word Selection Object with code example

Selection

Selection Object Represents the current cursor position in a Word Document. Selection object offers various properties and methods which can be used to manipulate document area.

Copy

Public Sub CopySelection()
	Dim oRange As Range
	Set oRange = Selection.Range
	oRange.Copy
End Sub

Cut

Public Sub CutSelection()
	Documents(3).ActiveWindow.Selection.Cut
End Sub

Copy Paste

Public Sub CopyPasteSelection()
	ActiveDocument.ActiveWindow.Panes(1).Selection.Copy 
	ActiveDocument.ActiveWindow.Panes(2).Selection.Paste
End Sub

Text property

Public Sub RemoveParaMarkFromSelection()
	Dim sTemp as String 
	sTemp = Selection.Text 
	
	If Right(sTemp, 1) = vbCr Then  
		sTemp = Left(sTemp, Len(sTemp) - 1)
	End if
End Sub

Collapse and Expand

Public Sub CollapseSelection()
	Selection.EndOf Unit:=wdStory, Extend:=wdMove 
	Selection.HomeKey Unit:=wdLine, Extend:=wdExtend 
	Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
End Sub

Select

Public Sub SelectExample()
	Options.ReplaceSelection = True 
	ActiveDocument.Sentences(1).Select 
	Selection.TypeText "This is a new paragraph." 
	Selection.TypeParagraph
End Sub

Delete

Public Sub CopyPasteSelection()
	With Documents(1) 
		.Paragraphs.Last.Range.Select 
		.ActiveWindow.Selection.Cut 
	End With 

	With Documents(2).ActiveWindow.Selection 
		.StartOf Unit:=wdStory, Extend:=wdMove 
		.Paste 
	End With
End Sub

Font

Public Sub ChangeFont()
	If Selection.Font.Name = "Times New Roman" Then  
		Selection.Font.Name = "Arial"
	End If
End Sub

Type

Public Sub SelectionType()
	If Selection.Type = wdSelectionIP Then 
		MsgBox Prompt:="You have not selected any text!!!" 
		Exit Sub 
	End If
End Sub

Next>>InsertFile Method Microsoft Word VBA

One thought on “Microsoft Word Selection Object with code example

  1. Do you have a spam issue on this website; I also am a blogger,
    and I was wanting to know your situation; wee have developed some nice procedures and we are
    looking to trade strategieds with others, please shoot mme an email if interested.

Leave a Reply

Your email address will not be published. Required fields are marked *