Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word Save As PDF / scheduled task

Status
Not open for further replies.

fredmartinmaine

IS-IT--Management
Mar 23, 2015
79
0
0
US
' ChildScript.vbs
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
objSelection.TypeText "Hello World"
objDoc.SaveAs "test.pdf", 17 ' 17 = PDF
objDoc.Close 0

The above code creates a Word doc and saves it as a PDF file, then closes Word without saving the Word doc.

Works perfectly when run interactively by a user. It doesn't prompt user for anything. Just runs and created the PDF.

When the script is a scheduled task, though, same machine and same user, it always hangs (doesn't crash) on the SaveAs call.

Details of the scheduled task:
A parent vbscript is scheduled, the action is:
Program: C:\Windows\System32\cscript.exe
Argument: "C:\Windows\test\ParentScript.vbs"

In that parent script a call is made to run the child script:
objShell.Run("C:\Windows\test\ChildScript.vbs", 0, TRUE) ' windows style 0, wait for return TRUE

I do not believe this is anything to do with scheduler, the objShell,run, and so on. I have other scripts running in this exact way that are working, creating Excel docs and XML files.

What's different here is the MS Word SaveAs bit. Why would it work interactively but not as a scheduled task?

Stumped.
 
I experienced this in vba, so may need to do something similar in vbs. Here is a code snippet and maybe you can modify it for use in your code.

Code:
...
...
'seems that even though file is editable, when try to
'save as pdf, word thinks file is read only, to work
'around this, save the file, close it, reopen and then
'file will save as pdf
'Due to fake readonly issue, save as then reopen
'in order to be able to print to pdf
    stPDFName = sSavePath & InsertText & " " & stExtractTitle & ".docx"
    worddoc.SaveAs stPDFName
    worddoc.Close False

    Set worddoc = WordApp.Documents.Open(stPDFName)
    'Print the document as a PDF    
    worddoc.ExportAsFixedFormat Replace(stPDFName, ".docx", ".pdf"), 17
...
...
 
Thank you for that; unfortunately the same result. Works when run interactively, but no through the task scheduler.
 
Couple of things:

[tt]objShell.Run("C:\Windows\test\ChildScript.vbs", 0, TRUE) ' windows style 0, wait for return TRUE[/tt]

This is an error. Childscript.vb will not run

Needs to be

[tt]objShell.Run "C:\Windows\test\ChildScript.vbs", 0, TRUE ' windows style 0, wait for return TRUE[/tt]

or

[tt]intResult=objShell.Run("C:\Windows\test\ChildScript.vbs", 0, TRUE) ' windows style 0, wait for return TRUE[/tt]

or

[tt]call objShell.Run("C:\Windows\test\ChildScript.vbs", 0, TRUE) ' windows style 0, wait for return TRUE[/tt]


Secondly, if you do run childscript.vsb it will leave invisible instances of winword running. You need to add

[tt]objWord.Quit[/tt]

at the end.
 
You are correct, I'm actually using the intResult=objShell.Run version, I didn't copy/paste the whole thing. I do quit Word as well. The issue really is with saving the document.

In fact, saving as a normal Word doc doesn't work either. That is, it works interactively but not as a scheduled task.

I just noticed, when the scheduled Parent starts, it appears as a CScript (console script) in Task Manager as you'd expect.

But when objShell.Run is called to run the Child, a WScript (windows script) task appears in Task Manager. Perhaps that's the root problem; maybe a scheduled task chokes on anything but CScript.

 
So you are not posting all your code, and the code you are posting differs from what you are actually using? Makes it difficult for us to help! If I use the following (verbatim):

Code:
[blue][COLOR=green]' parentscript.vbs[/color][COLOR=green][/color]
Set objShell = wscript.CreateObject("Wscript.Shell")
objShell.Run "d:\test\ChildScript.vbs", 0, TRUE [COLOR=green]' windows style 0, wait for return TRUE[/color][/blue]

Code:
[blue][COLOR=green]' ChildScript.vbs[/color]
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
objSelection.TypeText "Hello World"
objDoc.SaveAs "d:\test\test.pdf", 17 ' 17 = PDF
objDoc.Close 0
objWord.Quit[/blue]

it works fine, exactly as expected, whether I run parentscript.vbs directly by double-clicking in explorer or from a commandline, or via task scheduler. This now suggests that there is something else going on in your code that we can't see

And scheduled tasks are as happy to use wscript as they are to use cscript as the engine.

 
Thanks. I set up the same test with new scripts, using your code except file paths are c:\bin\, and I get the same result as I did with the other script. From explorer, works fine. As scheduled task, or as 'run now' from scheduler, it runs and does not stop; creates no pdf.

I modified it to write a line to a log file both before and after the SaveAs command; logs before, but not after. So it's hanging there alright.

Task action: C:\Windows\System32\cscript.exe "c:\bin\parentscript.vbs"
Since the user is running other scheduled VBS tasks set up the same way I can't imagine that the task setup is the issue.
There must be some difference between your setup and mine. The system is running Windows 10; version of Outlook is 2013.
 
Have you got it configured to 'Run whether the user is logged in or not'? I ask because I have managed to replicate your symptoms if I make that config change to the task - but given Office apps perform erratically when no longer running in the user's interactive session, this doesn't surprise me (see for Microsoft's comments).
 
I have. Checked a few other things too, but to no effect, such as "Configure for:". It turns out, I had seen that article, in particular the modal dialog box thing. I assumed that since running interactively didn't pop up a box, then running from the scheduler wouldn't either; but I can imagine there might be a scenario where that would happen. Well, stumped for the moment but will likely be back. In this project I need to pull data from an XML file, stuff it into a PDF, and attach that to an email. Can't use any 3rd party products; goal is to have it all be native to Windows or Microsoft Office suite.


 
I wanted to post back ... the solution was, the install of Office on the PC in question was very much behind in MS patches. Not sure why WSUS wasn't patching it up (like other PCs here) but after we manually patched Office up fully on that machine, by going out to MS directly ... this started working. A similar issue trying to save Excel as another format, while running via vbscript as a scheduled task, also cleared up.
Well there's a good argument for keeping patched current.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top