×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

MS Word Save As PDF / scheduled task

MS Word Save As PDF / scheduled task

MS Word Save As PDF / scheduled task

(OP)
' 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.

RE: MS Word Save As PDF / scheduled task

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
...
... 

RE: MS Word Save As PDF / scheduled task

(OP)
Interesting. I'll look at this and post back here.

RE: MS Word Save As PDF / scheduled task

(OP)
Thank you for that; unfortunately the same result. Works when run interactively, but no through the task scheduler.

RE: MS Word Save As PDF / scheduled task

Couple of things:

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

This is an error. Childscript.vb will not run

Needs to be

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

or

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

or

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


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

objWord.Quit

at the end.

RE: MS Word Save As PDF / scheduled task

(OP)
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.

RE: MS Word Save As PDF / scheduled task

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

' parentscript.vbs
Set objShell = wscript.CreateObject("Wscript.Shell")
objShell.Run "d:\test\ChildScript.vbs", 0, TRUE ' windows style 0, wait for return TRUE 

CODE

' ChildScript.vbs
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 

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.

RE: MS Word Save As PDF / scheduled task

(OP)
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.

RE: MS Word Save As PDF / scheduled task

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 https://support.microsoft.com/en-gb/help/257757/co... for Microsoft's comments).

RE: MS Word Save As PDF / scheduled task

(OP)
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.


RE: MS Word Save As PDF / scheduled task

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close