2016-11-18 163 views
0

我正在研究一个脚本以帮助用户在循环中截取屏幕截图并将它们保存到word文档中。杀死一个Word.Basic应用程序,而不会杀死Word.Application主进程

我的代码正在运行,但是我面临的问题是,对于每个屏幕截图,我创建了一个WINWORD.EXE进程,并且该进程未被杀死,所以如果我多次运行脚本或在一次运行,我将最终得到大量的流程,我必须手动杀死。

这是我的脚本:

Option Explicit 

Dim strPath : strPath = WScript.ScriptFullName 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
Dim objFile : Set objFile = objFSO.GetFile(strPath) 
Dim strMainPath : strMainPath = objFSO.GetParentFolderName(objFile) 

' Cleaning 
Set objFile = Nothing 
Set objFSO = Nothing 

Dim objWord : Set objWord = CreateObject("Word.Application") 
objWord.Visible = False 
objWord.Documents.Open strMainPath & "\template\template.doc" 

Const wdStory = 6 
Const wdMove = 0 

Dim objSelection : Set objSelection = objWord.Selection 
objSelection.EndKey wdStory, wdMove 

Dim execFlag : execFlag = True 

Do While execFlag = True 

    Dim strPrint : strPrint = InputBox("Enter screenshot name","Screenshot Name", "") 
    With objSelection 
     .Font.Name = "Arial" 
     .Font.Size = "10" 
     .TypeText strPrint 
    End With 

    objSelection.TypeParagraph() 

    WScript.Sleep 5000 

    'Taking Screenshot using word object 
    With CreateObject("Word.Basic") 'This is the point where I create the processes that I'm unable to kill 
     .SendKeys "{prtsc}"  
    End With 

    ' Paste in the screen shot 
    objWord.Selection.Paste 

    Dim intAnswer : intAnswer = MsgBox("Continue?", vbYesNo, "Printscreen") 

    If intAnswer = vbNo Then execFlag = False 

    objSelection.EndKey wdStory, wdMove 
    objSelection.TypeParagraph() 

Loop 

Dim strFileName : strFileName = "" 
Do 
    strFileName = InputBox("Provide the file name","File Name", "") 
Loop While strFileName = "" 

objWord.ActiveDocument.SaveAs strMainPath & "\" & strFileName & ".doc" 
objWord.ActiveDocument.Close 
objword.Quit 
Set objword = Nothing 

Set objWord = CreateObject("Word.Application") 
objWord.Visible = True 
objWord.Documents.Open strMainPath & "\" & strFileName & ".doc" 
Set objWord = Nothing 

这些都是我指的是过程。我不能简单地杀死所有进程,因为其中一个实际引用了我的模板,这是一个Word文档,我正在存储截图。有关如何解决此问题的任何提示?

enter image description here

+2

如果你正在你的屏幕截图创建多余的进程。每次运行循环时,都会创建一个新进程。在代码的顶部,键入'Dim objBasic:Set objBasic = CreateObject(“Word.Basic”)',然后用'objBasic.SendKeys“{prtsc}”'替换你的'With'块。在循环结束之前立即输入'Set objBasic = Nothing'。这应该防止发生多个进程。 – Lou

+0

@Lou非常感谢。这解决了我的问题。如果你想发布它作为答案,我会接受它 –

回答

1

为了防止过度过程的发生,你的代码应该是这样的:

Option Explicit 

Dim strPath : strPath = WScript.ScriptFullName 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
Dim objFile : Set objFile = objFSO.GetFile(strPath) 
Dim strMainPath : strMainPath = objFSO.GetParentFolderName(objFile) 
Dim objBasic : Set objBasic = CreateObject("Word.Basic") 

' Cleaning 
Set objFile = Nothing 
Set objFSO = Nothing 

Dim objWord : Set objWord = CreateObject("Word.Application") 
objWord.Visible = False 
objWord.Documents.Open strMainPath & "\template\template.doc" 

Const wdStory = 6 
Const wdMove = 0 

Dim objSelection : Set objSelection = objWord.Selection 
objSelection.EndKey wdStory, wdMove 

Dim execFlag : execFlag = True 

Do While execFlag = True 

    Dim strPrint : strPrint = InputBox("Enter screenshot name","Screenshot Name", "") 
    With objSelection 
     .Font.Name = "Arial" 
     .Font.Size = "10" 
     .TypeText strPrint 
    End With 

    objSelection.TypeParagraph() 

    WScript.Sleep 5000 

    'Taking Screenshot using word object 
    objBasic.SendKeys "{prtsc}" 

    ' Paste in the screen shot 
    objWord.Selection.Paste 

    Dim intAnswer : intAnswer = MsgBox("Continue?", vbYesNo, "Printscreen") 

    If intAnswer = vbNo Then execFlag = False 

    objSelection.EndKey wdStory, wdMove 
    objSelection.TypeParagraph() 
    Set objBasic = Nothing 

Loop 

Dim strFileName : strFileName = "" 
Do 
    strFileName = InputBox("Provide the file name","File Name", "") 
Loop While strFileName = "" 

objWord.ActiveDocument.SaveAs strMainPath & "\" & strFileName & ".doc" 
objWord.ActiveDocument.Close 
objword.Quit 
Set objword = Nothing 

Set objWord = CreateObject("Word.Application") 
objWord.Visible = True 
objWord.Documents.Open strMainPath & "\" & strFileName & ".doc" 
Set objWord = Nothing