Menu

 

Sunday, July 18, 2010

Reference-Collection:- (B) :- Bookmarks Collection

VBA Code :-

If ActiveDocument.Bookmarks.Exists("temp") = True Then
ActiveDocument.Bookmarks("temp").Select
End If

C# Code :-

public void BookMarkExists()
{
bool isBookMarkPresent = ThisApplication.ActiveDocument.Bookmarks.Exists("Temp");

if (isBookMarkPresent)
{
object Name = "temp";
ThisApplication.ActiveDocument.Bookmarks.get_Item(ref Name).Select();
}
}

VBA Code :-

ActiveDocument.Bookmarks.Add Name:="temp", Range:=Selection.Range

C# Code :-

public void BookmarksAdd()
{
string Name = "temp";
Word.Range wordrange = ThisApplication.Selection.Range;
object wr = wordrange;

ThisApplication.ActiveDocument.Bookmarks.Add(Name, ref wr);

}

VBA Code :-

ActiveDocument.Paragraphs(1).Borders.Enable = True

C# Code :-

public void BordersEnable()
{
ThisApplication.ActiveDocument.Paragraphs[1].Borders.Enable = 1;
}

VBA Code :-

With ActiveDocument.Paragraphs(1).Borders(wdBorderBottom)
.LineStyle = wdLineStyleDouble
.LineWidth = wdLineWidth025pt
End With

C# Code :-

public void ParaBorder()
{
ThisApplication.ActiveDocument.Paragraphs[1].Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDouble;
ThisApplication.ActiveDocument.Paragraphs[1].Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth025pt;
}

VBA Code :-

With Selection.Characters(1)
.Font.Size = 36
.Borders.Enable = True
End With

C# Code :-

public void Characterselection()
{
ThisApplication.Selection.Characters[1].Font.Size = 36;
ThisApplication.Selection.Characters[1].Borders.Enable = 1;
}

VBA Code :-

For Each aBorder In ActiveDocument.Sections(1).Borders
With aBorder
.ArtStyle = wdArtSeattle
.ArtWidth = 20
End With
Next aBorder

C# Code :-

public void sectionBorder()
{
foreach (Word.Border aborder in ThisApplication.ActiveDocument.Sections[1].Borders)
{
aborder.ArtStyle = Microsoft.Office.Interop.Word.WdPageBorderArt.wdArtSeattle;
aborder.ArtWidth = 20;
}
}

VBA Code :-

Dim objBreaks As Breaks

Set objBreaks = ActiveDocument.ActiveWindow _
.Panes(1).Pages(1).Breaks

C# Code :-

Dim objBreaks As Breaks

Set objBreaks = ActiveDocument.ActiveWindow _
.Panes(1).Pages(1).Breaks

Refrence Collections :- (A) :- Addins Collection

VBA Code :-

For Each ad In AddIns
If ad.Installed = True Then
MsgBox ad.Name & " is installed"
Else
MsgBox ad.Name & " is available but not installed"
End If
Next ad

C# Code

public void findaddin()
{



foreach (Word.AddIn ad in this.Application.AddIns)
{
string straddinname = ad.Name;
if (ad.Installed == true)
{
}
else
{
MessageBox.Show(straddinname);
}

}
}

VBA Code :-

AddIns.Add FileName:="C:\Templates\Other\Letter.dot", Install:=True

C# Code :-

public void AddAddins()
{
string FileName = @"C:\Templates\Other\Letter.dot";
object Installed = true;
this.Application.AddIns.Add(FileName, ref Installed);
}

VBA Code :-

Set rac = ActiveDocument.Shapes _
.AddShape(msoShapeRightArrowCallout, 10, 10, 250, 190)
With rac.Adjustments
.Item(1) = 0.5 'adjusts width of text box
.Item(2) = 0.15 'adjusts width of arrow head
.Item(3) = 0.8 'adjusts length of arrow head
.Item(4) = 0.4 'adjusts width of arrow neck
End With

C# Code :-

public void Addjustment()
{

int myvalue = (int)Microsoft.Office.Core.MsoAutoShapeType.msoShapeRightArrowCallout;
Word.Shape rac = this.Application.ActiveDocument.Shapes.AddShape(myvalue, 10.0f, 10.0f, 250.0f, 190.0f, ref missing);
rac.Adjustments[1] = 0.5f;
rac.Adjustments[2] = 0.15f;
rac.Adjustments[3] = 0.8f;
rac.Adjustments[4] = 0.4f;

}

VBA Code :-

For Each autoCap In AutoCaptions
If autoCap.AutoInsert = True Then
MsgBox autoCap.Name & " is configured for auto insert"
End If
Next autoCap

