Solved

Saving a .txt file as .rtf ...

Posted on 2006-06-20
23
753 Views
Last Modified: 2012-06-27
Guys,

For reasons I won't bore you with, my little Access VBA app needs to create 3 versions of a compiled 'string' based file ...

1. a .txt version
2. a HTML version
3. a .rtf version

No problems with 1 & 2 ... but the .rtf version is causing me a problem.

I'm compiling my string-based document and saving it using FSO ... cheekily just changing the extension ... but the .rft 'knows' it is not true .rft ... that is having a knock-on effect for what I am actually using this for (pre-designing Autosigs. in Outlook, if you are interested)

Anyone know how I can save a .txt file as 'real' .rft ??

Thanks,

S.S.
0
Comment
Question by:SpencerSteel
  • 7
  • 6
  • 5
  • +4
23 Comments
 
LVL 7

Expert Comment

by:Jonathan Kelly
ID: 16944157
if you save the file as rtf rather than changing a txt extension - does that make a difference ?

maybe >> use ole to load it into word and save it as rtf


0
 

Author Comment

by:SpencerSteel
ID: 16944179
mmm ... thought this might be the solution ... anyone want to write the code for me for the 500 points, where "myString" is the body copy (sorry - really rushed here)

Cheers,

S.S.

0
 
LVL 7

Expert Comment

by:wesbird
ID: 16944581
Simple

Put the "Microsoft Rich Textbox Control 6 (SP6)" or whatever version you have on a hidden form.  Call it RTFEdit

Simply call it to convert txt to rtf like this:

RTFEdit.Text = txtPlainText
txtRTF = RTFEdit.TextRTF

e.g. input:

Use the Microsoft Rich Textbox Control

output:

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 Use the Microsoft Rich Textbox Control
\par }

0
 
LVL 18

Expert Comment

by:Richard Daneke
ID: 16944729
DoCmd.OutputTo ObjectType, ObjectName, OutputFormat, OutputFile, AutoStart

Where ObjectType can be acOutputFunction or acOutputStoredProcedure and
where AcFormat can be one of these acFormatASP, acFormatHTML, acFormatRTF, or acFormatTXT

0
 
LVL 18

Expert Comment

by:Richard Daneke
ID: 16944761
Your code to a function that constructs a string would be:

1. DoCmd.OutputTo acOutputFunction, "MyString", AcFormatTXT, ".\MyString", False
2. DoCmd.OutputTo acOutputFunction, "MyString", AcFormatHTML, ".\MyString", False   or
    DoCmd.OutputTo acOutputFunction, "MyString", AcFormatASP, ".\MyString", False
3. DoCmd.OutputTo acOutputFunction, "MyString", AcFormatRTF, ".\MyString", False
0
 
LVL 18

Expert Comment

by:Richard Daneke
ID: 16944777
Oops, Function name needed not string variable....

1. DoCmd.OutputTo acOutputFunction, "funMyString", AcFormatTXT, ".\MyString", False
2. DoCmd.OutputTo acOutputFunction, "funMyString", AcFormatHTML, ".\MyString", False   or
    DoCmd.OutputTo acOutputFunction, "funMyString", AcFormatASP, ".\MyString", False
3. DoCmd.OutputTo acOutputFunction, "funMyString", AcFormatRTF, ".\MyString", False
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16946388
I was going to suggest you save your txt in a table, then output that using DoCmd.OutputTo

Now DoDahD has already given you the various options and so you don't have to worry about creating specific types
0
 

Author Comment

by:SpencerSteel
ID: 16949148
Thanks DoDahD - I like the idea but I'm a bit stuck

I've assigned this code to a blank form button ...

   DoCmd.OutputTo acOutputFunction, fnCreateString, acFormatRTF, ".\MyString", False

where fnCreateString (for test purposes is)

   Public Function fnCreateString()

   fnCreateString = "blah blah blah!"

   End Function

But on clicking the button, Access is saying that it "can't find the object 'blah blah blah'" ...

I've obviously missed the point on this. I thought the idea was to 'OutputTo' the result of the function ... whereas Access is just trying to output the entire function ... how does that work?

Thanks,

S.S.

0
 

Author Comment

