Solved

Handling Notepad Using VBA from Excel

Posted on 2006-10-19
1
2,190 Views
Last Modified: 2008-02-01
I am trying to send a cut-list from excel to a program called genesis/evolution on my CNC machine.  The file is to be read from a folder called "LSX". The file extension is ".LST".

The list will not open in Genesis if it is in excel ".csv" format.  However, I found that it will work if I:
1) Right click on the excel file (in .csv format) icon and "open with" Notepad (also appears with .csv extension)
2) Make some changes to the text in Notepad:
     a) The file appears as follows once opened in Notepad:
                    LST,1,,,,,,,,,,,,,,,,,                                                                                     (line1)
                    text1,text2,text3,text4,text5,text6,text7  etc.                                                (line2)    
                    text1,text2,text3,text4,text5,text6,text7  etc.                                                (line3) etc.
     b) I need to delete all of the extra commas at the end of the first line so I'm left with just:   LST,1,
     c) Finally I need to do a "Find" and "Replace" to change all of the remaining commas to semicolons.  The file will not open if it is delineated with commas.
3) "Save As" from Notepad with a ".txt" extension and close the open Notepad window.
4) Right click and "Rename" the file with the extension ".LST".
                           I can now open this file successfully from the above mentioned "LSX" folder
                           - note that if I save directly with an ".LST" extension and skip the ".txt" part at step #3, it does not work.

Could I please get some help in automating this sequence from excel using VBA?

I understand that it might not be possible to manipulate Notepad with VBA and an alternative approach may be necessary.
I am not  a programmer, and as much coding help as possible would be greatly appreciated.            
0
Comment
Question by:ssumb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 500 total points
ID: 17770483
Give this macro a try. adjust the necessary parts as noted:


Sub ExportToLST()
Dim ExportFile As String
Dim ff As Integer
Dim MaxColumns As Long
Dim MaxRows As Long
Dim strRow As String
Dim x As Long
Dim y As Long

' set your filename here. If it exists it will be overwritten
ExportFile = "C:\MyNewFile.LST"

ff = FreeFile
Open ExportFile For Output As #ff

' adjust sheet name here
Set ws = ThisWorkbook.Sheets("Sheet1")

' adjust cols and rows to check
MaxColumns = 24
MaxRows = 65000

' write first row
For y = 1 To MaxColumns
    If y = 1 Then
        If Trim$(ws.Cells(1, y)) <> "" Then strRow = ws.Cells(1, y)
    Else
        If Trim$(ws.Cells(1, y)) <> "" Then strRow = strRow & "," & ws.Cells(1, y)
    End If
Next y

' print to file
Print #ff, strRow

' get rest of rows & cols
For x = 2 To MaxRows
    For y = 1 To MaxColumns
        If y = 1 Then
            If Trim$(ws.Cells(x, y)) <> "" Then strRow = ws.Cells(x, y)
        Else
            If Trim$(ws.Cells(x, y)) <> "" Then strRow = strRow & ";" & ws.Cells(x, y)
        End If
    Next y
   
    ' print to file
    Print #ff, strRow
   
' get next row
Next x

Close #ff

MsgBox "File is Complete!", vbExclamation, "Process Finished"

End Sub
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 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