Menu

 

Sunday, June 27, 2010

Working With Text

Now workign with Text in Word Using VBA

VBA Code :-

Sub InsertTextAtEndOfDocument()
ActiveDocument.Content.InsertAfter Text:=" The end."
End Sub

C# Code:-

public void InsertTextAtEndofDocument()
{
ThisApplication.ActiveDocument.Content.InsertAfter("The end.");
}

VBA Code:-

Sub SelectTable()
ActiveDocument.Tables(1).Select
End Sub

C# Code :-

public void SelectTable()
{

ThisApplication.ActiveDocument.Tables[1].Select();
}

VBA Code :-

Sub SelectField()
ActiveDocument.Fields(1).Select
End Sub

C# Code :-

public void selectfiled()
{
ThisApplication.ActiveDocument.Fields[1].Select();
}

Sub AddTextBeforeSelection()
Selection.InsertBefore Text:="new text "
End Sub

C# Code:-

public void AddTextBeforeSelection()
{
ThisApplication.Selection.InsertBefore("New text");
}

VBA Code :-

Sub SelectRange()
Dim rngParagraphs As Range
Set rngParagraphs = ActiveDocument.Range( _
Start:=ActiveDocument.Paragraphs(1).Range.Start, _
End:=ActiveDocument.Paragraphs(4).Range.End)
rngParagraphs.Select
End Sub

C# Code :-

public void SelectRange()
{
Word.Range myrange;

object start =ThisApplication.ActiveDocument.Paragraphs[1].Range.Start;
object last = ThisApplication.ActiveDocument.Paragraphs[1].Range.End;
myrange = ThisApplication.ActiveDocument.Range(ref start, ref last);
myrange.Select();
}

VB Code :-

Sub IsTextSelected()
If Selection.Type = wdSelectionIP Then MsgBox "Nothing is selected"
End Sub

C# Code :-

public void IsTextSelected()
{
if (ThisApplication.Selection.Type == Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionIP)
{
MessageBox.Show("Nothing Selected");

}
}

VBA Code :-

Sub CollapseToBeginning()
Selection.Collapse Direction:=wdCollapseStart
End Sub

C# Code:-

public void collapsetobeginning()

{
object selection = Word.WdCollapseDirection.wdCollapseStart;
ThisApplication.Selection.Collapse(ref selection);
}

VBA Code:-

Sub CollapseToEnd()
Dim rngWords As Range

Set rngWords = ActiveDocument.Words(1)
With rngWords
.Collapse Direction:=wdCollapseEnd
.Text = "(This is a test.) "
End With
End Sub

C# Code :-

public void CollapseToEnd()
{
Word.Range rngWords;
object direction = Word.WdCollapseDirection.wdCollapseEnd;
rngWords = ThisApplication.ActiveDocument.Words[1];
rngWords.Collapse(ref direction);
rngWords.Text = "This is test";

}

VBA Code:-

Sub ExtendSelection()
Selection.MoveEnd Unit:=wdWord, Count:=3
End Sub

C# Code :-

public void ExteneSelection()
{
object Unit =Word.WdUnits.wdWord;
object Count = 3;
ThisApplication.Selection.MoveEnd(ref Unit, ref Count);
}

VBA Code:-

Sub ExtendRange()
Dim rngParagraphs As Range

Set rngParagraphs = ActiveDocument.Paragraphs(1).Range
rngParagraphs.MoveEnd Unit:=wdParagraph, Count:=2
End Sub

C# Code :-


public void ExtendRange()
{
object Unit = Word.WdUnits.wdParagraph;
object Count = 2;
ThisApplication.Selection.MoveEnd(ref Unit, ref Count);
}

VBA Code :-

Sub ChangeText()
ActiveDocument.Words(1).Text = "The "
End Sub

C# Code :-

public void ChangeText()
{
ThisApplication.ActiveDocument.Words[1].Text = "The ";
}

VBA Code :-

Sub DeleteText()
Dim rngFirstParagraph As Range

Set rngFirstParagraph = ActiveDocument.Paragraphs(1).Range
With rngFirstParagraph
.Delete
.InsertAfter Text:="New text"
.InsertParagraphAfter
End With
End Sub

