Menu

 

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

No comments:

Post a Comment