VBA Open notepad file and copy it's contents then paste to word

Posted on 2006-04-06
Last Modified: 2008-02-26

I need to open a text file in notepad, copy it's contents and then paste them into a word document.

I can open the text pad file fine using  Shell "notepad c\:Test.txt" but do not know how I can then copy it's content.  I know that this is strictly outside the capabilities of vba but wondered if anyone has ever done this.  

I have tried using the code below to write the text in as stream, but as it contains various charactor sets eg Kanji and English it screws up the charactors due to the local settings etc, the only way I can get an exact copy in is when I manually open the text file and copy, paste it in.

Dim fso As FileSystemObject
Dim sFilename As String
Dim tsmfile As TextStream
Dim strXML As String
Dim TempFileLoc As String
Dim strXMLComp As String

Dim strXMLCompOne As String
Dim TempFileLocText As String
    TempFileLocText = gobjDASLoc.TempPath("Test.txt")
    sFilename = TempFileLoc    
    Set fso = New FileSystemObject
    Set tsmfile = fso.OpenTextFile(sFilename, ForReading, False)

    If tsmfile Is Nothing Then
        Exit Function
    End If

    strXML = tsmfile.ReadAll

ActiveDocument.Bookmarks.Add("bmk_xxx").Range.Text = strXML
Question by:wobbled
    LVL 44

    Accepted Solution

    Hello wobbled,

    you could try to use the clipboard object from the forms library [see the comment in the sub]
    Sub CopyPaste()
    Dim i As Long
    Dim strFileName As String
    'Because these procedures use the DataObject  variable type
    'you must have a reference set in your VBA project to
    'the Microsoft Forms 2.0 object library
    Dim m_clpData As New DataObject
      strFileName = "c:\test.txt"
      i = FreeFile
      Open strFileName For Input As i
      clpData.SetText Input$(LOF(i), #i)
      Close i
      ' Place the text from the Clipboard into document bookmark
      ActiveDocument.Bookmarks.Add("bmk_xxx").Range.Text = m_clpData.GetText()
    End Sub

    i didn't test this with the file you have so not sure if it takes all unicode characters with it

    hope this helps a bit
    LVL 17

    Author Comment

    Hi bruintje

    Sorry for the delay in getting back to you on this, as usual suddenly other items needed to be done urgently and this got pushed to the back of the que.  Thanks for your reply, I ended up doing this a different way due to changes in how I was getting sent the data (Reading XML into a freefile) , but as you pretty much got it spot on with your reply for what I asked I'll accept this as an excellent answer.



    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article will show you how to use shortcut menus in the Access run-time environment.
    Outlook Free & Paid Tools
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now