Vbscript 杀死一个字。不杀死字的基本应用程序进程。应用程序主进程
我正在编写一个脚本,帮助用户在循环中截图并将其保存到word文档中 我的代码正在运行,但我面临的问题是,对于每个屏幕截图,我正在创建一个Vbscript 杀死一个字。不杀死字的基本应用程序进程。应用程序主进程,vbscript,Vbscript,我正在编写一个脚本,帮助用户在循环中截图并将其保存到word文档中 我的代码正在运行,但我面临的问题是,对于每个屏幕截图,我正在创建一个WINWORD.EXE进程,而该进程并没有被终止,因此,如果我多次运行脚本或在一次运行中获取多个屏幕截图,我将不得不手动终止大量进程 这是我的剧本: Option Explicit Dim strPath : strPath = WScript.ScriptFullName Dim objFSO : Set objFSO = CreateObject("Scr
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文档,我在其中存储我的屏幕截图。关于如何解决这个问题有什么建议吗
为了防止出现过多的进程,您的代码应该如下所示:
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
拍摄屏幕截图的地方是创建多余的进程。每次运行循环时,都会创建一个新流程。在代码顶部,键入
Dim objBasic:Set objBasic=CreateObject(“Word.Basic”)
,然后将块替换为objBasic.SendKeys“{prtsc}”
。在循环结束之前,立即键入Set objBasic=Nothing
。这将防止出现多个进程。@非常感谢。这解决了我的问题。如果您想将其作为答案发布,我将接受它仅供参考:您可以使用Word.Application
中的WordBasic
来避免创建单独的实例-->objWord.WordBasic.SendKeys“{prtsc}”