Menu

 

Wednesday, July 28, 2010

Refernce-Collection :- (D) Diagram Nodes,Dialog ,Dictionaries,Document :- Collection

VBA Code :-

Sub FillDiagramNodes()
ActiveDocument.Shapes(1).Diagram.Nodes.SelectAll
Selection.ShapeRange.Fill.Patterned msoPatternSmallConfetti
End Sub

C# Code :-

public void FillDiagramNodes()
{
object objitemid = 1;
ThisApplication.ActiveDocument.Shapes.get_Item(ref objitemid ).Diagram.Nodes.SelectAll();
ThisApplication.Selection.ShapeRange.Fill.Patterned(Microsoft.Office.Core.MsoPatternType.msoPatternSmallConfetti);
}

VBA Code :-

Sub FillDiagramNode()
ActiveDocument.Shapes(1).Diagram.Nodes.Item(1).Delete
End Sub

C# Code :-

public void FillDiagramNode()
{
object objitemid = 1;
ThisApplication.ActiveDocument.Shapes.get_Item(ref objitemid).Diagram.Nodes.Item(objitemid).Delete();
}

VBA Code :-

MsgBox Dialogs.Count

C# Code :-

public void DialogsCount()
{
string strDilogCount = System.Convert.ToString(this.Application.Dialogs.Count);
MessageBox.Show(strDilogCount);
}

VBA Code :-

dlgAnswer = Dialogs(wdDialogFileOpen).Show

C# Code :-

this.Application.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFileOpen].Show(ref missing);

VBA Code :-

For Each d In CustomDictionaries
Msgbox d.Name
Next d

C# Code :-

public void CustomDictionaries()
{
int i = 0;
Word.Dictionaries wd = this.Application.CustomDictionaries;
foreach(Word.Dictionaries d in wd)
{
i++;
object idName = i;
MessageBox.Show(d.get_Item(ref idName).Name);

}
}

VBA Code :-

CustomDictionaries.Add FileName:="MyCustom.dic"

C# Code :-

public void CustomDictAdd()
{
this.Application.CustomDictionaries.Add("MyCDustom.dic");
}

VBA Code :-

With CustomDictionaries
.ClearAll
.Add FileName:= "MyCustom.dic"
.ActiveCustomDictionary = CustomDictionaries(1)
End With

C# Code :-

public void CustomDict()
{
this.Application.CustomDictionaries.ClearAll();
this.Application.CustomDictionaries.Add("MyCUSTOM.dic");
}

VBA Code :-

For Each aDoc In Documents
aName = aName & aDoc.Name & vbCr
Next aDoc
MsgBox aName

C# Code :-


public void aDocument()
{
foreach (Word.Document aDoc in this.Application.Documents)
{
string strName = aDoc.Name;
MessageBox.Show(strName);
}
}

VBA Code :-

Documents.Add

C# Code :-

public void DocumentAdd()
{
object objDoc = "C:\\mydoc.dotx";
ThisApplication.Documents.Add(ref objDoc, ref missing, ref missing, ref missing);
}

VBA Code :-

Documents.Open FileName:="C:\My Documents\Sales.doc"

C# Code :-

