Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8513973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:44:00+00:00 2026-06-11T04:44:00+00:00

The macro I created works fine, I just need to sort out the saving

  • 0

The macro I created works fine, I just need to sort out the saving business. Now I get a popup asking me where to save it, but I would like it to save it under a default name and path AND encoded in UTF-8.

This is my full code I use, the bottom part saves the document I presume.

Public Sub ExportToTextFile(FName As String, Sep As String, SelectionOnly As Boolean, AppendData As Boolean)
    Dim WholeLine As String
    Dim fnum As Integer
    Dim RowNdx As Long
    Dim ColNdx As Integer
    Dim StartRow As Long
    Dim EndRow As Long
    Dim StartCol As Integer
    Dim EndCol As Integer
    Dim CellValue As String
    Dim teller As Integer
    'Teller aangemaakt ter controle voor het aantal velden
    'teller = 1

    Application.ScreenUpdating = False
On Error GoTo EndMacro:
    fnum = FreeFile
    If SelectionOnly = True Then
        With Selection
            StartRow = .Cells(1).Row
            StartCol = .Cells(26).Column
            EndRow = .Cells(.Cells.Count).Row
            EndCol = .Cells(.Cells.Count).Column
        End With
    Else
        With ActiveSheet.UsedRange
            StartRow = .Cells(1).Row
            StartCol = .Cells(26).Column
            EndRow = .Cells(.Cells.Count).Row
            EndCol = .Cells(26).Column
        End With

    End If
    If AppendData = True Then
        Open FName For Append Access Write As #fnum
    Else
        Open FName For Output Access Write As #fnum
    End If
    For RowNdx = StartRow To EndRow
        WholeLine = ""
        For ColNdx = StartCol To EndCol
            If Cells(RowNdx, ColNdx).Value = "" Then
                CellValue = ""
            Else
                CellValue = Cells(RowNdx, ColNdx).Value
            End If
            WholeLine = WholeLine & CellValue & Sep
        Next ColNdx
        WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
        Print #fnum, WholeLine, ""
        'Print #fnum, teller, WholeLine, ""
        'teller = teller + 1

    Next RowNdx

EndMacro:
    On Error GoTo 0
    Application.ScreenUpdating = True
    Close #fnum
End Sub

