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
 
I am receiving file from client, which is I think generated by a script. But if I use a macro to split file it doesn't work correctly. Only thing that fix the file is when I open it with Notepad and save without doing any changes and close. And then all work fine.

That is why I need something that can open save and close the TXT file, if I do it with Word it can add some other character and mess up the file no ?

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

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).
When you say that your macro doesn't work correctly, what do you mean exactly? Maybe the macro can be modified accordingly. I did a quick search on Google and found the following thread (Post #10) with examples that use API functions...

Set noteApp = CreateObject("notepad.Application") , failed

Note that I haven't tested those functions.


My Macro split the text file to fix arrays to column but for some reason each file (having same formatting) is reacting differently and I end up with wrong data in wrong column (same thing if I do manually). But If I open these TXT files in notepad and just save without doing any changes, everything works perfectly after that in excel and all data is correctly split in right columns.

There must be some hidden characters that only get deleted when you save it manually. (copy data in new txt file with VBA result with same issue). Only manually opening and saving fix the thing, go understand. I cant ask client about it as data is correct and it is auto generated files.
 
Upvote 0
the text file already exists ie: it is not a newly cretaed one - Correct ?
 
Upvote 0

Forum statistics

Threads
1,223,362
Messages
6,171,640
Members
452,413
Latest member
N3edHelp

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