Results 1 to 8 of 8

Open Notepad form VBA

This is a discussion on Open Notepad form VBA within the Excel Questions forums, part of the Question Forums category; I am sure this is easy but I need some help. I want to: 1. Open Notepad using my VBA ...

  1. #1
    Board Regular raihnman's Avatar
    Join Date
    Nov 2002
    Posts
    99

    Default Open Notepad form VBA

    I am sure this is easy but I need some help.

    I want to:
    1. Open Notepad using my VBA macro
    2. Paste data from a specific Excel cell to the text file
    3. Copy data from a range and paste that after the previous data in the text file.
    4. Name the text file and save it in a directory.
    5. Close the text file

    any suggestions?

    Thanks,

    Raihnman

  2. #2
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,702

    Default

    Howdy Raihnman,

    It kind of depends what you're doing, but here's an example:

    Sub PrintToTextFile()
    Dim FileNum As Integer, cl As Range, z As Integer, y As Integer
    Dim myStr As String
    FileNum = FreeFile ' next free filenumber
    'Open "C:\Temp\TEXTFILE.TXT" For Output As #FileNum ' creates the new file
    Open "C:\temp\TEXTFILE.TXT" For Append As #FileNum
    Print #FileNum, [a1]
    z = 10
    For Each cl In [b10:f30]
    y = cl.Row
    If y = z Then
    myStr = myStr & "|" & cl
    'appends the input to an existing file write to the textfile
    Else: Print #FileNum, myStr
    z = cl.Row
    myStr = "": myStr = myStr & "|" & cl
    End If
    Next
    'appends the input to an existing file write to the textfile
    Print #FileNum, myStr
    Close #FileNum ' close the file
    End Sub


    Hope this helps. Incidentally, copy this code into Word before pasting it into your VBE module.

  3. #3
    Board Regular raihnman's Avatar
    Join Date
    Nov 2002
    Posts
    99

    Default

    Thanks NateO, I will give it a try.

    Raihnman

  4. #4
    New Member
    Join Date
    May 2011
    Posts
    4

    Default Re: Open Notepad form VBA

    can this code be changed to copy selected data instead of fixed range A1:B10.

    thanks,

    iask

  5. #5
    MrExcel MVP Tom Urtis's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, California USA
    Posts
    10,988

    Default Re: Open Notepad form VBA

    Code:
    Sub CopySelectionNotepad()
    With Application
    Selection.Copy
    Shell "Notepad.exe", 3
    SendKeys "^v"
    VBA.AppActivate .Caption
    .CutCopyMode = False
    End With
    End Sub

  6. #6
    New Member
    Join Date
    May 2011
    Posts
    4

    Default Re: Open Notepad form VBA

    let me be precise of what I need if I may,
    copy some data from a column then transfer these data to notepad,
    open a pop up message to name and date the notepad file to be saved inside a folder.

    thanks,

    iask

  7. #7
    New Member
    Join Date
    May 2011
    Posts
    4

    Default Re: Open Notepad form VBA

    I need help making a macro

    1. find a the word "script" in the first row
    2. copy from cell 2 to cell 6
    3. Open Notepad using my VBA macro
    4. Paste copied data from Excel cell to notepad
    5. Name the text file and save it in a directory.
    6. Close the text file

    no idea how to even start it!

    Thanks,

    iask


  8. #8
    New Member
    Join Date
    Mar 2009
    Posts
    48

    Default Re: Open Notepad form VBA

    ' Try This

    HTML Code:
    Option Explicit
    Sub notepad()
    Application.ScreenUpdating = False
    Dim FF As Integer
    Dim plik As String
    Dim tekst As String
    Dim kom As Range
    Dim intResult As Variant
    plik = ThisWorkbook.Path & "\webpage1.txt"
    If FileOrDirExists(plik) Then
    Kill plik
    MsgBox "This File is Exists"
    'Application.SendKeys "%{F4}", True 'close
    Else
    FF = FreeFile
    Open plik For Output As #FF
    For Each kom In Sheets(1).Range("A1:B16")
    tekst = kom.Text
    Print #FF, tekst
    Next
    Close #FF
    intResult = Shell("Notepad.exe " & plik, vbNormalFocus)
    Application.Wait
    Application.SendKeys "%{F4}", True 'close
    'or CloseAPP_B "Notepad.exe"
    End If
    Application.ScreenUpdating = True
    End Sub
    Function FileOrDirExists(PathName As String) As Boolean
     
    Dim iTemp As Integer
     
    On Error Resume Next
    iTemp = GetAttr(PathName)
     
    Select Case Err.Number
    Case Is = 0
    FileOrDirExists = True
    Case Else
    FileOrDirExists = False
    End Select
     
    On Error GoTo 0
    End Function
    '**************************************
    Sub KillTest()
    MsgBox IIf(CloseAPP("notepad.exe", _
    True, False), _
    "Killed", "Failed")
    End Sub
    '**************************************
    Sub KillTest_B()
    CloseAPP_B "notepad.exe"
    End Sub
    'Close Application
    'CloseApp KillAll=False -Only first occurrence
    ' KillAll=True -All occurrences
    ' NeedYesNo=True -Prompt to kill
    ' NeedYesNo=False -Silent kill
    Private Function CloseAPP _
    ( _
    AppNameOfExp _
    As String, _
    Optional _
    KillAll _
    As Boolean = False, _
    Optional _
    NeedYesNo _
    As Boolean = True _
    ) _
    As Boolean
    Dim oProcList As Object
    Dim oWMI As Object
    Dim oProc As Object
    CloseAPP = False
    ' step 1: create WMI object instance:
    Set oWMI = GetObject("winmgmts:")
    If IsNull(oWMI) = False Then
    ' step 2: create object collection of Win32 processes:
    Set oProcList = oWMI.InstancesOf("win32_process")
    ' step 3: iterate through the enumerated collection:
    For Each oProc In oProcList
    'MsgBox oProc.Name
    ' option to close a process:
    If UCase(oProc.Name) = UCase(AppNameOfExp) Then
    If NeedYesNo Then
    If MsgBox("Kill " & _
    oProc.Name & vbNewLine & _
    "Are you sure?", _
    vbYesNo + vbCritical) _
    = vbYes Then
    oProc.Terminate (0)
    'no test to see if this is really true
    CloseAPP = True
    End If 'MsgBox("Kill "
    Else 'NeedYesNo
    oProc.Terminate (0)
    'no test to see if this is really true
    CloseAPP = True
    End If 'NeedYesNo
    'continue search for more???
    If Not KillAll And CloseAPP Then
    Exit For 'oProc In oProcList
    End If 'Not KillAll And CloseAPP
    End If 'IsNull(oWMI) = False
    Next 'oProc In oProcList
    Else 'IsNull(oWMI) = False
    'report error
    End If 'IsNull(oWMI) = False
    ' step 4: close log file; clear out the objects:
    Set oProcList = Nothing
    Set oWMI = Nothing
    End Function
    '**************************************
    'No frills killer
    Private Function CloseAPP_B(AppNameOfExp As String)
    Dim oProcList As Object
    Dim oWMI As Object
    Dim oProc As Object
    ' step 1: create WMI object instance:
    Set oWMI = GetObject("winmgmts:")
    If IsNull(oWMI) = False Then
    ' step 2: create object collection of Win32 processes:
    Set oProcList = oWMI.InstancesOf("win32_process")
    ' step 3: iterate through the enumerated collection:
    For Each oProc In oProcList
    ' option to close a process:
    If UCase(oProc.Name) = UCase(AppNameOfExp) Then
    oProc.Terminate (0)
    End If 'IsNull(oWMI) = False
    Next
    End If
    End Function
    Last edited by Dossfm0q; Mar 5th, 2012 at 08:26 AM.

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com