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

时间:2011-02-03 00:10:28

标签: excel vbscript

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

  1. 如何左对齐工作表中的所有列/单元格?第25行是我试图这样做但它抛出错误,“无法设置Range类的Horizo​​ntalAlignment属性”。

  2. 某些服务器有超过1个驱动器(例如C,D,E)。当脚本生成报告时,它将仅显示最后一个驱动器(例如E)。如何让它显示每个服务器的每个驱动器?

  3. 当我运行脚本时,我希望它附加当前日期磁盘使用情况的报告。目前,它将用当前的磁盘使用量替换现有的单元格。

  4. 我的脚本代码如下:

    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
    

2 个答案:

答案 0 :(得分:2)

  1. 看起来您没有为xlHAlignLeft的值定义常量。

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

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

    根据您希望输出的显示方式,您可能必须使用增加计数器的代码中的确切位置。我认为我在我的例子中放置的地方会导致每台机器之间出现空白。

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

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

答案 1 :(得分:0)

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