Sub Dump4Mini()
    Dim FileName As Variant
    Dim Sep As String

    FileName = Application.GetSaveAsFilename(InitialFileName:=Blank, filefilter:="Text (*.txt),*.txt")

    If FileName = False Then
        Exit Sub
    End If
    Sep = "|"
    If Sep = vbNullString Then
        Exit Sub
    End If
    Debug.Print "FileName: " & FileName, "Separator: " & Sep
    ExportToTextFile FName:=CStr(FileName), Sep:=CStr(Sep), SelectionOnly:=False, AppendData:=False
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:44:01+00:00Added an answer on June 11, 2026 at 4:44 am

    This is what I use to pass http webpages and it returns a string with the correct encoding

    Public Function UTF8(ByVal http As Object) As String
    Dim BinaryStream
    
    Const adTypeBinary = 1
    Const adTypeText = 2
    Const adModeReadWrite = 3
    
     Set BinaryStream = CreateObject("ADODB.Stream")
    
     With BinaryStream
        .Type = adTypeBinary
        .Open
        .Write http.responseBody
    
        'Change stream type To binary
        .Position = 0
        .Type = adTypeText
    
        'Specify charset For the source text
        '.Charset = "iso-8859-1" 'unicode
        .Charset = "utf-8" 'or utf-16
    
        'Open the stream And get binary data from the object
        UTF8 = .ReadText
    End With
    End Function
    

    Where http in this case is something like Set http = CreateObject("Microsoft.XMLHTTP") but I’m sure you can adapt to fit your needs.

    This works with strings and outputs text file directly

    Option Explicit
    
    Sub test()
    Dim filePath As String
    Dim fileName As String
    Dim charToEncode As String
    Dim success As Boolean
    
        filePath = "C:\Users\ooo\Desktop\"
        fileName = "test.txt"
        charToEncode = "Télécom"
    
        success = ConvertToUTF8thenSaveToFile(charToEncode, filePath, fileName)
    
        If success Then
            MsgBox ("Success")
        Else
            MsgBox ("Failed")
        End If
    End Sub
    
    Function ConvertToUTF8thenSaveToFile(ByVal charToEncode As String, _
        ByVal filePath As String, ByVal fileName As String) As Boolean
    
        Dim fsT As Object
        Dim adodbStream  As Object
    
        On Error GoTo Err:
        Set adodbStream = CreateObject("ADODB.Stream")
        With adodbStream
            .Type = 2 'Stream type
            .Charset = "utf-8" 'or utf-16 etc
            .Open
            .WriteText charToEncode
            .SaveToFile filePath & fileName, 2 'Save binary data To disk
        End With
    
        ConvertToUTF8thenSaveToFile = True
    
        On Error GoTo 0
    
        Exit Function
    
    Err:
    ConvertToUTF8thenSaveToFile = False
    
    End Function
    

    UPDATE: below code has been updated to create delimited string from a range, encode the string and save to a file.

    Option Explicit
    
    Sub test()
    Dim filePath As String
    Dim fileName As String
    Dim charToEncode As String
    Dim encodingType As String
    Dim success As Boolean
    Dim rngArray() As Variant
    
    
        filePath = "C:\Users\ooo\Desktop\"
        fileName = "test.csv"
        rngArray = Sheet1.Range("A1:E10000").Value
        encodingType = "utf-8"
    
        charToEncode = DelimitRange(rngArray)
        success = ConvertToUTF8thenSaveToFile(charToEncode, filePath, fileName, encodingType)
    
        If success Then
            MsgBox ("Success")
        Else
            MsgBox ("Failed")
        End If
    End Sub
    
    Function ConvertToUTF8thenSaveToFile(ByVal charToEncode As String, _
        ByVal filePath As String, ByVal fileName As String, ByVal encodingCharSet As String) As Boolean
    
        Dim fsT As Object
        Dim adodbStream  As Object
    
        On Error GoTo Err:
        Set adodbStream = CreateObject("ADODB.Stream")
        With adodbStream
            .Type = 2 'Stream type
            .Charset = encodingCharSet 'or utf-16 etc
            .Open
            .WriteText charToEncode
            .SaveToFile filePath & fileName, 2 'Save binary data To disk
        End With
    
        ConvertToUTF8thenSaveToFile = True
    
        On Error GoTo 0
    
        Exit Function
    
    Err:
    ConvertToUTF8thenSaveToFile = False
    
    End Function
    
    Function DelimitRange(ByVal XLArray As Variant) As String
    Const delimiter As String = ","
    Const lineFeed As String = vbCrLf
    Const removeExisitingDelimiter As Boolean = True
    Dim rowCount As Long
    Dim colCount As Long
    Dim tempString As String
    
    
        For rowCount = LBound(XLArray, 1) To UBound(XLArray, 1)
            For colCount = LBound(XLArray, 2) To UBound(XLArray, 2)
    
                If removeExisitingDelimiter Then
                    tempString = tempString & Replace(XLArray(rowCount, colCount), delimiter, vbNullString)
                Else
                    tempString = tempString & XLArray(rowCount, colCount)
                End If
    
                'Don't add delimiter to column end
                If colCount < UBound(XLArray, 2) Then tempString = tempString & delimiter
    
            Next colCount
    
            'Add linefeed
            If rowCount < UBound(XLArray, 1) Then tempString = tempString & lineFeed
    
        Next rowCount
    
        DelimitRange = tempString
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a macro to just get what was in the tags of each
I've created latex macro to typeset guitar chords diagrams(using picture environment). Now I want
I have created a xlt excel template which works fine in Excel 2007 under
I'm confused about how defun macro works, because (defun x () hello) will create
I have created a macro for excel which will pop up form that contains
I'm having trouble obtaining data with a macro I've created using XSLT. I have
So I've created a simple Macro to: un-hide a worksheet. find and replace text.
Please help a macro beginner... I created a simple macro for loading images and
Using primarily the macro recorder, I created a VBA macro that sets up a
I have created a simple excel worksheet. This is my macro code: Sub MyMacro()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.