开发者

Opening Word from VBScript hangs, can't figure out why

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:

Se开发者_开发百科t 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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