+ Reply to Thread
Results 1 to 15 of 15

Open txt-file after Macro creating it

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Open txt-file after Macro creating it

    Hello


    I have a macro that creates a txt-file and saves it.

    How can I have the macro open that txt-file once it created and saved it ?


    Sub ExportToTextFile()
        Dim a       As Variant
        Dim s       As String
        Dim r       As Range
        Dim rng     As Range
        Dim C       As Range
        Dim i       As Long
        Dim k       As Long
        Dim cl      As Long
    
        ReDim a(1 To 1)
        s = "C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\"
        cl = 25
        k = 1
    
        For i = 3 To Cells(Rows.Count, 1).End(xlUp).Row
            If i = 3 Then Set r = Cells(i, 1)
    
            If Cells(i, 1).Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
                ReDim Preserve a(1 To k)
                a(k) = "c:\(" & Cells(1, cl).Value & ").printer"
                Set rng = Range(r, Cells(i, 1))
    
                For Each C In rng
                    k = k + 1
                    ReDim Preserve a(1 To k)
                    a(k) = CStr(s & C.Value & ".jpg")
                Next C
    
                Set r = Cells(i + 1, 1)
                cl = cl + 1: k = k + 1
            End If
        Next i
    
        ArrayToTextFile a, "C:\Users\User\Documents\Spaces\TD1\" & "\Print.job"
    
        MsgBox "Done...", 64
    End Sub

    Thanks !

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open txt-file after Macro creating it

    Hello
    Try this method
    Sub Open_Text_File_Using_Shell()
        Dim o       As Object
        Dim s       As String
    
        Set o = CreateObject("Shell.Application")
        s = ThisWorkbook.Path & "\Sample.txt"
        o.Open (s)
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    Check this screenshot:

    Screenshot 2017-09-18 13.51.04.png


    You notice those little "o" in Column B. These "o" mark Black&White items.

    The macro that you wrote a few weeks ago would need to be extended with a feature, that, whenever there is an item with an "o" in B, there should be the following line written to the text file before that:


    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\192sw.prs

    and after that, the line


    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\192.prs

    These are Printer settings, so it switches the printer's settings to black&white, and then back to normal.

    I will attach a sample text file, so you can see.
    Attached Files Attached Files
    Last edited by elgato74; 09-18-2017 at 09:10 AM.

  4. #4
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    +++ deleted +++
    Last edited by elgato74; 09-18-2017 at 09:11 AM.

  5. #5
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    Hi YasserKahlil,

    thanks for that. Did you get my PM ?

    The above code needs to be extended with a feature, can you help ?

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open txt-file after Macro creating it

    Hello
    Try this code
    Sub ExportToTextFile()
        Dim a       As Variant
        Dim s       As String
        Dim r       As Range
        Dim rng     As Range
        Dim c       As Range
        Dim i       As Long
        Dim k       As Long
        Dim cl      As Long
    
        ReDim a(1 To 1)
        s = "C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\"
        cl = 25
        k = 1
    
        For i = 3 To Cells(Rows.Count, 1).End(xlUp).Row
            If i = 3 Then Set r = Cells(i, 1)
    
            If Cells(i, 1).Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
                ReDim Preserve a(1 To k)
                a(k) = "c:\(" & Cells(1, cl).Value & ").printer"
                Set rng = Range(r, Cells(i, 1))
    
                For Each c In rng
                    If c.Offset(, 1).Value = "o" And c.Offset(-1, 1).Value <> "o" Then
                        k = k + 1
                        ReDim Preserve a(1 To k)
                        a(k) = CStr(s & Cells(1, cl).Value & "sw.prs")
                    End If
                    
                    k = k + 1
                    ReDim Preserve a(1 To k)
                    a(k) = CStr(s & c.Value & ".jpg")
                    
                    If c.Offset(, 1).Value = "o" And c.Offset(1, 1).Value <> "o" Then
                        k = k + 1
                        ReDim Preserve a(1 To k)
                        a(k) = CStr(s & Cells(1, cl).Value & ".prs")
                    End If
                Next c
    
                Set r = Cells(i + 1, 1)
                cl = cl + 1: k = k + 1
            End If
        Next i
        
        ArrayToTextFile a, "C:\Users\User\Documents\Spaces\TD1" & "\Print.txt"
    
        MsgBox "Done...", 64
    End Sub
    
    Function ArrayToTextFile(a, strPath)
        Const forWriting = 2
    
        Dim fso, writer
        Dim i As Long
    
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set writer = fso.OpenTextFile(strPath, forWriting, True)
    
        For i = LBound(a) To UBound(a)
            writer.writeline a(i)
        Next i
    
        writer.Close
    End Function

  7. #7
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    I just discovered one minor thing.

    It would be best to put the line

    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\192.prs

    to always start with printers' default settings, unless the first item is marked with "o" - then the *sw.prs should appear.

    Everything else should remain the same.

  8. #8
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    Perfect !! I need to test it some more, but it seems to work really well. Thank you!

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open txt-file after Macro creating it

    I don't get the last point. Can you attach some of your expected output?

  10. #10
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    Hello


    Yes, I need to put the "normal" .prs in front of each batch, like shown above !

    Can you do this for me ?

  11. #11
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    here is the example

    192.prs is the normal printer settings file for color prints
    192sw.prs is the black&white printer settings file for the black&white prints

    If the last item for a printer is black&white, the printer stays that way.

    But I just noticed, that if the last item is marked with the "o" and the macro puts the 192sw.prs in front of it, it also puts a 192.prs after it. So all should be good.

    My idea was to put the normal printer setting file in front of each printer like this:


    c:\(194).printer
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\194.prs
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\ASH-1P.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\DIS-4LP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\EHZ-4LP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\DRK-3P.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\BEZ-MFP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\ACR-MFP.jpg

    c:\(196).printer
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\196.prs
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\BMC-1K.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\Magic Cat 1K.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\Roulette Pano.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\Ali MFP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\Bulli V3 MFP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\FCQ-MFP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\CFP-5P.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\ETI-MFP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\ALK-Pano.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\DTB-3P.jpg

    c:\(200).printer
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\200.prs
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\BLS-4erP.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\200sw.prs
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\Marilyn V8 3P.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\Pacino 3P.jpg
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\200.prs
    C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\Venice V4 1P.jpg
    Last edited by elgato74; 09-18-2017 at 01:03 PM.

  12. #12
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    great, that looks right !

    I will test it tomorrow

    Have a good evening, and thank you again!

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open txt-file after Macro creating it

    Give this a try
    Sub ExportToTextFile()
        Dim a       As Variant
        Dim s       As String
        Dim r       As Range
        Dim rng     As Range
        Dim c       As Range
        Dim i       As Long
        Dim k       As Long
        Dim cl      As Long
    
        ReDim a(1 To 1)
        s = "C:\Dokumente und Einstellungen\Felix\Eigene Dateien\PRODUCTION\"
        cl = 25
        k = 1
    
        For i = 3 To Cells(Rows.Count, 1).End(xlUp).Row
            If i = 3 Then Set r = Cells(i, 1)
    
            If Cells(i, 1).Borders(xlEdgeBottom).LineStyle <> xlLineStyleNone Then
                ReDim Preserve a(1 To k)
                a(k) = "c:\(" & Cells(1, cl).Value & ").printer"
                Set rng = Range(r, Cells(i, 1))
                k = k + 1
                ReDim Preserve a(1 To k)
                a(k) = s & Cells(1, cl).Value & ".prs"
                
                For Each c In rng
                    If c.Offset(, 1).Value = "o" And c.Offset(-1, 1).Value <> "o" Then
                        k = k + 1
                        ReDim Preserve a(1 To k)
                        a(k) = CStr(s & Cells(1, cl).Value & "sw.prs")
                    End If
                    
                    k = k + 1
                    ReDim Preserve a(1 To k)
                    a(k) = CStr(s & c.Value & ".jpg")
                    
                    If c.Offset(, 1).Value = "o" And c.Offset(1, 1).Value <> "o" Then
                        k = k + 1
                        ReDim Preserve a(1 To k)
                        a(k) = CStr(s & Cells(1, cl).Value & ".prs")
                    End If
                Next c
    
                Set r = Cells(i + 1, 1)
                cl = cl + 1: k = k + 1
            End If
        Next i
        
        ArrayToTextFile a, "C:\Users\User\Documents\Spaces\TD1" & "\Print.txt"
    
        MsgBox "Done...", 64
    End Sub
    
    Function ArrayToTextFile(a, strPath)
        Const forWriting = 2
    
        Dim fso, writer
        Dim i As Long
    
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set writer = fso.OpenTextFile(strPath, forWriting, True)
    
        For i = LBound(a) To UBound(a)
            writer.writeline a(i)
        Next i
    
        writer.Close
    End Function

  14. #14
    Forum Contributor
    Join Date
    12-05-2008
    Location
    Germany
    MS-Off Ver
    365
    Posts
    580

    Re: Open txt-file after Macro creating it

    Works great ! Thanks !

  15. #15
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open txt-file after Macro creating it

    You're welcome. Glad I can offer some help

+ 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. [SOLVED] VBA Open File/Run Macro/Close &Save/Open Next File
    By JPSIMMON in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-21-2015, 12:16 PM
  2. Macro - Open File Explorer, Select File, Import certain information from file
    By Drayde in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2015, 08:58 AM
  3. Workbooks.open is creating a file if it does not exist
    By jkj115 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-22-2015, 10:06 AM
  4. Creating Code to open a file whose name changes by week
    By dafigu01 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-04-2014, 06:55 PM
  5. [SOLVED] Macro - Master file to import data from another open file with variable file name
    By jdodz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2012, 10:56 PM
  6. macro prompts immediately to open a file once the Excel file with the macro is opened
    By jhmayor03 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-03-2012, 10:00 AM
  7. Open a file do a macro ( made) and open next succesive file
    By SVTman74 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-21-2006, 05:20 PM

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