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);
}
}

Saturday, June 26, 2010

Converting Macro Example From Sample

VBA Code:-

Sub Macro1()
Selection.Goto What:=wdGotoBookmark, Name:="Temp"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="New text"
End Sub

C# Code

public void Macro1()
{
object item = Word.WdGoToItem.wdGoToBookmark;
object name = "Temp";

this.ThisApplication.Selection.GoTo(ref item, ref missing, ref missing, ref name);

object unit = Word.WdUnits.wdCharacter;
object count = 1;


this.ThisApplication.Selection.MoveRight(ref unit, ref count, ref missing);

string mytext = "New text";

this.ThisApplication.Selection.TypeText(mytext);

}

VBA Code:-

Sub MyMacro()
If ActiveDocument.Bookmarks.Exists("Temp") = True Then
endloc = ActiveDocument.Bookmarks("Temp").End
ActiveDocument.Range(Start:=endloc, _
End:=endloc).InsertAfter "New text"
End If
End Sub

C# Code:-

public void myMacro()
{
object endloc;
object bookmarkindex = 1;
if (ThisApplication.ActiveDocument.Bookmarks.Exists("Temp"))
{
endloc = ThisApplication.ActiveDocument.Bookmarks.get_Item(ref bookmarkindex).End;
ThisApplication.ActiveDocument.Range(ref endloc, ref endloc).InsertAfter("Newtext");

}
}

VBA Code :-

Sub Macro1()
Selection.HomeKey Unit:=wdStory
Selection.TypeText Text:="Title"
Selection.ParagraphAlignment.Alignment = wdAlignParagraphCenter
End Sub

C# Code:-

public void Macro1()
{
object unit = Word.WdUnits.wdStory;
ThisApplication.Selection.HomeKey(ref unit, ref missing);
ThisApplication.Selection.Text = "Title";
ThisApplication.Selection.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

}

VBA Code:-

Sub MyMacro()
With ActiveDocument.Range(Start:=0, End:=0)
.InsertAfter "Title"
.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End Sub

C# Code

public void MyMacro()
{
object Start =0;
object End = 0;
ThisApplication.ActiveDocument.Range(ref Start, ref End).InsertAfter("Title");
ThisApplication.ActiveDocument.Range(ref Start, ref End).ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

}

VBA Code:-

Sub Macro1()
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 6
.SpaceAfter = 6
.LineSpacingRule = 0
.Alignment = wdAlignParagraphLeft
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = 10
End With
End Sub

Sub MyMacro()
With Selection.ParagraphFormat
.SpaceBefore = 6
.SpaceAfter = 6
End With
End Sub

C# Code:-

public void Macro1()
{
ThisApplication.Selection.Paragraphs.Format.LeftIndent = ThisApplication.InchesToPoints(0);
ThisApplication.Selection.Paragraphs.Format.RightIndent = ThisApplication.InchesToPoints(0);

ThisApplication.Selection.Paragraphs.Format.SpaceBefore = 6f;
ThisApplication.Selection.Paragraphs.Format.SpaceAfter = 6f;
ThisApplication.Selection.Paragraphs.Format.LineSpacingRule = 0;
ThisApplication.Selection.Paragraphs.Format.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
ThisApplication.Selection.Paragraphs.Format.WidowControl = 1;
ThisApplication.Selection.Paragraphs.Format.KeepWithNext = -1;
ThisApplication.Selection.Paragraphs.Format.KeepTogether = -1;

ThisApplication.Selection.Paragraphs.Format.PageBreakBefore = -1;
ThisApplication.Selection.Paragraphs.Format.Hyphenation = 1;
ThisApplication.Selection.Paragraphs.Format.FirstLineIndent = ThisApplication.InchesToPoints(0);
ThisApplication.Selection.Paragraphs.Format.OutlineLevel = Microsoft.Office.Interop.Word.WdOutlineLevel.wdOutlineLevel9;






}

public void MyMacro()
{
ThisApplication.Selection.Paragraphs.Format.SpaceBefore = 6f;
ThisApplication.Selection.Paragraphs.Format.SpaceAfter = 6f;

}

VBA Code:-

