+ Reply to Thread
Results 1 to 6 of 6

Executing a SaveAs Command in an Open .txt File

Hybrid View

  1. #1
    Registered User
    Join Date
    12-11-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    3

    Executing a SaveAs Command in an Open .txt File

    Hello!

    I'm writing a Macro that writes the contents of the cells in the first column of a worksheet into an already existing .txt file on my desktop. All of that is working fine and dandy until I want to save the .txt file under a new name (See bolded code below). I'm getting an Object Required Error, probably because my syntax is incorrect. Anyone have any helpful corrections?

    Thank you!!

        Sheets("Blank").Select
        Range("A1").Select
        Selection.pastespecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        Dim r As Integer
        Open "Macintosh HD:Users:Name:Desktop:trial.txt" For Output As #1
            Sheets("Blank").Activate
               ii = Sheets("Blank").Cells(Rows.Count, 1).End(xlUp).Row
                For r = 1 To ii
                    data = Cells(r, 1)
                        Print #1, data
                Next r
            ActiveDocument.SaveAs Filename:="NewDocument.txt", _
            FileFormat:=wdFormatText
    
        Close
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you since it's your first post. --6SJ
    Last edited by HannahTee; 12-18-2013 at 03:49 PM. Reason: code tags

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,874

    Re: Executing a SaveAs Command in an Open .txt File

    Object Required is not a syntax error, it's a runtime error. It most likely means that ActiveDocument is Nothing. Based only on the code you are showing, I see you are opening a file as device #1. As you clearly understand from your code, you can Open, Close, and Print with this file. However, ActiveDocument is a Word object. It has nothing at all to do with a file accessed with the Open statement. They are two completely different animals.

    If you describe in a little more detail the steps you want to take with this file, I might be able to recommend a way to accomplish it, although I do not experience with Office or VBA on a Mac. Do you want to make an exact copy of the file after you write to it? Or do you want to save the changes as a new file, leaving the original the way it was before you wrote to it? There is not a way to directly save an I/O file with a new name but there are other commands you can use to rename it (which I suppose would also work on a Mac).

    Is this macro in a Word file, or Excel? This is posted to an Excel forum.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    12-11-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Executing a SaveAs Command in an Open .txt File

    Hi 6StringJazzer,

    Thank you so much for your response!

    This is in fact an Excel Macro written in VBA that writes the text of several cells from an excel worksheet into the trial.txt file. I would like to make a copy of the trial.txt file and then save the exact copy under a different name. Can you advise?

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,874

    Re: Executing a SaveAs Command in an Open .txt File

    You are simply copying column A into the text file. Is there other data in the worksheet? If not you can simply save the Excel file as a text file without having to copy data. Otherwise you can try this:

        Sheets("Blank").Select
        Range("A1").Select
        Selection.pastespecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        Dim r As Integer
        Open  For Output As #1
            Sheets("Blank").Activate
               ii = Sheets("Blank").Cells(Rows.Count, 1).End(xlUp).Row
                For r = 1 To ii
                    data = Cells(r, 1)
                        Print #1, data
                Next r
        Close
    
        FileCopy "Macintosh HD:Users:Name:Desktop:trial.txt", "Macintosh HD:Users:Name:Desktop:NewDocument.txt"

  5. #5
    Registered User
    Join Date
    12-11-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Executing a SaveAs Command in an Open .txt File

    Thank you so much! Such a simple line of code but that's exactly what I was looking for!

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,874

    Re: Executing a SaveAs Command in an Open .txt File

    Glad to help! Nice new username, by the way

+ 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. Problem With Open File and Save File With Saveas and then close the File
    By John Vieren in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-27-2013, 12:53 AM
  2. [SOLVED] Open and SaveAs an .xlsx file to a .csv
    By DJBittner in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2012, 03:15 PM
  3. Open file after workbook executing code is closed
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2011, 10:05 PM
  4. Macro: SaveAs: how to keep file open after saved?
    By trillium in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2010, 04:38 PM
  5. [SOLVED] Open Excel with a XML file and SaveAs XLS
    By Li Pang in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-03-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