VB脚本需要帮助,并生成一个Excel工作表

我正在尝试创build一个VB脚本,它将为我的环境中的服务器的磁盘空间生成基于Excel的报告。 我还有3个障碍,我无法克服。

  1. 如何将工作表中的所有列/单元格alignment? 25行是我试图做到这一点,但是它会引发错误,“无法设置Range类的Horizo​​ntalAlignment属性”。

  2. 某些服务器有多个驱动器(例如C,D,E)。 脚本生成报告时,只显示最后一个驱动器(例如E)。 我怎样才能让它显示每个服务器的每个驱动器?

  3. 当我运行这个脚本的时候,我希望它能够在当天的磁盘使用情况下追加报表。 目前,它将用当天的磁盘使用率replace现有的单元格。

我的脚本代码如下:

On Error Resume Next Const ForReading = 1 Const HARD_DISK = 3 x = 1 dtmDate = Date strDay = Day(Date) strMonth = Month(Date) strYear = Right(Year(Date), 2) strFileName = "C:\Users\cvandal\Desktop\Scripts\Server_Disk_Space_Report.xlsx" Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists("C:\Users\cvandal\Desktop\Scripts\Server_Disk_Space_Report.xlsx") Then Set serverList = objFSO.OpenTextFile("servers.txt", ForReading) Set objExcel = CreateObject("Excel.Application") objExcel.Workbooks.Open "C:\Users\cvandal\Desktop\Scripts\Server_Disk_Space_Report.xlsx" objExcel.Visible = True objExcel.Columns("A:ZZ").ColumnWidth = 25 objExcel.Columns("A:ZZ").HorizontalAlignment = xlHAlignLeft objExcel.Cells(2, 1).Value = "Server Disk Space Report" objExcel.Cells(4, 1).Value = dtmDate objExcel.Cells(5, 1).Value = "Drives:" objExcel.Cells(6, 1).Value = "Total Capacity (in GB):" objExcel.Cells(7, 1).Value = "Used Capacity (in GB):" objExcel.Cells(8, 1).Value = "Free Space (in GB):" objExcel.Cells(9, 1).Value = "Free Space (in %):" Do Until serverList.AtEndOfStream x = x + 1 strComputer = serverList.ReadLine Set objWMIService = GetObject("winmgmts:{impersonationlevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = " & HARD_DISK & "") If Err.Number <> 0 Then 'WScript.Echo "Error: " & Err.Number 'WScript.Echo "Error (Hex): " & Hex(Err.Number) 'WScript.Echo "Source: " & Err.Source 'WScript.Echo "Description: " & Err.Description objExcel.Cells(4, x).Value = strComputer & " - " & Err.Description objExcel.Cells(4, x).Columns.AutoFit Err.Clear Else For Each objDisk in colDisks drives = "Error" totalCapacity = 0 freeSpace1 = 0 usedCapacity = 0 freeSpace2 = 0 drives = objDisk.DeviceID totalCapacity = Round((objDisk.Size / 1073741824), 2) freeSpace1 = Round((objDisk.FreeSpace / 1073741824), 2) usedCapacity = Round((totalCapacity - freeSpace1), 2) freeSpace2 = Round((freeSpace1 / totalCapacity)*100, 0) If freeSpace2 > 20 Then objExcel.Cells(4, x).Value = strComputer objExcel.Cells(5, x).Value = drives objExcel.Cells(6, x).Value = totalCapacity & " GB" objExcel.Cells(7, x).Value = usedCapacity & " GB" objExcel.Cells(8, x).Value = freeSpace1 & " GB" objExcel.Cells(9, x).Value = freeSpace2 & "%" objExcel.Cells(9, x).Interior.Color = RGB(198,239,206) ElseIf freeSpace2 < 10 Then objExcel.Cells(4, x).Value = strComputer objExcel.Cells(5, x).Value = drives objExcel.Cells(6, x).Value = totalCapacity & " GB" objExcel.Cells(7, x).Value = usedCapacity & " GB" objExcel.Cells(8, x).Value = freeSpace1 & " GB" objExcel.Cells(9, x).Value = freeSpace2 & "%" objExcel.Cells(9, x).Interior.Color = RGB(255,199,206) Else objExcel.Cells(4, x).Value = strComputer objExcel.Cells(5, x).Value = drives objExcel.Cells(6, x).Value = totalCapacity & " GB" objExcel.Cells(7, x).Value = usedCapacity & " GB" objExcel.Cells(8, x).Value = freeSpace1 & " GB" objExcel.Cells(9, x).Value = freeSpace2 & "%" objExcel.Cells(9, x).Interior.Color = RGB(255,235,156) End If Next End If Loop Else Set objExcel = CreateObject("Excel.Application") objExcel.Visible = False Set objWorkbook = objExcel.Workbooks.Add() objWorkbook.SaveAs(strFileName) objExcel.Quit WScript.Echo "Server_Disk_Space_Report.xlsx has been created. Please re-run the script." End If 

  1. 看起来你并没有为xlHAlignLeft的值定义一个常量。

  2. 您应该增加磁盘循环中的计数器x

     For Each objDisk in colDisks x = x + 1 ' <-- add this line 

    你可能需要在代码中增加计数器的位置,具体取决于你希望输出的样子。 我想我放在我的例子中的地方会导致每台机器之间的空白。

  3. 这里的技巧是将x初始化为第一个可用行,而不是总是默认为1.下面的代码在第一列('A')中search最后一个非空行。 ( 参考 )

     Const xlUp = -4162 x = objExcel.Cells(Rows.Count, 1).End(xlUp).Row 

您是否知道可以将HTML表格的标记放入.xls文件并用Excel打开? 它甚至适用于Excel 2000! 尝试一下,你会更快乐,你不必创build“Excel.Application”COM对象!