Sub Macro1()
Documents.Open FileName:="C:\My Documents\Test.doc", _
ConfirmConversions:= False, ReadOnly:=False, _
AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
End Sub

C# Code:-

public void Macro1()
{
object fileName = @"C:\Test\NewDocument.doc";
object ConfirmConversions = false;
object ReadOnly = false;
object AddToRecentFiles = false;
object PasswordDocument = "";
object PasswordTemplate = "";
object Revert = false;
object WritePasswordDocument = "";
object WritePasswordTemplate = "";
object Format = Word.WdOpenFormat.wdOpenFormatAuto;


this.Application.Documents.Open(ref fileName,
ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
}

VBA Code:-

Sub FileSave()
'
' FileSave Macro
' Saves the active document or template
'
ActiveDocument.Save

End Sub

C# Code:-

public void FileSave()
{
this.ThisApplication.ActiveDocument.Save();
}

VBA To C#

For Theroy
http://www.microsoft.com/downloads/details.aspx?familyid=179bee82-e6e6-4b78-aff9-9a541167541f&displaylang=en

download the VBA Theroy I will Try to Migrate code


I will Only Put the Code and its Sample , link in C# No Explaination for code..
Chk the above Chm file for explanation.

Let Start With Concepts(this in Chm file which u will download )..

VBA CODE :-

Sub GetDocumentName()
Dim strDocName As String
strDocName = ActiveDocument.Name
MsgBox strDocName
End Sub

C# Version :-

public void GetDocumentName()
{
string strDocName = "";
strDocName = this.ThisApplication.ActiveDocument.Name;
MessageBox.Show(strDocName);
}


VBA CODE :-

Sub PrintThreePages()
ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:="1-3"
End Sub

C# Version :-

public void PrintThreePages()
{
/// 4 is wdPrintRangeOfPages chk below link for more info
///http://msdn.microsoft.com/en-us/library/aa211923%28office.11%29.aspx

object mywordrange = (object)4;
object myPages = (object)"1-3";

this.ThisApplication.ActiveDocument.PrintOut(ref missing, ref missing, ref mywordrange, ref missing, ref missing, ref missing, ref missing, ref missing, ref myPages, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

}

VBA CODE :-

Sub CloseDocument()
Documents(1).Close
End Sub

C# Code :-

public void CloseDocument()
{
this.ThisApplication.Documents.Close(ref missing, ref missing, ref missing);
}

VBA Code:-

Sub CloseSalesDoc()
Documents("Sales.doc").Close
End Sub

C# Code

public void CloseDocument()
{
//// learned from link below
/// http://msdn.microsoft.com/en-us/library/af6z0wa2.aspx

Word._Document doc = Application.Documents["Mydocument.doc"] as Word._Document;
doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
}

VBA Code:-

Sub SaveAllOpenDocuments()
Documents.Save
End Sub

C# Code:-

public void SaveAllOpenDocuments()
{

Word.Document mydoc = ( Word.Document)this.ThisApplication.Documents;
mydoc.Save();
}

VBA Code :-

Sub SaveSalesDoc()
Documents("Sales.doc").Save
End Sub

C# Code

Word.Document mydoc = (Word.Document)this.ThisApplication.Documents;
object testmydoc = "D://testsaveas.docx";
mydoc.SaveAs(ref testmydoc, 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 :-

Sub CloseDocSaveChanges()
ActiveDocument.Close SaveChanges:=wdSaveChanges
End Sub

C# Code :-
//// Link may be usefull
http://msdn.microsoft.com/en-us/library/af6z0wa2%28VS.80%29.aspx

object fileName = "Name_of_document";
object SaveChanges = Word.WdSaveOptions.wdSaveChanges;
Word.DocumentClass doc = Application.Documents.get_Item(ref fileName) as Word.DocumentClass;

doc.Close(ref SaveChanges, ref missing, ref missing);

VBA Code:-

Sub MaximizeDocumentWindow()
ActiveDocument.ActiveWindow.WindowState = wdWindowStateMaximize
End Sub

C# Code:-

public void MaximizeDocumentWindow()
{
ThisApplication.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMaximize
;
}