Nachdem ich mich ziemlich lange umgesehen habe, stehe ich ein wenig ratlos da dies (ich bin mir sicher) ein ziemlich häufiges Problem.Speichern und Schließen von Office-Programmen (Word, Excel, Access, Outlook) mit VBS für Backups
Ich führe nächtliche Backups aller unserer Büromaschinen durch, aber Outlook PST-Dateien verhindert oft, dass dieses Formular erfolgreich abgeschlossen wird. Ich habe die Lösung für Outlook gefunden, aber andere MS Office-Anwendungen neigen dazu, Backups auch nach erfolgreichem Abschluss zu blockieren.
Ich habe herausgefunden, wie Outlook, Word und Excel zu speichern und zu schließen. Access Ich habe eine Lösung für, aber möchte das ein wenig eleganter schließen.
Ich habe Bits und Teile verstreut gefunden, aber es scheint, dass es ein Repository geben sollte, in dem Leute finden können, wie man all diese Programme schließt. (sie sind doch gar nicht so unterschiedlich, aber es gibt genug Unterschiede, um einen ernsthaften Schraubenschlüssel in meine Gänge zu werfen).
Ich sah auch bei this StackOverflow thread, but it only addresses part of the issue (not excel..)
Hier ist der ist Code arbeitet ein Dokument und schließen Sie Word speichern:
Dim objWord
Dim doc
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
'No need to do anything, Word is not running
Else
'Open your document and ensure its visible and activate after openning
objWord.Visible = True
objWord.Activate
Set oWS = WScript.CreateObject("WScript.Shell")
' Get the %userprofile% in a variable, or else it won't be recognized
userProfile = oWS.ExpandEnvironmentStrings("%userprofile%")
Dim objNetwork
Dim userName
Dim FSO
Dim Folder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.userName
If NOT (FSO.FolderExists(userProfile + "\Desktop\Docs-You-Left-Open")) Then
FSO.CreateFolder(userProfile + "\Desktop\Docs-You-Left-Open")
End If
Do while objWord.Documents.Count <> 0
For Each doc in objWord.Documents
doc.SaveAs(userProfile + "\Desktop\Docs-You-Left-Open\" & doc.Name)
doc.Close
Next
Loop
Set doc = Nothing
objWord.quit
End If
Set objWord = Nothing
Hier ist die Arbeits Code ordnungsgemäß schließen Ausblick:
Dim objOutlook 'As Outlook.Application
Dim olkIns
Set objOutlook = CreateObject("Outlook.Application")
If objOutlook Is Nothing Then
'no need to do anything, Outlook is not running
Else
'Outlook running
Do while objOutlook.Inspectors.Count <> 0
For each olkIns in objOutlook.Inspectors
olkIns.Close olSave
Next
Loop
objOutlook.Session.Logoff
objOutlook.Quit
End If
Set objOutlook = Nothing
Hier ist der Arbeitscode Zugang zu schließen - nicht ordnungsgemäß - braucht improvment:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'MSACCESS.EXE'")
Set oShell = CreateObject("WScript.Shell")
For Each objProcess in colProcessList
oShell.Run "taskkill /im MSACCESS.EXE", , True
Next
Und das ist der Excel-Code, den ich zu bekommen habe versucht, aber kann nicht scheinen, zu durchbrechen diese, wo es 16 klebt auf der Linie hält objExcel.Application.Visible = True
:
Dim objExcel
Dim wkb
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.
If ExcelWasNotRunning = True Then
objExcel.Application.Quit
Else
'Open your document and ensure its visible and activate after openning
objExcel.Application.Visible = True
objExcel.Activate
Set oWS = WScript.CreateObject("WScript.Shell")
' Get the %userprofile% in a variable, or else it won't be recognized
userProfile = oWS.ExpandEnvironmentStrings("%userprofile%")
Dim objNetwork
Dim userName
Dim FSO
Dim Folder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.userName
If NOT (FSO.FolderExists(userProfile + "\Desktop\Docs-You-Left-Open")) Then
FSO.CreateFolder(userProfile + "\Desktop\Docs-You-Left-Open")
End If
Do while objExcel.Workbooks.Count <> 0
For Each wkb in objExcel.Workbooks
wkb.SaveAs(userProfile + "\Desktop\Docs-You-Left-Open\" & wkb.Name)
wkb.Close
Next
Loop
Set wkb = Nothing
objExcel.quit
End If
Set objExcel = Nothing
Jede Hilfe auf dem Excel - und warum dies würde hängen:
objExcel.Application.Visible = True
oder wie man den Zugriff Access elegant (einschließlich der Behandlung von Fehlern beim Schließen von Formularen) gehen würde sehr geschätzt werden! Und ich hoffe, dass diese Konsolidierung der Themen hilft anderen, so dass sie müssen nicht einen ganzen Tag damit verbringen, den Kopf wickeln diese um ...
Stellen Sie Ihren 'Set wkb = Nothing' &' objExcel. quit 'outside 'end if' versuche auch' objExcel.quit' in' objExcel.Application.Quit' zu ändern, ob das hilft – 0m3r