C# Code :-

public void deletetext()
{
Word.Range rngFirstParagraph;

rngFirstParagraph = ThisApplication.ActiveDocument.Paragraphs[1].Range;
rngFirstParagraph.Delete(ref missing, ref missing);
rngFirstParagraph.InsertAfter("New text");
rngFirstParagraph.InsertParagraphAfter();
}

VB Code :-

Sub FormatSelection()
With Selection.Font
.Name = "Times New Roman"
.Size = 14
.AllCaps = True
End With
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.5)
.Space1
End With
End Sub

C# Code :-

public void FormatSelection()
{
ThisApplication.Selection.Font.Name = "Times New Roman";
ThisApplication.Selection.Font.Size = 14;
ThisApplication.Selection.Font.AllCaps = 1;

ThisApplication.Selection.ParagraphFormat.LeftIndent = ThisApplication.InchesToPoints(0.5f);
ThisApplication.Selection.ParagraphFormat.Space1();

}

VBA Code :-

Sub FormatRange()
Dim rngFormat As Range
Set rngFormat = ActiveDocument.Range( _
Start:=ActiveDocument.Paragraphs(1).Range.Start, _
End:=ActiveDocument.Paragraphs(3).Range.End)
With rngFormat
.Font.Name = "Arial"
.ParagraphFormat.Alignment = wdAlignParagraphJustify
End With
End Sub

C# Code:-

public void FormatRange()
{
Word.Range rngFormat = null;
object start = ThisApplication.ActiveDocument.Paragraphs[1].Range.Start;
object last = ThisApplication.ActiveDocument.Paragraphs[2].Range.End;
ThisApplication.ActiveDocument.Range(ref start, ref last);

rngFormat.Font.Name = "Arial";
rngFormat.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;


}

VBA Code :-


Sub InsertFormatText()
Dim rngFormat As Range
Set rngFormat = ActiveDocument.Range(Start:=0, End:=0)
With rngFormat
.InsertAfter Text:="Title"
.InsertParagraphAfter
With .Font
.Name = "Tahoma"
.Size = 24
.Bold = True
End With
End With
With ActiveDocument.Paragraphs(1)
.Alignment = wdAlignParagraphCenter
.SpaceAfter = InchesToPoints(0.5)
End With
End Sub

C# Code

public void InsertFormatText()
{
Word.Range rngFormat = null;
object start = 0;
object last = 0;

rngFormat = ThisApplication.ActiveDocument.Range(ref start, ref last);
rngFormat.InsertAfter("Title");
rngFormat.InsertParagraphAfter();
rngFormat.Font.Name = "Tahoma";
rngFormat.Font.Size = 24;
rngFormat.Bold = 1;
ThisApplication.ActiveDocument.Paragraphs[1].Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
ThisApplication.ActiveDocument.Paragraphs[1].SpaceAfter = ThisApplication.InchesToPoints(0.5f);

}

VBA Code :-

Sub ToggleParagraphSpace()
With Selection.Paragraphs(1)
If .SpaceBefore <> 0 Then
.SpaceBefore = 0
Else
.SpaceBefore = 6
End If
End With
End Sub

C# Code :-

public void ToggleParagraphSpace()
{
if (ThisApplication.Selection.Paragraphs[1].SpaceBefore != 0)
{
ThisApplication.Selection.Paragraphs[1].SpaceBefore = 0;
}
else
{
ThisApplication.Selection.Paragraphs[1].SpaceBefore = 6;
}
}

VBA Code :-

Sub ToggleBold()
Selection.Font.Bold = wdToggle
End Sub

C# Code :-

public void ToggleBold()
{
ThisApplication.Selection.Font.Bold = (int)Word.WdConstants.wdToggle;
}

VBA Code :-

Sub FormatMargins()
With ActiveDocument.PageSetup
.LeftMargin = .LeftMargin + InchesToPoints(0.5)
.RightMargin = .RightMargin + InchesToPoints(0.5)
End With
End Sub

C# Code :-

