Skip to content

Commit c601e68

Browse files
tduguidtduguid
authored andcommitted
Copied over VB.NET procedures, still need to complete converting to C#
1 parent 6d24a45 commit c601e68

File tree

2 files changed

+286
-8
lines changed

2 files changed

+286
-8
lines changed

CS/Scripts/Ribbon.cs

Lines changed: 274 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
using System;
1+
using ADODB;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
5+
using System.Management;
46
using System.Windows.Forms;
57
using System.Linq;
68
using System.Reflection;
79
using System.Runtime.InteropServices;
810
using System.Text;
911
using Office = Microsoft.Office.Core;
12+
using Excel = Microsoft.Office.Interop.Excel;
1013

1114
namespace ServerActions.Scripts
1215
{
@@ -171,9 +174,21 @@ public string GetLabelText(Office.IRibbonControl control)
171174
/// <returns>A method that returns an integer of total count of items used for a combobox or dropdown </returns>
172175
public int GetItemCount(Office.IRibbonControl control)
173176
{
177+
Excel.ListObject tbl = null;
174178
try
175179
{
176-
return 0;
180+
181+
if (ErrorHandler.IsValidListObject())
182+
{
183+
tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject;
184+
}
185+
186+
if (tbl == null)
187+
{
188+
return 2;
189+
}
190+
191+
return tbl.ListColumns.Count + 1;
177192
}
178193
catch (Exception ex)
179194
{
@@ -190,9 +205,21 @@ public int GetItemCount(Office.IRibbonControl control)
190205
/// <returns>A method that returns a string per index of a combobox or dropdown </returns>
191206
public string GetItemLabel(Office.IRibbonControl control, int index)
192207
{
208+
Excel.ListObject tbl = null;
193209
try
194210
{
195-
return string.Empty;
211+
212+
if (ErrorHandler.IsValidListObject())
213+
{
214+
tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject;
215+
}
216+
217+
if (tbl == null || index == 0)
218+
{
219+
return String.Empty;
220+
}
221+
222+
return tbl.ListColumns[index].Name;
196223
}
197224
catch (Exception ex)
198225
{
@@ -213,10 +240,10 @@ public void OnAction(Office.IRibbonControl control)
213240
switch (control.Id)
214241
{
215242
case "btnPing":
216-
//AddPingColumn();
243+
AddPingColumn();
217244
break;
218245
case "btnCreateRdgFile":
219-
//CreateRdgFile();
246+
CreateRdgFile();
220247
break;
221248
case "btnDownloadNewVersion":
222249
//DownloadNewVersion();
@@ -231,10 +258,10 @@ public void OnAction(Office.IRibbonControl control)
231258
OpenSettings();
232259
break;
233260
case "btnRefreshCombobox":
234-
//RefreshCombobox();
261+
RefreshCombobox();
235262
break;
236263
case "btnRefreshServerList":
237-
//RefreshServerList();
264+
RefreshServerList();
238265
break;
239266
}
240267
}
@@ -289,6 +316,222 @@ public void OnChange(Office.IRibbonControl control, string text)
289316

290317
#region | Ribbon Buttons |
291318

319+
/// <summary>
320+
///
321+
/// </summary>
322+
public void AddPingColumn()
323+
{
324+
Excel.ListColumn lstCol;
325+
Excel.ListObject tbl;
326+
Excel.ListColumn col;
327+
object a;
328+
object c;
329+
int cnt;
330+
int i;
331+
string colServer;
332+
string colPing;
333+
Excel.Range cellServer;
334+
Excel.Range cellPing;
335+
try
336+
{
337+
if (ErrorHandler.IsValidListObject())
338+
{
339+
//lstCol = Ribbon.GetItem(tbl.ListColumns, colPing)
340+
//If lstCol Is Nothing Then
341+
// lstCol = tbl.ListColumns.Add
342+
// lstCol.Name = colPing
343+
//End If
344+
345+
//For Each col In tbl.ListColumns
346+
// If col.Name = colServer Then
347+
// a = col.DataBodyRange.Value2
348+
// For i = LBound(a) To UBound(a)
349+
// c = a(i, 1)
350+
// cellServer = col.DataBodyRange.Cells(1).Offset(i - 1, 0)
351+
// cellPing = lstCol.DataBodyRange.Cells(1).Offset(i - 1, 0)
352+
// If col.DataBodyRange.Rows(i).EntireRow.Hidden = False Then
353+
// cellPing.Value = Ribbon.GetPingResult(cellServer.Value)
354+
// End If
355+
// cnt = cnt + 1
356+
// Next
357+
// End If
358+
//Next
359+
}
360+
361+
}
362+
catch (Exception ex)
363+
{
364+
ErrorHandler.DisplayMessage(ex);
365+
}
366+
}
367+
368+
/// <summary>
369+
///
370+
/// </summary>
371+
public void CreateRdgFile()
372+
{
373+
Excel.ListColumn lstCol;
374+
Excel.ListObject tbl;
375+
Excel.ListColumn col;
376+
Excel.Range cellServer;
377+
Excel.Range cellDesc;
378+
string colServer = Properties.Settings.Default.Rdg_ServerName;
379+
string colDesc = Properties.Settings.Default.Rdg_Description;
380+
string FileName = Properties.Settings.Default.Rdg_FileName;
381+
string script = string.Empty;
382+
string vbCrLf = string.Empty;
383+
string Q = ((char)34).ToString();
384+
object a;
385+
object c;
386+
int cnt;
387+
int i;
388+
try
389+
{
390+
if (ErrorHandler.IsValidListObject())
391+
{
392+
script = "<?xml version=" + Q + "1.0" + Q + " encoding=" + Q + "UTF-8" + Q + "?>";
393+
script += vbCrLf + "<RDCMan programVersion=" + Q + "2.7" + Q + " schemaVersion=" + Q + "3" + Q + ">";
394+
script += vbCrLf + "<file>";
395+
script += vbCrLf + "<credentialsProfiles />";
396+
script += vbCrLf + "<properties>";
397+
script += vbCrLf + "<expanded>True</expanded>";
398+
script += vbCrLf + "<name>" + Scripts.AssemblyInfo.Title + "</name>";
399+
script += vbCrLf + "</properties>";
400+
401+
//lstCol = Ribbon.GetItem(tbl.ListColumns, colDesc)
402+
403+
//For Each col In tbl.ListColumns
404+
// If col.Name = colServer Then
405+
// a = col.DataBodyRange.Value2
406+
// For i = LBound(a) To UBound(a)
407+
// c = a(i, 1)
408+
// cellServer = col.DataBodyRange.Cells(1).Offset(i - 1, 0)
409+
// cellDesc = lstCol.DataBodyRange.Cells(1).Offset(i - 1, 0)
410+
// If col.DataBodyRange.Rows(i).EntireRow.Hidden = False Then
411+
// script += vbCrLf + "<server>"
412+
// script += vbCrLf + "<properties>"
413+
// script += vbCrLf + "<displayName>" & cellServer.Value & " (" & cellDesc.Value & ")</displayName>"
414+
// script += vbCrLf + "<name>" & cellServer.Value & "</name>"
415+
// script += vbCrLf + "</properties>"
416+
// script += vbCrLf + "</server>"
417+
// End If
418+
// cnt = cnt + 1
419+
// Next
420+
// End If
421+
//Next
422+
script += vbCrLf + "</file>";
423+
script += vbCrLf + "<connected />";
424+
script += vbCrLf + "<favorites />";
425+
script += vbCrLf + "<recentlyUsed />";
426+
script += vbCrLf + "</RDCMan>";
427+
428+
System.IO.File.WriteAllText(FileName, script);
429+
}
430+
431+
}
432+
catch (Exception ex)
433+
{
434+
ErrorHandler.DisplayMessage(ex);
435+
}
436+
}
437+
438+
/// <summary>
439+
///
440+
/// </summary>
441+
public void RefreshServerList()
442+
{
443+
ADODB.Connection cn = new ADODB.Connection();
444+
ADODB.Recordset rs = new ADODB.Recordset();
445+
ADODB.Command cmd = new ADODB.Command();
446+
Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook;
447+
Excel.Worksheet ws;
448+
Excel.ListObject tbl;
449+
int iCols = 0;
450+
string msg = String.Empty;
451+
string ldapQry = Properties.Settings.Default.Rdg_LdapQry;
452+
try
453+
{
454+
cn.Open("Provider=ADsDSOObject;");
455+
ldapQry = ldapQry.Replace("[Rdg.LdapPath]", Properties.Settings.Default.Rdg_LdapPath);
456+
cmd.CommandText = ldapQry;
457+
cmd.ActiveConnection = cn;
458+
object objRecAff = null;
459+
object objParameters = null;
460+
rs = cmd.Execute(out objRecAff, ref objParameters, (int)ADODB.CommandTypeEnum.adCmdText);
461+
462+
bool sheetExists;
463+
//For Each ws In wb.Sheets
464+
// If My.Settings.Rdg_SheetName = ws.Name Then
465+
// sheetExists = True
466+
// ws.Activate()
467+
// End If
468+
//Next ws
469+
470+
//If sheetExists = False Then
471+
// ws = wb.ActiveSheet
472+
// Dim answer As Integer
473+
// msg = "The sheet named '" & My.Settings.Rdg_SheetName & "' does not exist."
474+
// msg = msg & vbCrLf & "Would you like to use the current sheet?"
475+
// answer = MsgBox(msg, vbYesNo + vbQuestion, "Sheet Not Found")
476+
// 'MessageBox.Show(msg, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
477+
// If answer = vbYes Then
478+
// ws = wb.ActiveSheet
479+
// My.Settings.Rdg_SheetName = wb.ActiveSheet.Name
480+
// Else
481+
// Exit Try
482+
// End If
483+
//Else
484+
// ws = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets(My.Settings.Rdg_SheetName)
485+
//End If
486+
487+
//Globals.ThisAddIn.Application.Sheets(My.Settings.Rdg_SheetName).Activate
488+
//Ribbon.ClearSheetContents()
489+
//For iCols = 0 To rs.Fields.Count - 1
490+
// ws.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
491+
//Next
492+
//ws.Range(ws.Cells(1, 1), ws.Cells(1, rs.Fields.Count)).Font.Bold = True
493+
//ws.Range("A2").CopyFromRecordset(rs)
494+
495+
//Ribbon.CreateTableFromRange()
496+
//Ribbon.UpdateBlankCells()
497+
//Ribbon.FormatDateColumns()
498+
499+
//'create server type column from the first 2 characters of the server name
500+
//'If My.Settings.Rdg_ServerGroup = "ServerType" Then
501+
//' tbl.ListColumns.Add(3).Name = My.Settings.Rdg_ServerGroup
502+
//' tbl.ListColumns(My.Settings.Rdg_ServerGroup).DataBodyRange.FormulaR1C1 = "=UPPER(IFERROR(IF(SEARCH(""-"", [@Name]) > 0, LEFT([@Name], 2), """"), ""(NONE)""))"
503+
//' Globals.ThisAddIn.Application.Columns.AutoFit()
504+
//'End If
505+
506+
//Ribbon.InvalidateRibbon() 'reset dropdown lists
507+
//Ribbon.ActivateTab()
508+
509+
}
510+
catch (Exception ex)
511+
{
512+
ErrorHandler.DisplayMessage(ex);
513+
}
514+
}
515+
516+
/// <summary>
517+
///
518+
/// </summary>
519+
public void RefreshCombobox()
520+
{
521+
try
522+
{
523+
if (ErrorHandler.IsValidListObject())
524+
{
525+
ribbon.Invalidate();
526+
}
527+
528+
}
529+
catch (Exception ex)
530+
{
531+
ErrorHandler.DisplayMessage(ex);
532+
}
533+
}
534+
292535
/// <summary>
293536
/// Opens the settings taskpane
294537
/// </summary>
@@ -347,6 +590,30 @@ public void OpenNewIssue()
347590

348591
}
349592

593+
/// <summary>
594+
///
595+
/// </summary>
596+
public void ShowServerStatus()
597+
{
598+
string FullComputerName = "<Name of Remote Computer>";
599+
ConnectionOptions options = new ConnectionOptions();
600+
ManagementScope scope = new ManagementScope("\\\\" + FullComputerName + "\\root\\cimv2", options);
601+
scope.Connect();
602+
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_TerminalService");
603+
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
604+
ManagementObjectCollection queryCollection = searcher.Get();
605+
foreach (ManagementObject queryObj in queryCollection)
606+
{
607+
Console.WriteLine("-----------------------------------");
608+
Console.WriteLine("Win32_TerminalService instance");
609+
Console.WriteLine("-----------------------------------");
610+
Console.WriteLine("Started: {0}", queryObj["Started"]);
611+
Console.WriteLine("State: {0}", queryObj["State"]);
612+
Console.WriteLine("Status: {0}", queryObj["Status"]);
613+
}
614+
615+
}
616+
350617
#endregion
351618

352619
}

CS/ServerActions.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<Reference Include="System.Data" />
138138
<Reference Include="System.Deployment" />
139139
<Reference Include="System.Drawing" />
140+
<Reference Include="System.Management" />
140141
<Reference Include="System.Windows.Forms" />
141142
<Reference Include="System.Xml" />
142143
<Reference Include="System.Core" />
@@ -228,7 +229,17 @@
228229
</Compile>
229230
<AppDesigner Include="Properties\" />
230231
</ItemGroup>
231-
<ItemGroup />
232+
<ItemGroup>
233+
<COMReference Include="ADODB">
234+
<Guid>{B691E011-1797-432E-907A-4D8C69339129}</Guid>
235+
<VersionMajor>6</VersionMajor>
236+
<VersionMinor>1</VersionMinor>
237+
<Lcid>0</Lcid>
238+
<WrapperTool>tlbimp</WrapperTool>
239+
<Isolated>False</Isolated>
240+
<EmbedInteropTypes>True</EmbedInteropTypes>
241+
</COMReference>
242+
</ItemGroup>
232243
<ItemGroup>
233244
<EmbeddedResource Include="Ribbon.xml" />
234245
</ItemGroup>

0 commit comments

Comments
 (0)