- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Assumes Windows OS and that you have Word 2010+ installed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
CSCRIPT can be executed from a CMD window.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Also what is the sFolder supposed to be ? Is that the path where the vbs file is being saved to ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes thats the error I am getting. I changed the path and the x to .pdf.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.