Saving Notepad from VBA

netuser

Active Member
Joined
Jun 19, 2015
Messages
420
Hi,

I need to know how can I save Notepad document that i Open with VBA , i found the code to close Notepad but Saveas don't work, I can offcorse use Sendkey but it is not recommended method and I would really want to use CreateObject("Word.Application") method to save it


I got a code from this thread http://www.mrexcel.com/forum/excel-questions/715607-closing-notepad-visual-basic-applications.html
but author seem to have not connected for some time now, so I am creating new thread for help;
Thanks in advance for all your help :)

Code:
Public Sub CloseNotepad()
Dim oWdApp As Word.Application
Dim oTask As Task
Dim bCreate As Boolean

On Error Resume Next
Set oWdApp = GetObject(, "Word.Application")
On Error GoTo 0
If oWdApp Is Nothing Then
    Set oWdApp = CreateObject("Word.Application")
    oWdApp.Visible = False
    bCreate = True
End If

For Each oTask In oWdApp.Tasks
    'Provide a condition here which will match the notepad instances created by your code!
    If InStr(oTask.Name, "Notepad") > o Then
        oTask.Close
    End If
Next

If bCreate Then oWdApp.Quit

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Would you be saving the file under a different name each time? Or are you going to use a constant file name?

It will be same name as the file, what would I like is just Save if possible, the text file is already opened I just need to save it (but if Just save is not possible I can pass the name through variable)
 
Upvote 0
My apologies as I'm very much an amateur on VBA (only just started) but you could try something along the lines of;

Code:
Word.Application.SaveAs "C:\MyDir\YourFileName.txt", wdFormatText

I'm just downloading OpenOffice to test this out (I could be wrong - I apologise in advance if so!)


Edit - Just had a cheeky google and this may work:

ActiveDocument.Save
 
Last edited:
Upvote 0
Unfortunately the first solution is not working , I have already tried it.

And the 2nd one is I think for Word macro, I tried it anyway but doesn't work.

Also it is for Notepad and not Word just to be precise
 
Upvote 0
Unfortunately, Notepad does not support Automation. So you won't be able to use the CreateObject function. If you don't want to use the Sendkeys method, I think you'll need to use API functions. Try searching Google for some sample code.
 
Upvote 0
Unfortunately, Notepad does not support Automation. So you won't be able to use the CreateObject function. If you don't want to use the Sendkeys method, I think you'll need to use API functions. Try searching Google for some sample code.


Thanks for the reply, if it doesn't support automation, how become the above script work ? Close is working perfectly, just I don't know how to save it before closing.
 
Upvote 0
Close is a method of the Task object, which both belong to the Word object model. Unfortunately, it doesn't include a method to save.
 
Upvote 0
Close is a method of the Task object, which both belong to the Word object model. Unfortunately, it doesn't include a method to save.

Thanks, that's unfortunate.

Do you have any link to help me for API solution, I tried Google but was not able to find example link to notepad save.

Thanks again for your help :)
 
Upvote 0
What is it that you're trying to do? Do you want to open the text file, make changes to it, save it, and then close it? If so, have you considered using Word?
 
Upvote 0

Forum statistics

Threads
1,223,362
Messages
6,171,636
Members
452,411
Latest member
sprichwort

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