无法通过Visual Basic复制文件

问题描述 投票:0回答:1

好的,所以我有这个代码(发布在下面)复制outlook PST文件,当与完整的位置文件路径一起使用时,它运行得非常好。我添加了一个方法来在第一行中运行%UserProfile%,因为这需要在GPO的域上下文中运行并且单独执行它是不可行的。这会运行并关闭outlook并在适当的时候重新打开它,除非有一件事是不对的。它不再复制适当的文件。我回应了最初的%userprofile%部分,它正在正确读取驱动器号\ users \ userprofile。我不确定这是破坏的地方或如何识别它。任何帮助,将不胜感激

'===================BEGIN MODIFY====================================
Set objShell = CreateObject("WScript.Shell")
userProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")

'Set the amount of pst-files you want to copy. Start counting at 0!
ReDim pst(1)

'Define the location of each pst-file to backup. Increase the counter!
pst(0) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Outlook Data File - mike.pst"
pst(1) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Archive.pst"

'Define your backup location
BackupPath = "%UserProfile%\Documents\Outlook Backups\"

'Keep old backups? TRUE/FALSE
KeepHistory = FALSE

'Maximum time in milliseconds for Outlook to close on its own
delay = 30000 'It is not recommended to set this below 8000

'Start Outlook again afterwards? TRUE/FALSE
start = TRUE

'===================STOP MODIFY====================================

'Close Outlook
Call CloseOutlook(delay)

'Outlook is closed, so we can start the backup
Call BackupPST(pst, BackupPath, KeepHistory)

'Open Outlook again when desired.
If start = TRUE Then
  Call OpenOutlook()
End If


Sub CloseOutlook(delay)
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  'If Outlook is running, let it quit on its own.
  For Each Process in objWMIService.InstancesOf("Win32_Process")
    If StrComp(Process.Name,"OUTLOOK.EXE",vbTextCompare) = 0 Then
      Set objOutlook = CreateObject("Outlook.Application")
      objOutlook.Quit
      WScript.Sleep delay
      Exit For
    End If
  Next

  'Make sure Outlook is closed and otherwise force it.
  Set colProcessList = objWMIService.ExecQuery _
  ("Select * from Win32_Process Where Name = 'Outlook.exe'")
  For Each objProcess in colProcessList
    objProcess.Terminate()
  Next
  Set objWMIService = Nothing
  Set objOutlook = Nothing
  set colProcessList = Nothing
End Sub


Sub BackupPST(pst, BackupPath, KeepHistory)
  Set fso = CreateObject("Scripting.FileSystemObject")

  If KeepHistory = True Then
    ArchiveFolder = Year(Now) & "-" & Month(Now) & "-" & Day(Now)
    BackupPath = BackupPath & ArchiveFolder & "\"
  End If

  For Each pstPath in pst
    If fso.FileExists(pstPath) Then
      fso.CopyFile pstPath, BackupPath, True
    End If
  Next
  Set fso = Nothing
End Sub


Sub OpenOutlook()
  Set objShell = CreateObject("WScript.Shell")
  objShell.Run "Outlook.exe"
End Sub
vbscript outlook copying gpo
1个回答
0
投票

当你执行userProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")时,你将%UserProfile%的路径放在名为userProfilePath的变量中但是在你不使用这个变量之后。

我认为你的代码应该更好用以下的东西:

'Define the location of each pst-file to backup. Increase the counter!
pst(0) = userProfilePath+"\AppData\Local\Microsoft\Outlook\PST\Outlook Data File - mike.pst"
pst(1) = userProfilePath+"\AppData\Local\Microsoft\Outlook\PST\Archive.pst"

'Define your backup location
BackupPath = userProfilePath"\Documents\Outlook Backups\"

代替

'Define the location of each pst-file to backup. Increase the counter!
pst(0) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Outlook Data File - mike.pst"
pst(1) = "%UserProfile%\AppData\Local\Microsoft\Outlook\PST\Archive.pst"

'Define your backup location
BackupPath = "%UserProfile%\Documents\Outlook Backups\"
© www.soinside.com 2019 - 2024. All rights reserved.