Excel宏错误说“自动化错误”时使用WMI

ryoqjall  于 2023-01-10  发布在  其他
关注(0)|答案(2)|浏览(247)

我试图在VB(Excel宏)中运行下面提到的代码,但我坚持运行时弹出一个错误说“自动化错误”。

strComputer = "."

Set objNetwork = CreateObject("Wscript.Network")
Set fs = CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='U:\'} Where " _ 
        & "ResultClass = CIM_DataFile")
For Each objFile In colFiles
       if objFile.FileName = "ml_*" Then
           destinationPROD = "X:\ABC\" & objFile.FileName & "." & objFile.Extension
           objFile.Copy(destinationPROD)
           objFile.delete
       else
           destinationPROD = "X:\PQR\" & objFile.FileName & "." & objFile.Extension
           objFile.Copy(destinationPROD)
           objFile.delete
       End If
Next
wgx48brx

wgx48brx1#

您只需要在"winmgmts:"之后再加一个斜杠:)
它应该是:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
cnwbcb6i

cnwbcb6i2#

可能是权限问题。请使用本地磁盘进行测试。请确保所有必需的目录都存在。

相关问题