public void DocumentOpen()
{
object objDoc = "C:\\mydoc.dotx";
ThisApplication.Documents.Open(ref objDoc, 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 :-

Documents(1).Activate

C# Code :-

public void DocuActive()
{
object idDoc = 1;
ThisApplication.Documents.get_Item(ref idDoc).Activate();
}

VBA Code :-

For Each doc In Documents
If doc.Name = "Report.doc" Then found = True
Next doc
If found <> True Then
Documents.Open FileName:="C:\Documents\Report.doc"
Else
Documents("Report.doc").Activate
End If

C# Code :-

public void DocumentLoop()
{
int i=0;
foreach (Word.Documents doc in ThisApplication.Documents)
{ i++;
object docid =i;
string strName = doc.get_Item(ref docid).Name;
if (strName == "Report.doc")
{
object objDoc = "C:\\mydoc.dotx";

ThisApplication.Documents.Open(ref objDoc, 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);
}
else
{
ThisApplication.Documents.get_Item(ref docid).Activate();
}
}
}

Sunday, July 25, 2010

Reference-Collection:- (C) :- CanvasShapes Collection

VBA Code :-

Sub AddCanvasShapes()
Dim shpCanvas As Shape
Dim shpCanvasShapes As CanvasShapes
Dim shpCnvItem As Shape

'Adds a new canvas to the document
Set shpCanvas = ActiveDocument.Shapes _
.AddCanvas(Left:=100, Top:=75, _
Width:=50, Height:=75)
Set shpCanvasShapes = shpCanvas.CanvasItems

'Adds shapes to the CanvasShapes collection
With shpCanvasShapes
.AddShape Type:=msoShapeRectangle, _
Left:=0, Top:=0, Width:=50, Height:=50
.AddShape Type:=msoShapeOval, _
Left:=5, Top:=5, Width:=40, Height:=40
.AddShape Type:=msoShapeIsoscelesTriangle, _
Left:=0, Top:=25, Width:=50, Height:=50
End With
End Sub

C# Code :-

public void AddCanvasShapes()
{
Word.Shape shpCanvas = null;
Word.CanvasShapes shpCanvasShapes = null;
Word.Shape shpCnvItem = null;

shpCanvas = ThisApplication.ActiveDocument.Shapes.AddCanvas(100f, 75f, 50, 75f, ref missing);
shpCanvasShapes = shpCanvas.CanvasItems;
shpCanvasShapes.AddShape(1, 0f, 0f, 50f, 50f);
shpCanvasShapes.AddShape(2, 0f, 0f, 40f, 40f);
shpCanvasShapes.AddShape(2, 0f, 25f, 50f, 50f);

}

VBA Code :-

Sub CanvasShapeThree()
With ActiveDocument.Shapes(1).CanvasItems(3)
.Line.ForeColor.RGB = RGB(50, 0, 255)
.Fill.ForeColor.RGB = RGB(50, 0, 255)
.Flip msoFlipVertical
End With
End Sub

C#

public void CanvasShapeThree()
{
object shapesid = "1";
object CanvasItem = "3";
Microsoft.Office.Interop.Word.ColorFormat ColorRGB ;

ThisApplication.ActiveDocument.Shapes.get_Item(ref shapesid).CanvasItems.get_Item(ref CanvasItem).Line.ForeColor.RGB = 1;
ThisApplication.ActiveDocument.Shapes.get_Item(ref shapesid).CanvasItems.get_Item(ref CanvasItem).Flip(Microsoft.Office.Core.MsoFlipCmd.msoFlipVertical);
}

VBA Code :-

CaptionLabels.Add Name:="Photo"

C# Code :-

public void CaptionLabel()
{
ThisApplication.CaptionLabels.Add("Photo");

}

VBA Code :-

CaptionLabels("Figure").NumberStyle = _
wdCaptionNumberStyleLowercaseLetter

C# Code :-

public void CaptionLabes()
{
object objindex = 1;
ThisApplication.CaptionLabels.Add("Photo");
ThisApplication.CaptionLabels.get_Item(ref objindex).NumberStyle = Microsoft.Office.Interop.Word.WdCaptionNumberStyle.wdCaptionNumberStyleHindiLetter1;
}

VBA Code :-

ActiveDocument.Tables(1).Rows(1).Cells.Width = 30

C# Code :-

public void TablesCell()
{
ThisApplication.ActiveDocument.Tables[1].Rows[1].Cells.Width = 30;
}

VBA Code :-

num = Selection.Rows(1).Cells.Count

C# Code :-

public void SelRows()
{
int num = 0;
num = ThisApplication.Selection.Rows[1].Cells.Count;
}

VBA Code :-

Set myTable = ActiveDocument.Tables(1)
myTable.Range.Cells.Add BeforeCell:=myTable.Cell(1, 1)

C# Code:-

public void myTable()
{
object beforecell = "1,2";

Word.Table myTable = ThisApplication.ActiveDocument.Tables[1];
myTable.Range.Cells.Add(ref beforecell);
}

VBA Code :-

Set myCell = ActiveDocument.Tables(1).Cell(Row:=1, Column:=2)
myCell.Shading.Texture = wdTexture20Percent

C# Code :-

public void MyCell()
{
int Row = 1;
int Column = 2;

Word.Table myTable = ThisApplication.ActiveDocument.Tables[1];
myTable.Cell( Row, Column);

myTable.Shading.Texture = Microsoft.Office.Interop.Word.WdTextureIndex.wdTexture20Percent;

}

VBA Code :-

Set myTable = ActiveDocument.Tables(1)
Set aColumn = myTable.Columns.Add(BeforeColumn:=myTable.Columns(1))
For Each aCell In aColumn.Cells
aCell.Range.Delete
aCell.Range.InsertAfter num + 1
num = num + 1
Next aCell

C# Code :-

public void DeeleteColumCell()
{
Word.Table myTable = ThisApplication.ActiveDocument.Tables[1];
object col = "1";
object beforeColumn = myTable.Columns.Add(ref col);
Word.Column aColumn = (Word.Column)myTable.Columns.Add(ref beforeColumn);

int num =0;
foreach (Word.Cell aCell in aColumn.Cells)
{num ++;
aCell.Range.Delete(ref missing, ref missing);
aCell.Range.InsertAfter("" + num + 1 + "");
}

}

VBA Code :-

With ActiveDocument
.Paragraphs(1).Range.InsertParagraphAfter
.Paragraphs(2).Range.InsertBefore "New Text"
End With

C# Code :-

public void Charatcertest()
{
ThisApplication.ActiveDocument.Paragraphs[1].Range.InsertAfter("test1");
ThisApplication.ActiveDocument.Paragraphs[2].Range.InsertAfter("New Text");
}

VBA Code :-

MsgBox ActiveDocument.Tables(1).Columns.Count

C# Code :-

MessageBox.Show((string)ThisApplication.ActiveDocument.Tables[1].Columns.Count);

VBA Code :-

Set myTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=3, NumColumns:=6)
For Each col In myTable.Columns
col.Shading.Texture = 2 + i
i = i + 1
Next col

C# Code :-

public void ActiveTablesAdd()
{
Word.Range wordRange = ThisApplication.Selection.Range;
Word.Table myTable = ThisApplication.ActiveDocument.Tables.Add(wordRange, 3, 6, ref missing, ref missing);


foreach (Word.Column col in myTable.Columns)
{

col.Shading.Texture = Microsoft.Office.Interop.Word.WdTextureIndex.wdTexture15Percent ;

}
}

VBA Code :-

If ActiveDocument.Tables.Count >= 1 Then
Set myTable = ActiveDocument.Tables(1)
myTable.Columns.Add BeforeColumn:=myTable.Columns(1)
myTable.Columns.DistributeWidth
End If

C# Code :-

public void ActiveDocumentTablesCount()
{
int tablecount = ThisApplication.ActiveDocument.Tables.Count;

if (tablecount >= 1)
{
Word.Table myTable = ThisApplication.ActiveDocument.Tables[1];
object BeforeColumn = myTable.Columns[1];
myTable.Columns.Add(ref BeforeColumn);
myTable.Columns.DistributeWidth();
}
}

VBA Code :-

ActiveDocument.Tables(1).Columns(1).Select

C# Code :-

public void ColSelect()
{
ThisApplication.ActiveDocument.Tables[1].Columns[1].Select();
}

VBA Code :-

ActiveDocument.ActiveWindow.View.SplitSpecial = wdPaneComments
ActiveDocument.Comments.ShowBy = "Don Funk"

C# Code :-

public void SplitView()
{
ThisApplication.ActiveWindow.View.SplitSpecial = Microsoft.Office.Interop.Word.WdSpecialPane.wdPaneComments;
ThisApplication.ActiveDocument.Comments.ShowBy = "Avinash Tiwari";
}

VBA Code :-

Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.Comments.Add Range:=Selection.Range, _
Text:="review this"

C# Code :-

public void Collpasetest()
{
object Director = Word.WdCollapseDirection.wdCollapseEnd;

ThisApplication.Selection.Collapse(ref Director);
ThisApplication.Selection.Text = "Review This";
}

VBA Code :-

MsgBox ActiveDocument.Comments(1).Author

C# Code :-

public void ActiveComments()
{
string strMessage = ThisApplication.ActiveDocument.Comments[1].Author;
MessageBox.Show(strMessage);
}

VBA Code :-

MsgBox Application.MailingLabel.CustomLabels.Count

C# Code :-

public void MailingLable()
{
int strMessage = ThisApplication.MailingLabel.CustomLabels.Count;
MessageBox.Show(strMessage.ToString());
}

VBA Code :-

Set ML = _
Application.MailingLabel.CustomLabels.Add(Name:="My Labels", _
DotMatrix:=False)
ML.PageSize = wdCustomLabelA4

C# Code :-

public void MailingLabel()
{
object DotMatrix = false;
string strName = "My label";
Word.MailingLabel mailLable = ThisApplication.MailingLabel;
mailLable.CustomLabels.Add(strName, ref DotMatrix);

}

VBA Code :-

If Application.MailingLabel.CustomLabels.Count >= 1 Then
MsgBox Application.MailingLabel.CustomLabels(1).Name
End If

C# Code :-

public void AppMailingLablel()
{
if (ThisApplication.MailingLabel.CustomLabels.Count >= 1)
{object indexvalue = 1;
string strCustomLabels = ThisApplication.MailingLabel.CustomLabels.get_Item(ref indexvalue).Name;
}
}

VBA Code :-

Sub AddProps()
With ThisDocument.SmartTags(1)
.Properties.Add Name:="President", Value:=True
MsgBox "The XML code is " & .XML
End With
End Sub

C# Code :-

public void AddProps()
{
object inderxId =1;
ThisDocument mydoc = new ThisDocument();
mydoc.SmartTags.get_Item(ref inderxId).Properties.Add("Avinash", "true");

}

VBA Code :-

Sub ReturnProps()
With ThisDocument.SmartTags(1).Properties(1)
MsgBox "The Smart Tag name is: " & .Name & vbLf & .Value
End With
End Sub

C# Code :-


public void ReturnProps()
{
object inderxId =1;
ThisDocument mydoc = new ThisDocument();
string name = "";
name = mydoc.SmartTags.get_Item(ref inderxId).Properties.get_Item(ref inderxId).Name;
string value = "";
value = mydoc.SmartTags.get_Item(ref inderxId).Properties.get_Item(ref inderxId).Value;
}

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