BookmarkSubscribeRSS Feed
omka_
Fluorite | Level 6

Hello everyone,,

 

I am trying to convert an RTF file to a PDF file using SAS 9.4. I am not talking about merely changing the ods output from rtf to pdf. Instead, I have a file in a folder that is in RTF format, and I want SAS to use that and convert it with code into a PDF file into another folder. Attached is a sample rtf file. I have read online that you can use the x command to do this but I am not sure about how to write the code. Thanks.

33 REPLIES 33
Reeza
Super User

You need either DDE or VBS for this. I'm going to recommend vbs. 

 

First try this script, by pasting into Notepad and saving it as a file with the vbs extension (not txt).

 

Modify the sFolder path to be appropriate for your computer and test that the script works first without SAS. 

 

bRecursive = False
sFolder = "C:\_localdata\temp\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWord = CreateObject("Word.Application")
oWord.Visible = True

Set oFolder = oFSO.GetFolder(sFolder)
ConvertFolder(oFolder)
oWord.Quit

Sub ConvertFolder(oFldr)
    For Each oFile In oFldr.Files
        If LCase(oFSO.GetExtensionName(oFile.Name)) = "rtf" Then
            Set oDoc = oWord.Documents.Open(oFile.path)
            oWord.ActiveDocument.SaveAs oFile.path & "x", 17
            oDoc.Close
        End If
    Next

    If bRecursive Then
        For Each oSubfolder In oFldr.Subfolders
            ConvertFolder oSubfolder
        Next
    End If
End Sub

Once it works, then you can call the script from SAS, via x Command. 

 

x "cscript ""path to file\temp.vbs""";

 

Then once that is working, you can decide if you want to create the vbs file directly from SAS using PUT statements. 

 

This is one way at least, there are probably others...I think there's a one liner if you have Adobe Professional installed.

 

This is an example of how that may look when fully done, which converts files to a native Excel format.

https://gist.github.com/statgeek/c51f58a009f8d315a200f34912e494b1

 


@omka_ wrote:

Hello everyone,,

 

I am trying to convert an RTF file to a PDF file using SAS 9.4. I am not talking about merely changing the ods output from rtf to pdf. Instead, I have a file in a folder that is in RTF format, and I want SAS to use that and convert it with code into a PDF file into another folder. Attached is a sample rtf file. I have read online that you can use the x command to do this but I am not sure about how to write the code. Thanks.


 

omka_
Fluorite | Level 6

How would I test the code without SAS? Also when I try to save it as vbs, vbs does not show in the drop down menu. I just saved it with .vbs and it seemed to work.

Reeza
Super User
Save the program as a .vbs file from NotePad and then run it by double clicking it.
Assumes Windows OS and that you have Word 2010+ installed.
data_null__
Jade | Level 19

CSCRIPT can be executed from a CMD window.

 

Capture.PNG

 

 

omka_
Fluorite | Level 6

Also what is the sFolder supposed to be ? Is that the path where the vbs file is being saved to ?

Reeza
Super User
Input files that need to be converted, in this program the converted PDFs are saved to the same folder.
Reeza
Super User
You may need to modify this line...
oWord.ActiveDocument.SaveAs oFile.path & "x", 17

17 is for PDF, but the x is because I used this initially to convert DOC to DOCX. You'll need to add .pdf instead.

For testing, just change x to .pdf and once you confirm it works you can fix the path and extension to be more of what you want.
omka_
Fluorite | Level 6

I changed what you said and I am still getting this error.

 

Script: C:\Users\ojoshi\Documents\temp.vbs

Line: 7

Char: 1

Error: Path not found

Code: 800A004C

Source: Microsoft VBScript runtime error

 

Maybe its the path that is incorrect?

Reeza
Super User
Post the full code you're trying to run.
omka_
Fluorite | Level 6
bRecursive = False
sFolder = "C:\Users\ojoshi\Documents\temp\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWord = CreateObject("Word.Application")
oWord.Visible = True

Set oFolder = oFSO.GetFolder(sFolder)
ConvertFolder(oFolder)
oWord.Quit

Sub ConvertFolder(oFldr)
    For Each oFile In oFldr.Files
        If LCase(oFSO.GetExtensionName(oFile.Name)) = "rtf" Then
            Set oDoc = oWord.Documents.Open(oFile.path)
            oWord.ActiveDocument.SaveAs oFile.path & ".pdf", 17
            oDoc.Close
        End If
    Next

    If bRecursive Then
        For Each oSubfolder In oFldr.Subfolders
            ConvertFolder oSubfolder
        Next
    End If
End Sub
Reeza
Super User
And when you double click this file, that's the error you get?
omka_
Fluorite | Level 6

Yes thats the error I am getting. I changed the path and the x to .pdf.

Reeza
Super User

I ran your exact code,changing just the path, in my Windows 10 and it worked perfectly.

 

delete_demo.pngThe path is to where your rtf files are stored?

omka_
Fluorite | Level 6

So the path to where the rtf is located is this:

 

C:\Users\ojoshi\Documents\sample

 

I added that in the line where the path is supposed to go now and it is still giving me the same error.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 33 replies
  • 7986 views
  • 8 likes
  • 7 in conversation