Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3ea946

Browse files
tduguidtduguid
tduguid
authored and
tduguid
committed
Updated procedures in VB.NET version to match C#
1 parent 3d49b08 commit e3ea946

File tree

9 files changed

+272
-334
lines changed

9 files changed

+272
-334
lines changed

‎CS/Scripts/ErrorHandler.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,27 @@ public static bool IsValidListObject(bool showMsg = false)
222222
}
223223
}
224224

225+
/// <summary>
226+
/// Check to see if there is a column in the table
227+
/// </summary>
228+
/// <param name="tbl"></param>
229+
/// <param name="columnName"></param>
230+
/// <returns></returns>
231+
public static bool IsValidListColumn(Excel.ListObject tbl, string columnName)
232+
{
233+
try
234+
{
235+
Excel.ListColumn col = tbl.ListColumns[columnName];
236+
return true;
237+
238+
}
239+
catch (Exception)
240+
{
241+
return false;
242+
}
243+
244+
}
245+
225246
/// <summary>
226247
/// This method check whether Excel is in Cell Editing mode or not
227248
/// There are few ways to check this (eg. check to see if a standard menu item is disabled etc.)

‎CS/Scripts/Ribbon.cs‎

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,33 @@ public void AddPingColumn()
366366
{
367367
try
368368
{
369+
Excel.ListColumn colResults;
370+
369371
if (ErrorHandler.IsValidListObject())
370372
{
371373
Excel.ListObject tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject;
372-
Excel.ListColumn colResults = tbl.ListColumns[tbl.Range.Columns.Count];
373-
if (colResults.Name != Properties.Settings.Default.Ping_Results)
374+
375+
if (ErrorHandler.IsValidListColumn(tbl, Properties.Settings.Default.Ping_Results))
376+
{
377+
colResults = tbl.ListColumns[Properties.Settings.Default.Ping_Results];
378+
}
379+
else
374380
{
375381
colResults = tbl.ListColumns.Add();
376382
colResults.Name = Properties.Settings.Default.Ping_Results;
377383
}
378-
379-
Excel.ListColumn colServer = tbl.ListColumns[Properties.Settings.Default.Ping_ServerName];
380-
381-
for (int i = 1; i <= tbl.ListRows.Count; i++)
384+
385+
if (ErrorHandler.IsValidListColumn(tbl, Properties.Settings.Default.Ping_ServerName))
382386
{
383-
if (colServer.DataBodyRange.Rows[i].EntireRow.Hidden == false)
387+
Excel.ListColumn colServer = tbl.ListColumns[Properties.Settings.Default.Ping_ServerName];
388+
for (int r = 1; r <= tbl.ListRows.Count; r++)
384389
{
385-
Excel.Range cellResult = colResults.DataBodyRange.Cells[1].Offset(i - 1, 0);
386-
Excel.Range cellServer = colServer.DataBodyRange.Cells[1].Offset(i - 1, 0);
387-
cellResult.Value = Ribbon.GetPingResult(cellServer.Value);
390+
if (colServer.DataBodyRange.Rows[r].EntireRow.Hidden == false)
391+
{
392+
Excel.Range cellResult = colResults.DataBodyRange.Cells[1].Offset(r - 1, 0);
393+
Excel.Range cellServer = colServer.DataBodyRange.Cells[1].Offset(r - 1, 0);
394+
cellResult.Value = Ribbon.GetPingResult(cellServer.Value.ToString());
395+
}
388396
}
389397
}
390398

@@ -399,48 +407,72 @@ public void AddPingColumn()
399407

400408
public void CreateRdgFile()
401409
{
402-
string vbCrLf = string.Empty;
403-
string Q = ((char)34).ToString();
404-
405410
try
406411
{
412+
string quote = ((char)34).ToString();
413+
string previousGroup = string.Empty;
414+
string currentGroup = string.Empty;
407415
if (ErrorHandler.IsValidListObject())
408416
{
409-
string script = "<?xml version=" + Q + "1.0" + Q + " encoding=" + Q + "UTF-8" + Q + "?>";
410-
script += vbCrLf + "<RDCMan programVersion=" + Q + "2.7" + Q + " schemaVersion=" + Q + "3" + Q + ">";
411-
script += vbCrLf + "<file>";
412-
script += vbCrLf + "<credentialsProfiles />";
413-
script += vbCrLf + "<properties>";
414-
script += vbCrLf + "<expanded>True</expanded>";
415-
script += vbCrLf + "<name>" + Scripts.AssemblyInfo.Title + "</name>";
416-
script += vbCrLf + "</properties>";
417+
var script = new StringBuilder()
418+
.AppendLine("<?xml version=" + quote + "1.0" + quote + " encoding=" + quote + "UTF-8" + quote + "?>")
419+
.AppendLine("<RDCMan programVersion=" + quote + "2.7" + quote + " schemaVersion=" + quote + "3" + quote + ">")
420+
.AppendLine("<file>")
421+
.AppendLine("<credentialsProfiles />")
422+
.AppendLine("<properties>")
423+
.AppendLine("<expanded>True</expanded>")
424+
.AppendLine("<name>" + Scripts.AssemblyInfo.Title + "</name>")
425+
.AppendLine("</properties>");
417426

418427
Excel.ListObject tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject;
419-
Excel.ListColumn colDesc = tbl.ListColumns[Properties.Settings.Default.Rdg_Description];
420428
Excel.ListColumn colServer = tbl.ListColumns[Properties.Settings.Default.Rdg_ServerName];
429+
Excel.ListColumn colDesc = tbl.ListColumns[Properties.Settings.Default.Rdg_Description];
430+
Excel.ListColumn colComment = tbl.ListColumns[Properties.Settings.Default.Rdg_Comment];
431+
Excel.ListColumn colServerGroup = tbl.ListColumns[Properties.Settings.Default.Rdg_ServerGroup];
421432

422-
for (int i = 1; i <= tbl.ListRows.Count; i++)
433+
for (int r = 1; r <= tbl.ListRows.Count; r++)
423434
{
424-
if (colServer.DataBodyRange.Rows[i].EntireRow.Hidden == false)
435+
if (colServer.DataBodyRange.Rows[r].EntireRow.Hidden == false)
425436
{
426-
Excel.Range cellDesc = colDesc.DataBodyRange.Cells[1].Offset(i - 1, 0);
427-
Excel.Range cellServer = colServer.DataBodyRange.Cells[1].Offset(i - 1, 0);
428-
script += vbCrLf + "<server>";
429-
script += vbCrLf + "<properties>";
430-
script += vbCrLf + "<displayName>" + cellServer.Value + " (" + cellDesc.Value + ")</displayName>";
431-
script += vbCrLf + "<name>" + cellServer.Value + "</name>";
432-
script += vbCrLf + "</properties>";
433-
script += vbCrLf + "</server>";
437+
Excel.Range cellServer = colServer.DataBodyRange.Cells[1].Offset(r - 1, 0);
438+
Excel.Range cellDesc = colDesc.DataBodyRange.Cells[1].Offset(r - 1, 0);
439+
Excel.Range cellComment = colComment.DataBodyRange.Cells[1].Offset(r - 1, 0);
440+
Excel.Range cellServerGroup = colServerGroup.DataBodyRange.Cells[1].Offset(r - 1, 0);
441+
currentGroup = cellServerGroup.Value;
442+
443+
if (currentGroup != previousGroup)
444+
{
445+
script.AppendLine("<group>");
446+
script.AppendLine("<properties>");
447+
script.AppendLine("<expanded>True</expanded>");
448+
script.AppendLine("<name>" + currentGroup + "</name>");
449+
script.AppendLine("</properties>");
450+
}
451+
452+
script.AppendLine("<server>");
453+
script.AppendLine("<properties>");
454+
script.AppendLine("<name>" + cellServer.Value + "</name>");
455+
script.AppendLine("<displayName>" + cellServer.Value + " (" + cellDesc.Value + ")</displayName>");
456+
script.AppendLine("<comment>" & cellComment.Value & "</comment>");
457+
script.AppendLine("</properties>");
458+
script.AppendLine("</server>");
459+
460+
if (currentGroup != colServerGroup.DataBodyRange.Cells[1].Offset(r, 0))
461+
{
462+
script.AppendLine("</group>");
463+
}
464+
434465
}
466+
previousGroup = currentGroup;
435467
}
436468

437-
script+=vbCrLf+"</file>";
438-
script+=vbCrLf+"<connected />";
439-
script+=vbCrLf+"<favorites />";
440-
script+=vbCrLf+"<recentlyUsed />";
441-
script+=vbCrLf+"</RDCMan>";
469+
script.AppendLine("</file>");
470+
script.AppendLine("<connected />");
471+
script.AppendLine("<favorites />");
472+
script.AppendLine("<recentlyUsed />");
473+
script.AppendLine("</RDCMan>");
442474

443-
System.IO.File.WriteAllText(Properties.Settings.Default.Rdg_FileName, script);
475+
System.IO.File.WriteAllText(Properties.Settings.Default.Rdg_FileName, script.ToString());
444476

445477
}
446478
}
@@ -480,16 +512,16 @@ public void RefreshServerList()
480512
// string msg = "The sheet named '" & My.Settings.Rdg_SheetName & "' does not exist."
481513
// msg = msg & vbCrLf & "Would you like to use the current sheet?"
482514
// answer = MsgBox(msg, vbYesNo + vbQuestion, "Sheet Not Found")
483-
// 'MessageBox.Show(msg, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
515+
// 'MessageBox.Show(msg, "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
484516
// If answer = vbYes Then
485517
// ws = wb.ActiveSheet
486518
// My.Settings.Rdg_SheetName = wb.ActiveSheet.Name
487519
// Else
488520
// Exit Try
489521
// End If
490522
//Else
491-
//ws = Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[Properties.Settings.Default.Rdg_SheetName];
492-
ws = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
523+
//ws = wb.Worksheets[Properties.Settings.Default.Rdg_SheetName];
524+
ws = wb.ActiveSheet;
493525
//End If
494526

495527
ws.Activate();
@@ -502,7 +534,8 @@ public void RefreshServerList()
502534
ws.Range["A2"].CopyFromRecordset(rs);
503535

504536
Ribbon.FormatTableFromRange();
505-
Ribbon.UpdateZeroStringToNull();
537+
Excel.ListObject tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject;
538+
Ribbon.UpdateZeroStringToNull(tbl);
506539
Ribbon.FormatDateColumns();
507540

508541
//'create server type column from the first 2 characters of the server name
@@ -529,7 +562,6 @@ public void RefreshCombobox()
529562
if (ErrorHandler.IsValidListObject())
530563
{
531564
ribbon.Invalidate();
532-
ribbon.InvalidateControl("ID1");
533565
}
534566

535567
}
@@ -853,31 +885,28 @@ public static Excel.Range FirstNotNullCellInColumn(Excel.Range rng)
853885
/// Change zero string cell values to string "NULL"
854886
/// </summary>
855887
/// <remarks></remarks>
856-
public static void UpdateZeroStringToNull()
888+
public static void UpdateZeroStringToNull(Excel.ListObjecttbl)
857889
{
858-
Excel.ListObject tbl = null;
859-
Excel.Range cell = null;
860-
Excel.Range usedRange = null;
861890
try
862891
{
863892
if (ErrorHandler.IsAvailable(true) == false)
864893
{
865894
return;
866895
}
867-
ErrorHandler.CreateLogRecord();
868-
tbl = Globals.ThisAddIn.Application.ActiveCell.ListObject;
869-
cell = default(Excel.Range);
896+
870897
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
871-
usedRange = tbl.Range;
872-
int n = tbl.ListColumns.Count;
873-
int m = tbl.ListRows.Count;
874-
for (int i = 0; i <= m; i++) // by row
898+
ErrorHandler.CreateLogRecord();
899+
900+
Excel.Range cell = default(Excel.Range);
901+
Excel.Range usedRange = tbl.Range;
902+
903+
for (int r = 0; r <= tbl.ListRows.Count; r++)
875904
{
876-
for (int j = 1; j <= n;j++)// by column
905+
for (int c = 1; c <= tbl.ListColumns.Count;c++)
877906
{
878-
if (usedRange[i + 1, j].Value2 == null)
907+
if (usedRange[r + 1, c].Value2 == null)
879908
{
880-
cell = tbl.Range.Cells[i + 1, j];
909+
cell = tbl.Range.Cells[r + 1, c];
881910
cell.Value = "NULL";
882911
}
883912
}
@@ -887,16 +916,7 @@ public static void UpdateZeroStringToNull()
887916
{
888917
ErrorHandler.DisplayMessage(ex);
889918
}
890-
finally
891-
{
892-
Cursor.Current = System.Windows.Forms.Cursors.Arrow;
893-
if (tbl != null)
894-
Marshal.ReleaseComObject(tbl);
895-
if (cell != null)
896-
Marshal.ReleaseComObject(cell);
897-
if (usedRange != null)
898-
Marshal.ReleaseComObject(usedRange);
899-
}
919+
900920
}
901921

902922
#endregion

‎VB/Scripts/ErrorHandler.vb‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Option Explicit On
44
Imports System.Environment
55
Imports System.Windows.Forms
66
Imports log4net
7-
'Imports log4net.Config 'needed if you want to change the path of the log file at runtime
87

98
<Assembly: log4net.Config.XmlConfigurator(Watch:=True)>
109

@@ -14,10 +13,6 @@ Namespace Scripts
1413

1514
Private Shared ReadOnly log As ILog = LogManager.GetLogger(GetType(ErrorHandler))
1615

17-
''' <summary>
18-
'''
19-
''' </summary>
20-
''' <param name="message"></param>
2116
Public Shared Sub CreateLogRecord(Optional ByVal message As String = "")
2217
Try
2318
Dim sf As New System.Diagnostics.StackFrame(1)
@@ -31,11 +26,6 @@ Namespace Scripts
3126

3227
End Sub
3328

34-
''' <summary>
35-
'''
36-
''' </summary>
37-
''' <param name="ex"></param>
38-
''' <param name="isSilent"></param>
3929
Public Shared Sub DisplayMessage(ex As Exception, Optional isSilent As Boolean = False)
4030
Dim sf As New System.Diagnostics.StackFrame(1)
4131
Dim caller As System.Reflection.MethodBase = sf.GetMethod()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /