|
-
December 1st, 2004, 09:57 AM
#1
save a text file in a new format
Hello,
I have a program that generates a formatted text file that gets emailed automatically. The problem is that some of the recipients are using web based email and the file gets garbled for some reason not known to me.
I was told that if I could save the file as either a word document or an html document that everything would work fine.
So my question is, how can I programmatically save this text file as either a word document or an html document.
Thanks so much,
Stephanie
-
December 1st, 2004, 12:47 PM
#2
Re: save a text file in a new format
Why don't you put a simple html file together?
Code:
Dim html as String
html = "<html>" + myText + "</html>"
You may add some additional html tags to change font, color etc.
Or did I catch your problem wrong?
-
December 1st, 2004, 01:26 PM
#3
Re: save a text file in a new format
Note that this solution uses Outlook to actually send the document, but it also saves to a file that can be used as an attachment for any method of emailing.
Code:
'Declarations
'Add a reference to the Word object library
'Add a reference to the Outlook object library
Private bOldWord As Boolean
Private oWord As Word.Application
Private Function PostProcess(sString As String, sSendTo As String) As Boolean
'sString argument is for what you want to email.
'sSendTo is email address.
Dim oDocOut As Word.Document
Dim oRange As Word.Range
Dim sDocName As String
Dim oOutlook As Outlook.Application 'Outlook objects.
Dim oEmail As Outlook.MailItem ' "
Dim oRecip As Outlook.Recipient ' "
If oWord Is Nothing Then
If AttachWord = False Then
Set oWord = New Word.Application
End If
End If
If bOldWord = True Then
Set oDocOut = oWord.Documents.Add(, , , False) 'Last arg here is the .Visible property
Else
Set oDocOut = oWord.ActiveDocument 'We opened this one, so can use the default empty doc.
End If
Set oRange = oDocOut.Paragraphs(1).Range
With oRange 'Send your text.
.Text = sString
.Font.Name = "Times New Roman"
'etc.
End With
Set oRange = Nothing
sDocName = Application.ActiveWorkbook.Path & "\Some.doc" 'Need to save temporarily to attach it to email.
Call oDocOut.SaveAs(sDocName, wdFormatDocument)
If bOldWord = True Then 'Close Word, but don't mess with currently open aps ;)
oDocOut.Close (wdDoNotSaveChanges)
Else
oWord.Quit (wdDoNotSaveChanges)
End If
Set oDocOut = Nothing
Set oWord = Nothing
Set oOutlook = New Outlook.Application 'Email it
Set oEmail = oOutlook.CreateItem(olMailItem)
With oEmail
' Set oRecip = .Recipients.Add("[email protected]") 'CC yourself?
' oRecip.Type = olCC
.Recipients.Add (sSendTo)
.Subject = "Here is Some.doc"
Call .Attachments.Add(sDocName, olByValue, 1)
.Send
End With
Set oRecip = Nothing
Set oEmail = Nothing
Set oOutlook = Nothing
Kill (sDocName) 'Kill the temp copy
PostProcess = True
End Function
Private Function AttachWord() As Boolean
'This function checks to see if word is already running, and uses that instance if it is.
'bOldWord is used in PostProcess() to determine whether to quit Word when done. Keeps the
'code from killing other open documents.
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWord = Nothing
Err.Clear
Else
AttachWord = True
bOldWord = True
End If
End Function
You can also make templates to use here too. I've found it easiest to make a .doc document (as opposed to .dot) and fill it with bookmarks at the insertion points and saving it as read only. Then you can do something like this:
Code:
'. . . [Stuff ]
'This method opens the template:
Set oDocOut = oWord.Documents.Open("Template.doc")
'. . . [Other.Stuff]
With oDocOut
.Bookmarks("One").Range.Text = sOne
.Bookmarks("Two").Range.Text = sTwo
.Bookmarks("Three").Range.Text = sThree
.Bookmarks("Etc").Range.Text = sEtc
End With
-
December 1st, 2004, 03:26 PM
#4
Re: save a text file in a new format
The problem may not be the file, but the method being used to send it. What sort of formatting are you using, and how are you sending it? Are you MIME encoding it?
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|