Home All Groups Group Topic Archive Search About

Macro to Print Text to file in UTF-8

Author
16 Jun 2009 6:39 PM
jakeatkins via VBMonster.com
Hello,

I am trying to print the value of a cell in Excel to a file "temp.html".  The
button's macro looks like this:

Sub Text()
Dim i As Long
Open "c:\temp\Homepage.html" For Output As #1
For i = 1 To Selection.Cells.Count
Print #1, Selection.Cells(i).Value
Next i
Close 1
End Sub

I need to know what to add to this code so that the file is created as UTF-8
encoding.  I am trying to preserve foreign characters, and currently they are
all being saved as "???????????".

Thanks!

--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vb/200906/1

Author
16 Jun 2009 6:58 PM
Jeff Johnson
"jakeatkins via VBMonster.com" <u35973@uwe> wrote in message
news:97b2bf58dbec7@uwe...

> I am trying to print the value of a cell in Excel to a file "temp.html".
> The
> button's macro looks like this:

You'd be better served asking in microsoft.public.excel.programming. This
group is for full-blown Visual Basic.
Author
16 Jun 2009 7:39 PM
jakeatkins via VBMonster.com
Jeff Johnson wrote:
>> I am trying to print the value of a cell in Excel to a file "temp.html".
>> The
>> button's macro looks like this:
>
>You'd be better served asking in microsoft.public.excel.programming. This
>group is for full-blown Visual Basic.

Post answers here:
http://www.officekb.com/Uwe/Forum.aspx/excel-prog/153933/Macro-to-Print-Text-to-file-in-UTF-8


--
Message posted via http://www.vbmonster.com
Author
16 Jun 2009 8:16 PM
MikeD
"jakeatkins via VBMonster.com" <u35973@uwe> wrote in message news:97b345f50ed6a@uwe...
> Jeff Johnson wrote:
>>> I am trying to print the value of a cell in Excel to a file "temp.html".
>>> The
>>> button's macro looks like this:
>>
>>You'd be better served asking in microsoft.public.excel.programming. This
>>group is for full-blown Visual Basic.
>
> Post answers here:
> http://www.officekb.com/Uwe/Forum.aspx/excel-prog/153933/Macro-to-Print-Text-to-file-in-UTF-8
>


You telling us where to answer you at?  I don't think so.  That's not how newsgroups work.

You better read up on newsgroup ettiquette before you make any more posts!

--
Mike
Author
16 Jun 2009 8:59 PM
jakeatkins via VBMonster.com
MikeD wrote:
>>>> I am trying to print the value of a cell in Excel to a file "temp.html".
>>>> The
>[quoted text clipped - 5 lines]
>> Post answers here:
>> http://www.officekb.com/Uwe/Forum.aspx/excel-prog/153933/Macro-to-Print-Text-to-file-in-UTF-8
>
>You telling us where to answer you at?  I don't think so.  That's not how newsgroups work.
>
>You better read up on newsgroup ettiquette before you make any more posts!
>

I frankly don't care where you post your answers at.  I was simply following
the previous poster's suggestion to move it elsewhere.  I was following
common forum ettiquette by showing where this post was now continuing (in
it's proper forum).  If anyone else was looking for the answer here, like I
was, they could find where I moved it.

Someone needs to seriously chill out...

--
Message posted via VBMonster.com
http://www.vbmonster.com/Uwe/Forums.aspx/vb/200906/1
Author
17 Jun 2009 5:57 PM
Jeff Johnson
"jakeatkins via VBMonster.com" <u35973@uwe> wrote in message
news:97b3f87d72705@uwe...

> I frankly don't care where you post your answers at.  I was simply
> following
> the previous poster's suggestion to move it elsewhere.  I was following
> common forum ettiquette by showing where this post was now continuing (in
> it's proper forum).  If anyone else was looking for the answer here, like
> I
> was, they could find where I moved it.
>
> Someone needs to seriously chill out...

And you need to proofread. "Post answers at" can easily be interpreted as a
command in English. It's certainly how I read it, and I was ready to respond
like Mike did. An unambiguous statement would have been either "Answers
posted at" or "Discussion continues at."
Author
16 Jun 2009 10:05 PM
Bob Riemersma
"jakeatkins via VBMonster.com" <u35973@uwe> wrote in message
news:97b2bf58dbec7@uwe...
Show quoteHide quote
> Hello,
>
> I am trying to print the value of a cell in Excel to a file "temp.html".
> The
> button's macro looks like this:
>
> Sub Text()
> Dim i As Long
> Open "c:\temp\Homepage.html" For Output As #1
> For i = 1 To Selection.Cells.Count
> Print #1, Selection.Cells(i).Value
> Next i
> Close 1
> End Sub
>
> I need to know what to add to this code so that the file is created as
> UTF-8
> encoding.  I am trying to preserve foreign characters, and currently they
> are
> all being saved as "???????????".
>
> Thanks!

Whether VB6 or VBA you should be able to use something like:

'Reference to ADO 2.5 or later.
Dim stmUTF8 As ADODB.Stream
Dim I As Integer

Set stmUTF8 = New ADODB.Stream
With stmUTF8
    .Open
    .Type = adTypeText
    .Charset = "utf-8"
    .LineSeparator = adLF
    For I = 1 To 10
        .WriteText CStr(I), adWriteLine
    Next
    .SaveToFile "C:\temp\somefile.htm", adSaveCreateOverWrite
    .Close
End With

This example creates a file of 10 lines containing 1 through 10, in UTF-8
format using LF as the line delimiter. Untested "air code" though.