+ Reply to Thread
Results 1 to 14 of 14

How to paste from clipboard to already existing text file ?

  1. #1
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    How to paste from clipboard to already existing text file ?

    Hello Forum members,

    I am wondering if there is a possible way to paste the data from clipboard to a already existing text file after deleting the old text in it and save it with VBA?
    I have a text file Data.txt at the location C:\MyData\Data.txt. What I want is to first delete the existing text of the file and then paste the data present in the clipboard and save it even without opening the text file in the whole process.

    Is it possible or I am asking something silly?

    Thanks in advance.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: How to paste from clipboard to already existing text file ?

    Why not delete file and create it again ?
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to paste from clipboard to already existing text file ?

    Quote Originally Posted by patel45 View Post
    Why not delete file and create it again ?
    Sorry to say that but didn't expect such a reply from a Valued Forum Contributor.

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: How to paste from clipboard to already existing text file ?

    What I want is to first delete the existing text of the file and then paste the data present in the clipboard and save it even without opening the text file in the whole process.
    my answer is:
    Delete file, then create a new one with the contents of clipboard

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    What Patel45 has suggested is the only to do this.
    If posting code please use code tags, see here.

  6. #6
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to paste from clipboard to already existing text file ?

    Quote Originally Posted by Norie View Post
    What Patel45 has suggested is the only to do this.
    Yeah, I understood what Patel45 said but couldn't believe it because I thought VBA had a solution to this. I have searched google and other forums as well and found that there are experts addressing the issue closely related to my query. But their solutions, codes and requirements were really very much complicated and beyond my capability to understand. So I thought it would be better if I submit a query in this ExcelForum as I have lots of respect for the experts over here and I have always found the answers to my queries.

    I still believe that VBA has the solution to my query. Lets see if a master of VBA suggests a possible way to do it.
    So lets hope that someone would be able to answer it.

    Thanks & Regards.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Who said what has been suggested isn't a VBA solution?

  8. #8
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: How to paste from clipboard to already existing text file ?

    hi sktneer, I do not understand refusal to delete the file if its content is deleted, option as you want it without deleting the file

    Please Login or Register  to view this content.

  9. #9
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to paste from clipboard to already existing text file ?

    Quote Originally Posted by watersev View Post
    hi sktneer, I do not understand refusal to delete the file if its content is deleted, option as you want it without deleting the file

    Please Login or Register  to view this content.
    Thanks a lot watersev ! for helping me.
    I am getting compile error and it says user-defined type not defined on line 3 i.e.
    Please Login or Register  to view this content.
    Please suggest what can be done to correct this compile error?

    Moreover I have no objection in deleting the text file and recreating it with the same name. I am sorry, I didn't mean that.

    Thanks once again for finding some time to help me with this code.

    Regards.

  10. #10
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: How to paste from clipboard to already existing text file ?

    For this code to run, you need to add a reference to the Microsoft Forms 2.0 Object Library.

    To do this, follow these steps:
    On the Microsoft Visual Basic Tools menu, click References to display the References dialog box.

    The References dialog box shows all object libraries registered with the operating system.
    Scroll through the list, and then click to select the Microsoft Forms 2.0 Object Library check box.

    If the Microsoft Forms 2.0 Object Library check box isn't available, follow these steps:
    Click Browse.
    Navigate to the file Fm20.dll, located in one of the following folders:
    In Microsoft Windows 95/98, the file should be located in the \SYSTEM folder.

    -or-
    In Microsoft Windows NT or Microsoft Windows 2000, the file should be located in the \SYSTEM32 folder.
    After you have located the file Fm20.dll, in the Add References box, click Open.

  11. #11
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to paste from clipboard to already existing text file ?

    Quote Originally Posted by watersev View Post
    For this code to run, you need to add a reference to the Microsoft Forms 2.0 Object Library.

    To do this, follow these steps:
    On the Microsoft Visual Basic Tools menu, click References to display the References dialog box.

    The References dialog box shows all object libraries registered with the operating system.
    Scroll through the list, and then click to select the Microsoft Forms 2.0 Object Library check box.

    If the Microsoft Forms 2.0 Object Library check box isn't available, follow these steps:
    Click Browse.
    Navigate to the file Fm20.dll, located in one of the following folders:
    In Microsoft Windows 95/98, the file should be located in the \SYSTEM folder.

    -or-
    In Microsoft Windows NT or Microsoft Windows 2000, the file should be located in the \SYSTEM32 folder.
    After you have located the file Fm20.dll, in the Add References box, click Open.
    Thanks watersev ! Added reference to the Microsoft Forms 2.0 Object Library as per your suggestion. But the code isn't doing anything. I am writing my steps what I am following...

    1) Copy new data on clipboard.
    2) Come back to excel sheet and from there I run the code.
    3) What it was supposed to do was to replace the older data in the Data.txt with the new data present on the clipboard and save it. But when I open Data.txt after the step 2, it is still having the older text in it.

    Any suggestions?

    Thanks indeed for your patience.

    Regards.

  12. #12
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: How to paste from clipboard to already existing text file ?

    The code works as expected. If the data.txt file is missing it will be created with the text data (if any) written to it.
    Are you checking correct file after code execution?
    What are you copying before code excution?

  13. #13
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to paste from clipboard to already existing text file ?

    Quote Originally Posted by watersev View Post
    The code works as expected. If the data.txt file is missing it will be created with the text data (if any) written to it.
    Are you checking correct file after code execution?
    What are you copying before code excution?
    Thanks watersev for your reply.

    With your help I have created this code and its working. I don't know if it is not supposed to be a good coding but its doing the required job.

    Please Login or Register  to view this content.
    Thanks for your support. And please let me know if there is a way to write this code in better way.

    Regards.

  14. #14
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How to paste from clipboard to already existing text file ?

    Why must you delete the older file if your new text is in a different file?

+ 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. macro to open, copy paste from existing excel file to a new blank file
    By 2blessed in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2012, 06:05 PM
  2. Importing a text file to an existing sheet, with a dialog to pick the text file
    By titushanke in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-22-2011, 01:43 PM
  3. [SOLVED] Importing text file, only option to edit existing file
    By smokey99 in forum Excel General
    Replies: 8
    Last Post: 04-26-2006, 04:08 PM
  4. [SOLVED] Store clipboard before "vba do cut&paste" then recover stored information to clipboard?
    By Marie J-son in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-08-2006, 02:00 AM
  5. Replies: 1
    Last Post: 10-17-2005, 04:05 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