?
Solved

Using Word to convert RTF to PDF with no-one logged in

Posted on 2011-04-26
4
Medium Priority
?
1,007 Views
Last Modified: 2012-05-11
Hi!
I am trying to set up a VBScript to use a Word object to open a RTF file and export it as PDF. The VBScript I wrote works fine if I execute it from a command prompt. However, when I get a server program to execute the command, it fails with error 4248 (This command is not available because no document is open.).

This might be a red herring, but when using this scenario before with Excel I found I needed to set the DCOM Config to a specific user so I wondered if this might help. However with Office 2007 installed on Server 2008 I can't find the DCOM entries for Word or Excel!

I've attached a code snippet. most things are obvious; 'tf' is a textstream object pointing to a log file. When executed by the server program it gets as far as line 13. It would appear that, although the oWord.Documents.Open didn't generate an error, the document is somehow not open as far as the ExportAsFixedFormat is concerned. I tried altering the line to oWord.Documents(1).ExportAsFixedFormat ... but this generated a different error: -5941 (The requested member of the collection does not exist.) which again implies the open didn't work.

I'm aware that this is pushing the boundaries a bit with the Word object (see http://support.microsoft.com/kb/257757), but I'm hoping there is a workaround!

Thanks in advance
Set oWord = CreateObject("Word.Application")
If oWord Is Nothing Or Err > 0 Then
    tf.writeline("ERROR: MSWord not installed! Err=" & Err)
    tf.Close
    Wscript.Quit
End If
tf.writeline("Open Word Application")
oWord.Visible = False
tf.writeline("Visible=False")
oWord.Documents.Open NewName, False, True, False
tf.writeline("OpenFile")
On Error Resume Next
oWord.ActiveDocument.ExportAsFixedFormat pdfFile, wdFormatPDF
If Err.Number <> 0 Then
    tf.writeline "ERROR: Can't generate pdf. Error:-" & Err.Number & ":" & Err.Description
    oWord.Quit
    Set oWord = Nothing
    Wscript.Quit
End If
On Error GoTo 0
tf.writeline("Created pdf file:" & pdfFile)
oWord.Quit
Set oWord = Nothing

Open in new window

0
Comment
Question by:David Allen
  • 3
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35468919
It might tell you something different if you try to capture the document object.
Set oWord = CreateObject("Word.Application")
If oWord Is Nothing Or Err > 0 Then
    tf.writeline("ERROR: MSWord not installed! Err=" & Err)
    tf.Close
    Wscript.Quit
End If
tf.writeline("Open Word Application")
oWord.Visible = False
tf.writeline("Visible=False")
SEt oDoc = oWord.Documents.Open(NewName, False, True, False)
tf.writeline("OpenFile")
On Error Resume Next
oDoc.ExportAsFixedFormat pdfFile, wdFormatPDF
If Err.Number <> 0 Then
    tf.writeline "ERROR: Can't generate pdf. Error:-" & Err.Number & ":" & Err.Description
    oDoc.Close
    oWord.Quit
    Set oWord = Nothing
    Wscript.Quit
End If
On Error GoTo 0
tf.writeline("Created pdf file:" & pdfFile)
oDoc.Close
oWord.Quit
Set oWord = Nothing

Open in new window

0
 

Author Comment

by:David Allen
ID: 35473180
I've tried using the code modified as you suggest (see code). When I run this from a command shell directly it works fine. But when it is executed via a service it is unable to open the document. I get these messages in the log file:
Open Word Application
Visible=False
Documents found. count=0
ERROR: Can't open document. Error:-424:Object required
This indicates that it failed on line 22. I can't make out what object is required. The oDocs object is OK because there was no error creating it nor using it.
Can you make sense of this?
Thanks.
Set oWord = CreateObject("Word.Application")
If oWord Is Nothing Or Err > 0 Then
    tf.writeline("ERROR: MSWord not installed! Err=" & Err)
    tf.Close
    Wscript.Quit
End If
tf.writeline("Open Word Application")
oWord.Visible = False
tf.writeline("Visible=False")
On Error Resume Next
Set oDocs = oWord.Documents
If oDocs Is Nothing Or Err.Number <> 0 Then
    tf.writeline "ERROR: Can't see documents object. Error:-" & Err.Number & ":" & Err.Description
    tf.Close
    oWord.Quit
    Set oWord = Nothing
    Wscript.Quit
Else
    tf.writeline("Documents found. count=" & oDocs.Count)
End If
On Error Resume Next
Set oDoc = oDocs.Open(NewName, False, True, False)
If oDoc Is Nothing Or Err.Number <> 0 Then
    tf.writeline "ERROR: Can't open document. Error:-" & Err.Number & ":" & Err.Description
    tf.Close
    oWord.Quit
    Set oWord = Nothing
    Wscript.Quit
Else
    tf.writeline("OpenFile. Character count=" & oDoc.Characters.Count)
End If
On Error Resume Next
oDoc.ExportAsFixedFormat pdfFile, wdFormatPDF
If Err.Number <> 0 Then
    tf.writeline "ERROR: Can't generate pdf. Error:-" & Err.Number & ":" & Err.Description
    tf.Close
    oDoc.Close
    oWord.Quit
    Set oWord = Nothing
    Wscript.Quit
Else
    tf.writeline("Created pdf file:" & pdfFile)
End If
On Error GoTo 0
oDoc.Close
oWord.Quit
Set oWord = Nothing

Open in new window

0
 

Accepted Solution

by:
David Allen earned 0 total points
ID: 35513051
I've independently solved this problem now. The solution was not related to the 1 response I received.

For those interested the solution was:
It would appear that on Server 2008 with Office 2007 a folder is missing which prevents the use of Word and Excel when run as a service. What I needed to do was to create a folder called 'Desktop' in
C:\Windows\SysWOW64\config\systemprofile. The following two links give some details:
http://social.msdn.microsoft.com/Forums/en-US/architecturegeneral/thread/0f5448a7-72ed-4f16-8b87-922b71892e07
http://stackoverflow.com/questions/863864/excel-2007-automation-on-top-of-a-windows-server-2008-x64
0
 

Author Closing Comment

by:David Allen
ID: 35714819
Not solved through Experts Exchange!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question