by:SpencerSteel
ID: 16949523
*sigh* ... been playing around with outputting queries (looks ugly, can't get rid of field name) and reports (does that whole 'exporting to...' flashing box thing (not good enough)

I've very frustrating. Would like to know if the Function,Module thing might help.

Thanks,

S.S.
0
 
LVL 18

Expert Comment

by:Richard Daneke
ID: 16950393
You are on the right track.

   Public Function fnCreateString()

   fnCreateString = "blah blah blah!"

   End Function
Redefines fnCreateString as "blah blah blah!"   The function needs to return a string.

   Public Function fnCreateString(strCreateString as String)

   Dim strCreateString as String
   strCreateString = "blah blah blah!"

   End Function

- D
0
 

Author Comment

by:SpencerSteel
ID: 16951043
Argh ! I must be really thick ... i'm still getting an error on the call ... what is wrong with this ?

---------

DoCmd.OutputTo acOutputFunction, fnCreateString(), acFormatRTF, "c:\test.rtf", False

----------

 Public Function fnCreateString()

  Dim strCreateString As String
   
   strCreateString = "blah blah blah!"

   End Function

--------

Thanks so much,

S.S.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:Richard Daneke
ID: 16953836
S.S.
I, too, have had trouble using the acOutputFunction option.  I cannot find a good reference on its use.

Your string data is stored in a table? , query?, or what?

I have had good luck in testing with acOutputQuery and acOutputReport.  Is your string available in a different object?

- DD
0
 
LVL 18

Expert Comment

by:Richard Daneke
ID: 16953859
BTW,
I created a table, a query, and a report.
The query and table add column headings, the report can be created w/out headers or footers and will format for appearance (fonts, sizes, positions).  
Let me know your progress...
0
 
LVL 18

Expert Comment

by:Richard Daneke
ID: 16953937
S.S.
If you want to look at the database I was using follow this link:

Direct link to your file
https://filedb.experts-exchange.com/incoming/ee-stuff/240-EE-Test.zip

The function trash was my attempt to follow my own advice  :-(

Leaving the filename option blank in OutputTo prompts for a filename.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16954586
Have u tried linking your document to Access as a table?
File/Get External Data

then outputto that table to the various formats
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 17040033
DoDahD,

Check this link, it has interesting info on what make a file an .rtf File.

http://www.experts-exchange.com/Databases/MS_Access/Q_21606146.html#15151642

Hope this helps as well
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 17304485
ok, never giving up (well try not to) on a challenge
Ive been looking at how we can do conversions in VBA and Word Automation just clicked to me.
This code does depend on Word being installed, but then if u got MSAccess, there is a very high chance that word is installed

What I do is open txt file in Word, then save as RTF format

Public Sub Txt2RTF()

    Dim objWord As Object
    Dim objDoc As Object
   
   
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Open("c:\ee\dd.txt")
    objDoc.SaveAs "c:\ee\dd.rtf", 6
    objDoc.Close
    objWord.Quit
    Set objDoc = Nothing
    Set objWord = Nothing
   
End Sub



I have tested this on a couple of files and found it is producing a file with RTF tags
So this is my proposed solution.
I also use late binding so there is never a issue with references and different office versions!

0
 

Author Comment

by:SpencerSteel
ID: 17309198
Hello Rockiroads,

You really don't give up, do you !

Sadly, I'm right slap-bang in the middle of an absolute nightmare of a (non-development) issue at the moment and am going to struggle to test this out right now, but I trust you!

I'm going to add it to my To Do list and make sure I check it out asap and award you your hard-earned points !

Just so you know I haven't disppeared into an EE blackhole

Thanks for caring :)

S.S.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17311654
S.S.
No worries. Twas a good challenging question, one hopefully I have sorted for you.
Any problems let me know. I dont plan on going anywhere quite just yet, well perhaps next week, if and when I win the lottery :)

Miriam, is it okay to leave this question open a bit longer?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 17311764
No problem...

S.S.,  Just please follow up on this within the next 21 days so that we know that you are still interested in the question.

Thanks,

mbizup
EE Cleanup Volunteer


0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17311815
thanks
0
 

Author Comment

by:SpencerSteel
ID: 17419314
Sorted ! We got there ... thanks Rockiroads, worked very well !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now