BookmarkSubscribeRSS Feed
omka_
Fluorite | Level 6
bRecursive = False
sFolder = "C:\Users\ojoshi\Documents\sample"
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

This is the code I am using now. I still don't think the path is correct because I am getting the same error. the rtf file is called sample.

Reeza
Super User
So when you paste that path into Windows Explorer, does it go to that folder? The path not found is a weird error. You are using Windows?
omka_
Fluorite | Level 6
yes it goes to that folder. Yeah I am confused as well. I am doing the same thing you are doing.
omka_
Fluorite | Level 6
what did you change the path to??
Reeza
Super User
The folder I had the RTF files in I want to convert and was testing, in my case that was "C:\localdata\"

I tried with and without the \ at the end and it works either way.

I put the script in the same folder but I don't think that usually matters.
omka_
Fluorite | Level 6
okay well I have tried different folders as well and it's still not working. What was the other method you mentioned? DDE I think?
Tom
Super User Tom
Super User

It looks like that program is trying to loop over the files itself.

Can you get it to run directly on the machine where SAS is executing by just running from the command line?  That is without using SAS at all.

 

If not then perhaps you can try just converting ONE file.

 

Can you follow these debugging steps.

1) Create a program to convert one file.

2) Run the program from the command line (on the machine where SAS runs).

3) Run the program from SAS using any of the many methods to call an operating system command from SAS code. (X, CALL SYSTEM, PIPE filename engine, etc.)

4) Try to generate a new version of the program from SAS code and test that it also runs.

blueskyxyz
Lapis Lazuli | Level 10
it can work on my PC
smol
Fluorite | Level 6

Hi 

How do I change filename to remove .rtf from .pdf file created?

 

Thanks

 

Reeza
Super User

oFile.path is the filename parameter being passed to the SaveAs function. You'd need to create a new variable without the extension and pass that to the SaveAs command.

 

Unfortunately don't have time to look into this, but some googling shows some code examples online. 

 

 

SaveAs

https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa220734(v=office.1...)

smol
Fluorite | Level 6
Thank you Reeza. I updated to add code "Filename= ofso.GetBaseName(oFile.name)" and changed ofile.path to point out location """C:\Users\userx\Desktop\test\"""
bRecursive = False
sFolder = "C:\Users\userx\Desktop\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
Filename= ofso.GetBaseName(oFile.name)
If LCase(oFSO.GetExtensionName(oFile.Name)) = "rtf" Then
Set oDoc = oWord.Documents.Open(oFile.path)
oWord.ActiveDocument.SaveAs "C:\Users\userx\Desktop\test\" & Filename & ".pdf", 17
oDoc.Close
End If
Next

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

@smol wrote:

Hi 

How do I change filename to remove .rtf from .pdf file created?

 

Thanks

 


You are better off starting your own thread. If you think this one is related then post the url as related.

Then provide the details of what you have attempted such as the code. With some description of your process there is no way for us to know where ".rtf" might be involved.

omka_
Fluorite | Level 6

Hi, I appreciate your help the other day but whatever you were explaining wasn't working on my end. Can you explain how to use DDE to do the same thing? Thanks.

Reeza
Super User
Unfortunately not, I no longer have a system where SAS can run DDE to test anything.
Reeza
Super User
When you go into the command line interface or program make sure it starts in C drive. If it starts elsewhere it will likely error out.

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
  • 7720 views
  • 8 likes
  • 7 in conversation