C# Code :-

public void AutocaptionCollection()
{

foreach(Word.AutoCaption autocap in ThisApplication.AddIns.Application.AutoCaptions )
{
if (autocap.AutoInsert == true)
{
MessageBox.Show(autocap.Name);
}
else
{
MessageBox.Show(autocap.Name);
}
}
}

VBA Code :-

AutoCaptions("Microsoft Word Table").CaptionLabel.Name

C# Code :-

public void ReadAutoCaptionIndex()
{
object NameIndex = "Microsoft Word Table";
string name = "";
name = ThisApplication.AddIns.Application.AutoCaptions.get_Item(ref NameIndex).Application.CaptionLabels.Application.Name;
MessageBox.Show(name);
}

VBA Code :-

AutoCaptions(1).Name

C# Code :-

public void ReadAutoCaptionnumericIndex()
{
object NameIndex = "1";
string name = "";
name = ThisApplication.AddIns.Application.AutoCaptions.get_Item(ref NameIndex).Application.CaptionLabels.Application.Name;
MessageBox.Show(name);
}

VBA Code :-

MsgBox AutoCorrect.Entries.Count

C# Code :-

public void countAutoCorrect()
{
int totalCount = this.Application.AutoCorrect.Entries.Count;
MessageBox.Show(totalCount.ToString());
}

VBA Code :-

AutoCorrect.Entries.Add Name:="thier", Value:="their"

C# Code :-

{
string name = "thier";
string value = "their";
this.Application.AutoCorrect.Entries.Add( name, value);
}

VBA Code :-

AutoCorrect.Entries.AddRichText Name:="PMO", Range:=Selection.Range

C# Code :-

public void AddRichText()
{
string name = "thier";
Word.Range wr = this.Application.Selection.Range;
this.Application.AutoCorrect.Entries.AddRichText(name, wr);

}

VBA Code :-

AutoCorrect.Entries("teh").Value = "the"

C# Code :-

public void autocorrctwordIndex()
{
object obValue = "teh";
this.Application.AutoCorrect.Entries.get_Item(ref obValue).Value = "the";
}

VBA Code :-

MsgBox "Name = " & AutoCorrect.Entries(1).Name & vbCr & _
"Value " & AutoCorrect.Entries(1).Value

C# Code :-

public void AutoCorrectEntriesIndex()
{
object Indexvalue = 1;
string strName = this.Application.AutoCorrect.Entries.get_Item(ref Indexvalue).Name;

MessageBox.Show(strName);

string strValue = this.Application.AutoCorrect.Entries.get_Item(ref Indexvalue).Value;
MessageBox.Show(strValue);
}

VBA Code :-

For Each i In NormalTemplate.AutoTextEntries
If LCase(i.Name) = "test" Then MsgBox "AutoText entry exists"
Next i

C# Code :-

public void AutotextEntries()
{

int countAutoTextEntries = ThisApplication.NormalTemplate.AutoTextEntries.Count;


int i = 1;
while (i <= countAutoTextEntries)
{
object objName = i;
string strname = ThisApplication.NormalTemplate.AutoTextEntries.get_Item(ref objName).Name;
if (strname == "test")
{
MessageBox.Show("Text AutoText Entry exists");
}
i++;
}
}

VBA Code :-

NormalTemplate.AutoTextEntries.Add Name:="Blue", _
Range:=Selection.Range

C# Code :-

public void AddAutotextEntries()
{
string name = "Avinash";
Word.Range wr = this.Application.Selection.Range;
ThisApplication.NormalTemplate.AutoTextEntries.Add(name, wr);
}

VBA code :-

NormalTemplate.AutoTextEntries("cName").Value = _
"The Johnson Company"

C# Code :-

public void AutoTextEntries()
{
object cName = "cName";
ThisApplication.NormalTemplate.AutoTextEntries.get_Item(ref cName).Value = "This Avinash Company";
}

VBA Code :-

Set myTemplate = ActiveDocument.AttachedTemplate
MsgBox "Name = " & myTemplate.AutoTextEntries(1).Name & vbCr _
& "Value " & myTemplate.AutoTextEntries(1).Value

C# Code :-

blic void AutotexrEntries()
{
Word.Template myTemplate = (Word.Template)this.Application.ActiveDocument.get_AttachedTemplate();
int mytempcount = myTemplate.AutoTextEntries.Count;

int i = 1;
while (i <= mytempcount)
{
object objName = i;
string strname = ThisApplication.NormalTemplate.AutoTextEntries.get_Item(ref objName).Name;
string strValue = ThisApplication.NormalTemplate.AutoTextEntries.get_Item(ref objName).Value;
i++;

}
}

