Skip to content

Commit 879ddf7

Browse files
committed
word commands
1 parent a126fed commit 879ddf7

16 files changed

+1083
-0
lines changed

taskt/Core/Automation/Commands/ScriptCommand.cs

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ namespace taskt.Core.Automation.Commands
6262
[XmlInclude(typeof(ExcelGetLastRowCommand))]
6363
[XmlInclude(typeof(ExcelSaveAsCommand))]
6464
[XmlInclude(typeof(ExcelSaveCommand))]
65+
[XmlInclude(typeof(WordCreateApplicationCommand))]
66+
[XmlInclude(typeof(WordCloseApplicationCommand))]
67+
[XmlInclude(typeof(WordOpenDocumentCommand))]
68+
[XmlInclude(typeof(WordSaveCommand))]
69+
[XmlInclude(typeof(WordSaveAsCommand))]
70+
[XmlInclude(typeof(WordExportToPDFCommand))]
71+
[XmlInclude(typeof(WordAddDocumentCommand))]
72+
[XmlInclude(typeof(WordReadDocumentCommand))]
73+
[XmlInclude(typeof(WordReplaceTextCommand))]
74+
[XmlInclude(typeof(WordAppendTextCommand))]
75+
[XmlInclude(typeof(WordAppendImageCommand))]
76+
[XmlInclude(typeof(WordAppendDataTableCommand))]
6577
[XmlInclude(typeof(SeleniumBrowserCreateCommand))]
6678
[XmlInclude(typeof(SeleniumBrowserNavigateURLCommand))]
6779
[XmlInclude(typeof(SeleniumBrowserNavigateForwardCommand))]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Windows.Forms;
4+
using System.Xml.Serialization;
5+
using taskt.UI.CustomControls;
6+
using taskt.UI.Forms;
7+
8+
namespace taskt.Core.Automation.Commands
9+
{
10+
[Serializable]
11+
[Attributes.ClassAttributes.Group("Word Commands")]
12+
[Attributes.ClassAttributes.Description("This command adds a new Word Document.")]
13+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to add a new document to a Word Instance")]
14+
[Attributes.ClassAttributes.ImplementationDescription("This command implements Excel Interop to achieve automation.")]
15+
public class WordAddDocumentCommand : ScriptCommand
16+
{
17+
[XmlAttribute]
18+
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
19+
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Word** command")]
20+
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
21+
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Word** command will cause an error")]
22+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
23+
public string v_InstanceName { get; set; }
24+
25+
public WordAddDocumentCommand()
26+
{
27+
this.CommandName = "WordAddDocumentCommand";
28+
this.SelectionName = "Add Document";
29+
this.CommandEnabled = true;
30+
this.CustomRendering = true;
31+
}
32+
public override void RunCommand(object sender)
33+
{
34+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
35+
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
36+
var excelObject = engine.GetAppInstance(vInstance);
37+
38+
39+
Microsoft.Office.Interop.Word.Application excelInstance = (Microsoft.Office.Interop.Word.Application)excelObject;
40+
excelInstance.Documents.Add();
41+
42+
}
43+
public override List<Control> Render(frmCommandEditor editor)
44+
{
45+
base.Render(editor);
46+
47+
//create standard group controls
48+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InstanceName", this, editor));
49+
50+
return RenderedControls;
51+
52+
}
53+
public override string GetDisplayValue()
54+
{
55+
return base.GetDisplayValue() + " [Instance Name: '" + v_InstanceName + "']";
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Windows.Forms;
4+
using System.Xml.Serialization;
5+
using taskt.UI.CustomControls;
6+
using taskt.UI.Forms;
7+
using System.Data;
8+
using System.Linq;
9+
using System.Text.RegularExpressions;
10+
using Microsoft.Office.Interop.Word;
11+
12+
namespace taskt.Core.Automation.Commands
13+
{
14+
[Serializable]
15+
[Attributes.ClassAttributes.Group("Word Commands")]
16+
[Attributes.ClassAttributes.Description("This command appends text to a word document.")]
17+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to append text to a specific document.")]
18+
[Attributes.ClassAttributes.ImplementationDescription("This command implements Word Interop to achieve automation.")]
19+
public class WordAppendDataTableCommand : ScriptCommand
20+
{
21+
[XmlAttribute]
22+
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
23+
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Word** command")]
24+
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
25+
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Word** command will cause an error")]
26+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
27+
public string v_InstanceName { get; set; }
28+
29+
[XmlAttribute]
30+
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the DataTable Name")]
31+
[Attributes.PropertyAttributes.InputSpecification("Enter the DataTable you would like to append.")]
32+
[Attributes.PropertyAttributes.SampleUsage("**myData**")]
33+
[Attributes.PropertyAttributes.Remarks("")]
34+
public string v_DataTableName { get; set; }
35+
36+
public WordAppendDataTableCommand()
37+
{
38+
this.CommandName = "WordAppendDataTableCommand";
39+
this.SelectionName = "Append DataTable";
40+
this.CommandEnabled = true;
41+
this.CustomRendering = true;
42+
}
43+
public override void RunCommand(object sender)
44+
{
45+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
46+
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
47+
var wordObject = engine.GetAppInstance(vInstance);
48+
var dataSetVariable = LookupVariable(engine);
49+
50+
System.Data.DataTable dataTable = new System.Data.DataTable();
51+
dataTable = (System.Data.DataTable)dataSetVariable.VariableValue;
52+
53+
//selecting the word instance and open document
54+
Microsoft.Office.Interop.Word.Application wordInstance = (Microsoft.Office.Interop.Word.Application)wordObject;
55+
Document wordDocument = wordInstance.ActiveDocument;
56+
57+
58+
//converting System DataTable to Word DataTable
59+
int RowCount = dataTable.Rows.Count;
60+
int ColumnCount = dataTable.Columns.Count;
61+
Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];
62+
63+
int r = 0;
64+
for (int c = 0; c <= ColumnCount - 1; c++)
65+
{
66+
DataArray[r, c] = dataTable.Columns[c].ColumnName;
67+
for (r = 0; r <= RowCount - 1; r++)
68+
{
69+
DataArray[r, c] = dataTable.Rows[r][c];
70+
} //end row loop
71+
} //end column loop
72+
73+
object collapseEnd = WdCollapseDirection.wdCollapseEnd;
74+
dynamic docRange = wordDocument.Content;
75+
docRange.Collapse(ref collapseEnd);
76+
77+
String tempString = "";
78+
for (r = 0; r <= RowCount - 1; r++)
79+
{
80+
for (int c = 0; c <= ColumnCount - 1; c++)
81+
{
82+
tempString = tempString + DataArray[r, c] + "\t";
83+
}
84+
}
85+
86+
//appending data row text after all text/images
87+
docRange.Text = tempString;
88+
89+
//converting and formatting data table
90+
object Separator = WdTableFieldSeparator.wdSeparateByTabs;
91+
object Format = WdTableFormat.wdTableFormatGrid1;
92+
object ApplyBorders = true;
93+
object AutoFit = true;
94+
95+
object AutoFitBehavior = WdAutoFitBehavior.wdAutoFitContent;
96+
docRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount, Type.Missing, ref Format,
97+
ref ApplyBorders, Type.Missing, Type.Missing, Type.Missing,Type.Missing,
98+
Type.Missing, Type.Missing, Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);
99+
100+
docRange.Select();
101+
wordDocument.Application.Selection.Tables[1].Select();
102+
wordDocument.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
103+
wordDocument.Application.Selection.Tables[1].Rows.Alignment = 0;
104+
wordDocument.Application.Selection.Tables[1].Rows[1].Select();
105+
wordDocument.Application.Selection.InsertRowsAbove(1);
106+
wordDocument.Application.Selection.Tables[1].Rows[1].Select();
107+
108+
//Adding header row manually
109+
for (int c = 0; c <= ColumnCount - 1; c++)
110+
{
111+
wordDocument.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = dataTable.Columns[c].ColumnName;
112+
}
113+
114+
//Formatting header row
115+
wordDocument.Application.Selection.Tables[1].Rows[1].Select();
116+
wordDocument.Application.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
117+
wordDocument.Application.Selection.Font.Bold = 1;
118+
119+
}
120+
121+
private Script.ScriptVariable LookupVariable(Core.Automation.Engine.AutomationEngineInstance sendingInstance)
122+
{
123+
//search for the variable
124+
var requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == v_DataTableName).FirstOrDefault();
125+
126+
//if variable was not found but it starts with variable naming pattern
127+
if ((requiredVariable == null) && (v_DataTableName.StartsWith(sendingInstance.engineSettings.VariableStartMarker)) && (v_DataTableName.EndsWith(sendingInstance.engineSettings.VariableEndMarker)))
128+
{
129+
//reformat and attempt
130+
var reformattedVariable = v_DataTableName.Replace(sendingInstance.engineSettings.VariableStartMarker, "").Replace(sendingInstance.engineSettings.VariableEndMarker, "");
131+
requiredVariable = sendingInstance.VariableList.Where(var => var.VariableName == reformattedVariable).FirstOrDefault();
132+
}
133+
134+
return requiredVariable;
135+
}
136+
public override List<Control> Render(frmCommandEditor editor)
137+
{
138+
base.Render(editor);
139+
140+
//create standard group controls
141+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InstanceName", this, editor));
142+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_DataTableName", this, editor));
143+
144+
return RenderedControls;
145+
}
146+
public override string GetDisplayValue()
147+
{
148+
return base.GetDisplayValue() + " ['" + v_DataTableName + "' To Instance Name: '" + v_InstanceName + "']";
149+
}
150+
}
151+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Windows.Forms;
4+
using System.Xml.Serialization;
5+
using taskt.UI.CustomControls;
6+
using taskt.UI.Forms;
7+
using System.Data;
8+
using System.Linq;
9+
using System.Text.RegularExpressions;
10+
using Microsoft.Office.Interop.Word;
11+
12+
namespace taskt.Core.Automation.Commands
13+
{
14+
[Serializable]
15+
[Attributes.ClassAttributes.Group("Word Commands")]
16+
[Attributes.ClassAttributes.Description("This command appends text to a word document.")]
17+
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to append text to a specific document.")]
18+
[Attributes.ClassAttributes.ImplementationDescription("This command implements Word Interop to achieve automation.")]
19+
public class WordAppendImageCommand : ScriptCommand
20+
{
21+
[XmlAttribute]
22+
[Attributes.PropertyAttributes.PropertyDescription("Please Enter the instance name")]
23+
[Attributes.PropertyAttributes.InputSpecification("Enter the unique instance name that was specified in the **Create Word** command")]
24+
[Attributes.PropertyAttributes.SampleUsage("**myInstance** or **seleniumInstance**")]
25+
[Attributes.PropertyAttributes.Remarks("Failure to enter the correct instance name or failure to first call **Create Word** command will cause an error")]
26+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
27+
public string v_InstanceName { get; set; }
28+
29+
[XmlAttribute]
30+
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the path to the image")]
31+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
32+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
33+
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the image.")]
34+
[Attributes.PropertyAttributes.SampleUsage("C:\\temp\\myimage.png or [vTextFilePath]")]
35+
[Attributes.PropertyAttributes.Remarks("")]
36+
public string v_ImagePath { get; set; }
37+
38+
public WordAppendImageCommand()
39+
{
40+
this.CommandName = "WordAppendImageCommand";
41+
this.SelectionName = "Append Image";
42+
this.CommandEnabled = true;
43+
this.CustomRendering = true;
44+
}
45+
public override void RunCommand(object sender)
46+
{
47+
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;
48+
var vInstance = v_InstanceName.ConvertToUserVariable(engine);
49+
var vImagePath = v_ImagePath.ConvertToUserVariable(engine);
50+
var wordObject = engine.GetAppInstance(vInstance);
51+
52+
Microsoft.Office.Interop.Word.Application wordInstance = (Microsoft.Office.Interop.Word.Application)wordObject;
53+
Document wordDocument = wordInstance.ActiveDocument;
54+
55+
//Appends image after text/images
56+
object collapseEnd = WdCollapseDirection.wdCollapseEnd;
57+
Range imageRange = wordDocument.Content;
58+
imageRange.Collapse(ref collapseEnd);
59+
imageRange.InlineShapes.AddPicture(vImagePath, Type.Missing, Type.Missing, imageRange);
60+
61+
}
62+
public override List<Control> Render(frmCommandEditor editor)
63+
{
64+
base.Render(editor);
65+
66+
//create standard group controls
67+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InstanceName", this, editor));
68+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_ImagePath", this, editor));
69+
70+
return RenderedControls;
71+
}
72+
public override string GetDisplayValue()
73+
{
74+
return base.GetDisplayValue() + " ['" + v_ImagePath + "' To Instance Name: '" + v_InstanceName + "']";
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)