Menu

 

Saturday, June 26, 2010

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

No comments:

Post a Comment