Acessing Data and Other Application

VBA Code :-

Sub UsingDAOWithWord()
Dim docNew As Document
Dim dbNorthwind As DAO.Database
Dim rdShippers As Recordset
Dim intRecords As Integer

Set docNew = Documents.Add
Set dbNorthwind = OpenDatabase _
(Name:="C:\Program Files\Microsoft Office\Office11\" _
& "Samples\Northwind.mdb")
Set rdShippers = dbNorthwind.OpenRecordset(Name:="Shippers")
For intRecords = 0 To rdShippers.RecordCount - 1
docNew.Content.InsertAfter Text:=rdShippers.Fields(1).Value
rdShippers.MoveNext
docNew.Content.InsertParagraphAfter
Next intRecords
rdShippers.Close
dbNorthwind.Close
End Sub

C# Code :-

public void UsingDAOWithWord()
{
///Download Northwind from
///http://www.microsoft.com/downloads/en/confirmation.aspx?familyId=c6661372-8dbe-422b-8676-c632d66c529c&displayLang=en
///if donot have

string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\\Database\\Nwind.mdb";
DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
string strAccessSelect = "select * from Shippers";
Word.Range rngFirstParagraph;

rngFirstParagraph = ThisApplication.ActiveDocument.Paragraphs[1].Range;
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch (Exception ex)
{

rngFirstParagraph.InsertAfter(String.Format( "Error: Failed to create a database connection. \n{0}" , ex.Message));
return;
}


try
{

OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

myAccessConn.Open();
myDataAdapter.Fill(myDataSet, "Shippers");

}
catch (Exception ex)
{
rngFirstParagraph.InsertAfter(String.Format("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message));
return;
}
finally
{
myAccessConn.Close();
}

DataRowCollection dra = myDataSet.Tables["Shippers"].Rows;
foreach (DataRow dr in dra)
{
// Print the CategoryID as a subscript, then the CategoryName:
rngFirstParagraph.InsertAfter(String.Format("ShippersValue[{0}] is {1}", dr[0], dr[1]));
rngFirstParagraph.InsertParagraphAfter();
}



}

Friday, July 9, 2010

Wordking with Controls and DialogBox

VBA Code :-

Private Sub GetUserName()
With UserForm1
.lstRegions.AddItem "North"
.lstRegions.AddItem "South"
.lstRegions.AddItem "East"
.lstRegions.AddItem "West"
.txtSalesPersonID.Text = "00000"
.Show
' ...
End With
End Sub

C# Code :-

Add Form Name "UserForm1"

Create 2 public Method

public void AddItemtolIst(string value)
{
lstRegions.Items.Add(value);
}

public void AddItemToText(string strvalue)
{
txtSalesPersonID.Text = strvalue;
}

and Now acess that in ThisDocument file..

private void GetUserName()
{
UserForm1 UF = new UserForm1();

UF.AddItemtolIst("North");
UF.AddItemtolIst("South");
UF.AddItemtolIst("East");
UF.AddItemtolIst("West");

UF.AddItemToText("Avinash");
}

VBA Code :-

'Code in module to declare public variables
Public strRegion As String
Public intSalesPersonID As Integer
Public blnCancelled As Boolean

'Code in form
Private Sub cmdCancel_Click()
Module1.blnCancelled = True
Unload Me
End Sub

Private Sub cmdOK_Click()
'Save data
intSalesPersonID = txtSalesPersonID.Text
strRegion = lstRegions.List(lstRegions.ListIndex)
Module1.blnCancelled = False
Unload Me
End Sub

Sub LaunchSalesPersonForm()
frmSalesPeople.Show
If blnCancelled = True Then
MsgBox "Operation Cancelled!", vbExclamation
Else
MsgBox "The Salesperson's ID is: " & _
intSalesPersonID & _
"The Region is: " & strRegion
End If
End Sub

C# Code :-

public string strRegion;
public int intSalesPersonId = 0;
public bool blnCancelled;

private void cmdCancel_Click()
{
blnCancelled = true;
UserForm1 uf = new UserForm1();
uf.Close();
}

private void cmdOK_Click()
{
intSalesPersonId = Convert.ToInt16(txtSalesPersonID.Text);

strRegion = lstRegions.Text;
blnCancelled = false;

UserForm1 uf = new UserForm1();
uf.Close();

}

public void LaunchSalesPersonForm()
{
UserForm1 uf = new UserForm1();
uf.Show();
if (blnCancelled == true)
{
MessageBox.Show("Operation Cancelled !");
}
else
{
MessageBox.Show("The Salesperson's ID is: " + intSalesPersonId + "The Region is: " + strRegion);
}
}

VBA Code :-

Sub ShowOpenDialog()
Dialogs(wdDialogFileOpen).Show
End Sub

C# Code :-

public void ShowOpenDialog()
{

int intvalue = ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileOpen].Show(ref missing);
}

VBA Code :-

Sub ShowPrintDialog()
Dialogs(wdDialogFilePrint).Show
End Sub

C# Code : -

public void ShowPrintDialog()
{
int intvalue = ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(ref missing);
}

VBA Code :-

Sub ShowBorderDialog()
With Dialogs(wdDialogFormatBordersAndShading)
.DefaultTab = wdDialogFormatBordersAndShadingTabPageBorder
.Show
End With
End Sub

C# Code :-

public void ShowBorderDialog()
{
ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFormatBordersAndShading].DefaultTab = Word.WdWordDialogTab.wdDialogFormatBordersAndShadingTabPageBorder;
int intvalue = ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFormatBordersAndShading].Show(ref missing);

}

