杀死Word.Basic应用程序进程而不杀死Word.Application主进程

时间:2016-11-18 13:15:09

标签: vbscript

我正在编写一个脚本来帮助用户在循环中截取屏幕截图并将其保存到word文档中。

我的代码正在运行,但我遇到的问题是,对于每个屏幕截图,我正在创建一个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

1 个答案:

答案 0 :(得分:2)

为防止发生多余进程,您的代码应如下所示:

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