+ Reply to Thread
Results 1 to 3 of 3

convert excel workbook to pipe delimited csv file using vba

Hybrid View

aman1234 convert excel workbook to... 03-27-2020, 01:14 PM
Paul Re: convert excel workbook to... 03-27-2020, 02:33 PM
LJMetzger Re: convert excel workbook to... 06-14-2020, 02:31 PM
  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    883

    convert excel workbook to pipe delimited csv file using vba

    Hi guys,

    The below code extracts outlook email contents to excel workbook but I want to save the workbook down in pipe delimited .txt file format.

    Can anyone please amend the code accordingly ?

    Sub Extract()
        On Error Resume Next
        Set myOlApp = Outlook.Application
        Set mynamespace = myOlApp.GetNamespace("mapi")
        
        'open the current folder
    
        Set myfolder = myOlApp.ActiveExplorer.CurrentFolder
        Set xlobj = CreateObject("excel.application")
        xlobj.Visible = True
        xlobj.Workbooks.Add
        
        Dim objFS As New Scripting.FileSystemObject
        Dim objFile As Scripting.TextStream
        Dim FilePath As String
        'open the current folder
        Dim sFilePath As String
        Dim fileNumber As Integer
        'Set Heading
    
        For I = 1 To myfolder.Items.Count
        
        Set myitem = myfolder.Items(I)
        
        msgtext = myitem.Body
            
        'search for specific text
        
        delimtedMessage = Replace(msgtext, "Name", "###")
        delimtedMessage = Replace(delimtedMessage, "Contact Number", "###")
        delimtedMessage = Replace(delimtedMessage, "Address", "###")
        delimtedMessage = Replace(delimtedMessage, "Telephone Number", "###")
        delimtedMessage = Replace(delimtedMessage, "Email", "###")
        delimtedMessage = Replace(delimtedMessage, "Account number", "###")
        delimtedMessage = Replace(delimtedMessage, "Hobbies", "###")
        delimtedMessage = Replace(delimtedMessage, "DOB", "###")
        delimtedMessage = Replace(delimtedMessage, "University", "###")
        delimtedMessage = Replace(delimtedMessage, "Subjects", "###")
        delimtedMessage = Replace(delimtedMessage, "Score", "###")
        messageArray = Split(delimtedMessage, "###")
        
    
        'write to excel
        
        xlobj.Range("a" & I + 1).Value = messageArray(1)
        xlobj.Range("b" & I + 1).Value = messageArray(2)
        xlobj.Range("c" & I + 1).Value = messageArray(3)
        xlobj.Range("d" & I + 1).Value = messageArray(4)
        xlobj.Range("e" & I + 1).Value = messageArray(5)
        xlobj.Range("f" & I + 1).Value = messageArray(6)
        xlobj.Range("g" & I + 1).Value = messageArray(7)
        xlobj.Range("h" & I + 1).Value = messageArray(8)
        xlobj.Range("i" & I + 1).Value = messageArray(9)
        xlobj.Range("j" & I + 1).Value = messageArray(10)
        xlobj.Range("k" & I + 1).Value = messageArray(11)
        
        myPath = "C:\test\"
        myFileName = "a" & Format(Now, "ddmmyyyyhhmmss")
        xlobj.SaveAs FileName:=myPath & sFilePath & ".csv", FileFormat:=xlCSV, Local:=True
        Next
        End Sub

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: convert excel workbook to pipe delimited csv file using vba

    I'm guessing you'd have to either:
    1. Write the code to join your cell values with the pipe symbol and then export that data to a text file; or
    2. Change your regional list separator settings to use a pipe instead of a comma, save the file as "CSV", then change your list separator back. The CSV format uses your system's regional list separator character in the file, even though it's called a 'Comma Separated Value' file.

    To do #2, in Win10 go to Control Panel > Clock and Region > Change date, time, or number formats > Additional settings... > List separator. Of course, if others receive the pipe-delimited file, they'll need to use Text-to-Columns when opening in Excel since they're list separator is likely going to be comma.

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: convert excel workbook to pipe delimited csv file using vba

    Hi,

    I had to simulate the data, since you got your data from a file structure. Important parts of the code are highlighted in red.
    Your code can be simplified by just replacing '###' with the 'Pipe Character'.

    Try code like the following
    Sub ExtractSimulation()
        
      Dim myNewWorkbook As Workbook
      Dim xlobj As Object
      Dim iRow As Long
      Dim delimtedMessage As String
        
      Dim sPath As String
      Dim sFileName As String
      Dim sPathAndFileNameCombination As String
        
        
      Set xlobj = CreateObject("excel.application")
      xlobj.Visible = True
      Set myNewWorkbook = xlobj.Workbooks.Add
      
      iRow = 0
      
      delimtedMessage = "abc###def###201-555-5555###03/27/1988###88###None"
      delimtedMessage = Replace(delimtedMessage, "###", "|")
      iRow = iRow + 1
      xlobj.Cells(iRow, "A").Value = delimtedMessage
        
      delimtedMessage = "def###def###201-555-5555###03/27/1988###88###None"
      delimtedMessage = Replace(delimtedMessage, "###", "|")
      iRow = iRow + 1
      xlobj.Cells(iRow, "A").Value = delimtedMessage
        
      sPath = "C:\test\"
      sPath = ThisWorkbook.Path & "\"    'DELETE THIS LINE - I don't write directly outside my Login
      sFileName = "a" & Format(Now, "ddmmyyyyhhmmss") & ".csv"
      sPathAndFileNameCombination = sPath & sFileName
      
      'Reference: https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.saveas
      'Reference: https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat
      myNewWorkbook.SaveAs Filename:=sPathAndFileNameCombination, FileFormat:=xlTextPrinter, Local:=True
      
      'Clear Object Pointers
      Set xlobj = Nothing
      Set myNewWorkbook = Nothing
        
    End Sub
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Closing a excel file as Pipe delimited txt
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-02-2016, 12:09 PM
  2. Convert .xls to pipe-delimited .txt file
    By Sandi99 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-30-2014, 12:54 PM
  3. Combine Excel Sheets (+2 Million Rows total) and Create Pipe Delimited TEXT File
    By kestefon in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-05-2013, 08:51 PM
  4. [SOLVED] convert .xls to pipe delimited -- but blank rows at last are also being considered
    By vishalbit04 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-31-2012, 02:39 AM
  5. Convert Pipe Delimited Text File into CSV File
    By sachin29feb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-28-2012, 10:47 AM
  6. need macro to create pipe delimited text file from excel
    By nefkho in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-04-2012, 12:03 PM
  7. How can I convert tab delimited files to pipe delimited?
    By Jeremy Town in forum Excel General
    Replies: 1
    Last Post: 01-23-2006, 11:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1