Creating file.

SamiTheExcellent

New Member
Joined
Mar 25, 2009
Messages
2
Hello!

I want my macro to create new notepad file. Then i want to paste text to that file from clipboard. Then I want to save that file to desktop and close it.

What should I write to VisualBasic?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this. Put this code in a sheet module or a new module (Insert - Module in VB Editor). Make sure you follow the comment at the top of the code.

Code:
'Requires reference to Microsoft Forms 2.0 Object Library.  Set this in Tools - References in VB Editor.

Option Explicit

Public Sub Copy_Clipboard_To_Text_File()

    Dim desktopFolder As String
    Dim desktopFile As String
    Dim filenum As Integer
    Dim clipboard_text As String
    
    desktopFolder = Get_SpecialFolderPath("Desktop")
    desktopFile = desktopFolder & "\clipboard_test.txt"
    
    filenum = FreeFile
    Open desktopFile For Output As #filenum
    clipboard_text = Get_Clipboard_Text
    Print #filenum, clipboard_text
    Close #filenum
    
End Sub

Private Function Get_SpecialFolderPath(strSpecialFolder) As String

    Dim WSshell As Object
     
    Set WSshell = CreateObject("WScript.Shell")
    Get_SpecialFolderPath = WSshell.SpecialFolders(strSpecialFolder)
    Set WSshell = Nothing
    
End Function


Private Function Get_Clipboard_Text() As String

    Dim formsDataObject As MSForms.DataObject

    Set formsDataObject = New MSForms.DataObject
    formsDataObject.GetFromClipboard
    Get_Clipboard_Text = formsDataObject.GetText
    
End Function
 
Upvote 0

Forum statistics

Threads
1,203,677
Messages
6,056,688
Members
444,883
Latest member
garyarubin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top