VBA Code :-

Sub DisplayUserInfoDialog()
With Dialogs(wdDialogToolsOptionsUserInfo)
.Display
MsgBox .Name
End With
End Sub

C# Code :-

public void DisplayUserInfoDialog()
{
string strvalue = ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogToolsOptionsUserInfo].Application.UserName ;

MessageBox.Show(strvalue);
}

VBA Coded :-

Sub DisplayUserInfo()
MsgBox Application.UserName
End Sub

C# Code :-

public void DisplayUserInfo()
{
String strvalue = ThisApplication.UserName;
}

VBA Code :-

Sub ShowAndSetUserInfoDialogBox()
With Dialogs(wdDialogToolsOptionsUserInfo)
.Display
If .Name <> "" Then .Execute
End With
End Sub

C# Code :-

public void ShowAndSetUserInfoDialogBox()
{
ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogToolsOptionsUserInfo].Display(ref missing);
ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogToolsOptionsUserInfo].Execute();

}

VBA Code :-

Sub SetUserName()
Application.UserName = "Jeff Smith"
Dialogs(wdDialogToolsOptionsUserInfo).Display
End Sub

C# Code :-

public void SetUserName()
{
ThisApplication.Application.UserName = "Jeff Smith";
ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogToolsOptionsUserInfo].Display(ref missing);
}

VBA Code :-

Sub ShowRightIndent()
Dim dlgParagraph As Dialog
Set dlgParagraph = Dialogs(wdDialogFormatParagraph)
MsgBox "Right indent = " & dlgParagraph.RightIndent
End Sub

C# Code :-

public void ShowRightIndent()
{
Word.Dialog dlgParagraph = null;
dlgParagraph = ThisApplication.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFormatParagraph];

}

VBA Code :-

Sub ShowRightIndexForSelectedParagraph()
MsgBox Selection.ParagraphFormat.RightIndent
End Sub

C# Code :-

public void ShowRightIndexForSelectedParagraph()
{
ThisApplication.Selection.ParagraphFormat.RightIndent = 0.5f;

}

VBA Code :-

Sub SetKeepWithNextForSelectedParagraph()
Selection.ParagraphFormat.KeepWithNext = True
End Sub

C# Code :-

public void SetKeepWithNextForSelectedParagraph()
{
ThisApplication.Selection.ParagraphFormat.KeepWithNext = 1;
}

Wednesday, July 7, 2010

Working With The Word Object

Know Working with The Word Object:-

VBA Code

Set Range1 = ActiveDocument.Words(1)
Set Range2 = ActiveDocument.Words(2)

C# Code

Word.Range Myrange = null;
Myrange = this.Application.ActiveDocument.Words[1];

Word.Range Myrange1 = null;
Myrange1 = this.Application.ActiveDocument.Words[2];

VBA Code

Set Range2 = Range1

C# Code

Word.Range Myrange = null;
Myrange = this.Application.ActiveDocument.Words[1];

Word.Range Myrange1 = null;

Myrange1 = Myrange;

VBA Code:-

Sub CopyWord()
Selection.Words(1).Copy
End Sub

C# Code :-

public void CopyWord()
{
Word.Selection myselection = this.Application.Selection;
myselection.Words[1].Copy();
}

VBA Code :-

Sub CopyParagraph()
ActiveDocument.Paragraphs(1).Range.Copy
End Sub

C# Code :-

public void CopyParagraph()
{
ThisApplication.Application.ActiveDocument.Paragraphs[1].Range.Copy();
}

