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.
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.
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.
CSCRIPT can be executed from a CMD window.
Also what is the sFolder supposed to be ? Is that the path where the vbs file is being saved to ?
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?
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
Yes thats the error I am getting. I changed the path and the x to .pdf.
I ran your exact code,changing just the path, in my Windows 10 and it worked perfectly.
The path is to where your rtf files are stored?
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.