从 VBScript 打开 Word 挂起,无法找出原因

发布于 2024-11-16 14:13:48 字数 1533 浏览 3 评论 0原文

我并不是一个真正的程序员,所以如果我不知道任何标准的调试工具,请原谅我。

我有我认为是一个非常简单的VBScript(只是一个以 .vbs 扩展名保存的 txt 文件):

Const wdDoNotSaveChanges = 0
Const wdRevisionsViewFinal = 0
Const wdFormatPDF = 17

Dim arguments
Set arguments = WScript.Arguments

Function DOC2PDF(sDocFile)

  Dim fso ' As FileSystemObject
  Dim wdo ' As Word.Application
  Dim wdoc ' As Word.Document
  Dim wdocs ' As Word.Documents

  Set fso = CreateObject("Scripting.FileSystemObject")
  sDocFile = fso.GetAbsolutePathName(sDocFile)
  sPdfFile = fso.GetParentFolderName(sDocFile) + "\" + fso.GetBaseName(sDocFile) + ".pdf"

  Set wdo = CreateObject("Word.Application")
  Set wdocs = wdo.Documents

  Set wdoc = wdocs.Open(sDocFile)

  if fso.FileExists(sPdfFile) Then
    fso.DeleteFile sPdfFile, True
  End If

  Set wview = wdoc.ActiveWindow.View
  wview.ShowRevisionsAndComments = False
  wview.RevisionsView = wdRevisionsViewFinal
  wdoc.SaveAs sPdfFile, wdFormatPDF
  wdo.Quit wdDoNotSaveChanges

  Set fso = Nothing
  Set wdo = Nothing

End Function

但是,以下行会造成巨大的痛苦:

Set wdoc = wdocs.Open(sDocFile)

有时 Word ActiveX 对象会冻结在此处 步。我通过一些超级简单的调试验证了这一点,方法是在每行后面放置一个 WriteLine 并查看它停止的位置。

Word 只是坐在那里,消耗 100% CPU,并且脚本永远不会越过该步骤。

我如何进行调试以找出 Word ActiveX 对象到底发生了什么以及为什么它只是挂起并且永远不会返回?

I'm not really a programmer by trade, so forgive me if I'm not aware of any standard debugging tools.

I have what I thought was a very simple VBScript (just a txt file saved with a .vbs extension):

Const wdDoNotSaveChanges = 0
Const wdRevisionsViewFinal = 0
Const wdFormatPDF = 17

Dim arguments
Set arguments = WScript.Arguments

Function DOC2PDF(sDocFile)

  Dim fso ' As FileSystemObject
  Dim wdo ' As Word.Application
  Dim wdoc ' As Word.Document
  Dim wdocs ' As Word.Documents

  Set fso = CreateObject("Scripting.FileSystemObject")
  sDocFile = fso.GetAbsolutePathName(sDocFile)
  sPdfFile = fso.GetParentFolderName(sDocFile) + "\" + fso.GetBaseName(sDocFile) + ".pdf"

  Set wdo = CreateObject("Word.Application")
  Set wdocs = wdo.Documents

  Set wdoc = wdocs.Open(sDocFile)

  if fso.FileExists(sPdfFile) Then
    fso.DeleteFile sPdfFile, True
  End If

  Set wview = wdoc.ActiveWindow.View
  wview.ShowRevisionsAndComments = False
  wview.RevisionsView = wdRevisionsViewFinal
  wdoc.SaveAs sPdfFile, wdFormatPDF
  wdo.Quit wdDoNotSaveChanges

  Set fso = Nothing
  Set wdo = Nothing

End Function

however, the following line is causing huge grief:

Set wdoc = wdocs.Open(sDocFile)

Sometimes the Word ActiveX object just freezes at this step. I've verified this by some super-simple debugging by putting a WriteLine after each line and seeing where it stops.

Word just sits there consuming 100% CPU, and the script never gets past that step.

How can I go about debugging to find out what the hell is going on with the Word ActiveX object and why it's just hanging and never returning?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一枫情书 2024-11-23 14:13:48

Word 可能正在等待您的提示。我将使 Word 可见,看看您是否可以直观地看到问题所在:

  Set wdo = CreateObject("Word.Application")
  'if memory serves, this should make Word visible
  wdo.Visible = true
  Set wdocs = wdo.Documents

Word might be waiting for a prompt from you. I would make Word visible and see if you can visually see what the problem is:

  Set wdo = CreateObject("Word.Application")
  'if memory serves, this should make Word visible
  wdo.Visible = true
  Set wdocs = wdo.Documents
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文