VBA Code :-

Sub ChangeCase()
ActiveDocument.Words(1).Case = wdUpperCase
End Sub

C# Code :-

public void ChnageCase()
{
this.Application.ActiveDocument.Words[1].Case = Word.WdCharacterCase.wdUpperCase;
}

VBA Code :-

Sub ChangeSectionMargin()
Selection.Sections(1).PageSetup.BottomMargin = InchesToPoints(0.5)
End Sub

C# Code :-

public void ChangeSectionMargin()
{
this.Application.Selection.Sections[1].PageSetup.BottomMargin = this.Application.InchesToPoints(0.5f);
}

VBA Code :-

Sub DoubleSpaceDocument()
ActiveDocument.Content.ParagraphFormat.Space2
End Sub

C# Code :-

public void DoubleSpaceDocument()
{
this.Application.ActiveDocument.Content.ParagraphFormat.Space2();
}

VBA Code :-

Sub SetRangeForFirstTenCharacters()
Dim rngTenCharacters As Range
Set rngTenCharacters = ActiveDocument.Range(Start:=0, End:=10)
End Sub

C# Code :-

public void SetRangeForFirstTenCharacters()
{
Word.Range rngTenCharacters = null;

object start = 0;
object last = 10;
rngTenCharacters = this.Application.ActiveDocument.Range (ref start, ref last);
}

VBA Code :-

Sub SetRangeForFirstThreeWords()
Dim docActive As Document
Dim rngThreeWords As Range
Set docActive = ActiveDocument
Set rngThreeWords = docActive.Range(Start:=docActive.Words(1).Start, _
End:=docActive.Words(3).End)
End Sub

C# Code

public void SetRangeForFirstThreeWords()
{
Word.Document docActive = null;
Word.Range rngThreeeword = null;

docActive = this.Application.ActiveDocument;
object start = docActive.Words[1].Start;
object last = docActive.Words[1].End ;
rngThreeeword = docActive.Range(ref start, ref last);
}

VBA Code :-

ub SetParagraphRange()
Dim docActive As Document
Dim rngParagraphs As Range
Set docActive = ActiveDocument
Set rngParagraphs = docActive.Range(Start:=docActive.Paragraphs(2).Range.Start, _
End:=docActive.Paragraphs(3).Range.End)
End Sub

C# Code :-

public void SetParagraphRange()
{
Word.Document docActive = null;
Word.Range rngThreeeword = null;

docActive = this.Application.ActiveDocument;
object start = docActive.Words[1].Start;
object last = docActive.Paragraphs[1].End;
rngThreeeword = docActive.Range(ref start, ref last);
}

VBA Code :-

Sub BorderAroundFirstParagraph()
Selection.Paragraphs(1).Borders.Enable = True
End Sub

C# Code :-

public void BorderAroundFirstParagraph()
{
this.Application.Selection.Paragraphs[1].Borders.Enable = true;
}

VBA Code :-

Sub ShadeTableRow()
Selection.Tables(1).Rows(1).Shading.Texture = wdTexture10Percent
End Sub

C# Code :-

public void BorderAroundSelection()
{
this.Application.Selection.Tables[1].Rows[1].Shading.Texture = Microsoft.Office.Interop.Word.WdTextureIndex.wdTexture10Percent;
}

VBA Code :-

Sub ShadeTableRow()
If Selection.Tables.Count >= 1 Then
Selection.Tables(1).Rows(1).Shading.Texture = wdTexture25Percent
Else
MsgBox "Selection doesn't include a table"
End If
End Sub

C# Code :-

public void ShadeTableRow()
{
if (this.Application.Selection.Tables.Count >= 1)
{
this.Application.Selection.Tables[1].Rows.Shading.Texture = Microsoft.Office.Interop.Word.WdTextureIndex.wdTexture25Percent;
}
else
{
MessageBox.Show("Selection doesn't include a table");
}
}

VBA Code :-

Sub ShadeAllFirstRowsInTables()
Dim tblTable As Table
If Selection.Tables.Count >= 1 Then
For Each tblTable In Selection.Tables
tblTable.Rows(1).Shading.Texture = wdTexture30Percent
Next tblTable
End If
End Sub

C# Code :-

public void ShadeAllFirstRowsInTables()
{
Word.Table tblTable = null;

if (this.Application.Selection.Tables.Count >= 1)
{
foreach(Word.Table tblTableloop in this.Application.Selection.Tables)
{
tblTableloop.Rows[1].Shading.Texture = Microsoft.Office.Interop.Word.WdTextureIndex.wdTexture30Percent;
}
}
}

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