public void formarmargins()
{
ThisApplication.ActiveDocument.PageSetup.LeftMargin = ThisApplication.ActiveDocument.PageSetup.LeftMargin + ThisApplication.InchesToPoints(0.5f);

ThisApplication.ActiveDocument.PageSetup.RightMargin = ThisApplication.ActiveDocument.PageSetup.RightMargin + ThisApplication.InchesToPoints(0.5f);

}

VBA Code :-

With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Text = "Hello"
.Execute
End With

C# Code :-

public void Testmethod()
{
ThisApplication.Selection.Find.Forward = true;
ThisApplication.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;
ThisApplication.Selection.Find.Text = "Hello";
ThisApplication.Selection.Find.Execute(ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing ,ref missing,ref missing );

}

VBA Code :-

With ActiveDocument.Content.Find
.Text = "blue"
.Forward = True
.Execute
If .Found = True Then .Parent.Bold = True
End With

C# Code :-

public void tesmethod()
{
ThisApplication.ActiveDocument.Content.Find.Text = "Blue";
ThisApplication.ActiveDocument.Content.Find.Forward = true;
if (ThisApplication.ActiveDocument.Content.Find.Found == true)
{
ThisApplication.ActiveDocument.Content.Bold = 1;
}
}

VBA Code :-

With Selection.Find
.ClearFormatting
.Text = "hi"
.Replacement.ClearFormatting
.Replacement.Text = "hello"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With

C# Code :-

public void Selectionfind()
{
ThisApplication.Selection.Find.ClearFormatting();
ThisApplication.Selection.Find.Text = "hi";
ThisApplication.Selection.Find.Replacement.ClearFormatting();
ThisApplication.Selection.Find.Text = "Hello";
object Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
object Foward = true;
object Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
ThisApplication.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Foward, ref Wrap, ref missing, ref missing, ref Replace, ref missing, ref missing, ref missing, ref missing);

}

VBA Code :-

Sub FindHeadingStyle()
With Selection.Find
.ClearFormatting
.Style = wdStyleHeading1
.Execute FindText:="", Format:=True, _
Forward:=True, Wrap:=wdFindStop
If .Found = True Then MsgBox Selection.Text
End With
End Sub

C# Code :-

public void FindHeadingStyle()
{
object styleobject =Word.WdBuiltinStyle.wdStyleHeading1;

ThisApplication.Selection.Find.ClearFormatting();
ThisApplication.Selection.Find.set_Style(ref styleobject);
object Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;

object Findtext = true;
object Foward = true;


ThisApplication.Selection.Find.Execute(ref Findtext, ref missing, ref missing, ref missing, ref missing, ref missing, ref Foward, ref Wrap, ref missing, ref missing, ref missing , ref missing, ref missing, ref missing, ref missing);
}

VBA Code:-

Sub ShowSelection()
Dim strText As String
strText = Selection.Text
MsgBox strText
End Sub

C# Code:-

public void ShowSelection()
{
String strText;
strText = ThisApplication.Selection.Text;
MessageBox.Show(strText);
}

VBA Code :-

Sub ShowFirstWord()
Dim strFirstWord As String
strFirstWord = ActiveDocument.Words(1).Text
MsgBox strFirstWord
End Sub

C# Code :-

public void ShowfirstWord()
{
string strFirstWord;
strFirstWord = ThisApplication.ActiveDocument.Words[1].Text;
MessageBox.Show(strFirstWord);
}

VBA Code :-

Sub ShowFirstBookmark()
Dim strBookmark As String
If ActiveDocument.Bookmarks.Count > 0 Then
strBookmark = ActiveDocument.Bookmarks(1).Range.Text
MsgBox strBookmark
End If
End Sub

C# Code :-

public void ShowFirstbookmark()
{
object refcountindex =1;
string strBookmark;
if (ThisApplication.ActiveDocument.Bookmarks.Count > 0)
{
strBookmark = ThisApplication.ActiveDocument.Bookmarks.get_Item(ref refcountindex).Range.Text;
MessageBox.Show(strBookmark);
}
}

1 comment:

  1. ThisApplication.Selection.Find.ClearFormatting(); ThisApplication is not included in c#

    ReplyDelete