Code to open an .rtf file in Word

JW

Board Regular
Joined
Mar 17, 2002
Messages
72
Hi, I'm automating a process that includes transferring data from an .rtf file into Excel. Doing this manually seems easiest by opening it in Word, selecting all and then copy/pasting back into Excel. I'd like to do this in a macro - can anyone give me some code to open a certain file (say \\server\Share\file.rtf) in Word and then select all data and copy, or suggest a way to do this all in Excel (each way I've opened it in Excel has brought in all kinds of code too). I've tried around ShellExecute but can't get this to work either.
Thanks in advance
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try something like this:

Code:
Sub Test()
    Dim WordApp As Object
    Set WordApp = CreateObject("Word.Application")
    With WordApp
'       Change file name to suit
        .Documents.Open Filename:="P:\TEMP\MrExcel\RTFFile.rtf"
        .ActiveDocument.Select
        .Selection.Copy
    End With
    ActiveSheet.Range("A1").Select
    ActiveSheet.Paste
    WordApp.Quit
    Set WordApp = Nothing
End Sub
 
Upvote 0
This is a great treat indeed! How would one search for a word...say "Monkey" and return the line instead of the whole document?
 
Upvote 0
Try:

Code:
Sub Test()
    Const wdLine As Integer = 5
    Const wdExtend As Integer = 1
    Dim WordApp As Object
    Set WordApp = CreateObject("Word.Application")
    With WordApp
'       Change file name to suit
        .Documents.Open Filename:="P:\TEMP\MrExcel\RTFFile1.rtf"
        .Visible = False
        With .Selection.Find
            .Text = "Monkey"
            .Execute
        End With
        With .Selection
            .HomeKey Unit:=wdLine
            .EndKey Unit:=wdLine, Extend:=wdExtend
        End With
        .Selection.Copy
    End With
    ActiveSheet.Range("A1").Select
    ActiveSheet.Paste
    WordApp.Quit
    Set WordApp = Nothing
End Sub
 
Upvote 0
this is a neat stunt, thankyou, have you seen anything like this with pdf files?
 
Upvote 0
Hey Andrew, this post really helped me. I am wondering if you know how to incorporate Outlook.

The goal is to
-check for emails with specific Subject line every 5 minutes
-when a new one arrives, open it and download the .rtf / .doc attachment to hard drive
-then launch the WordApp module


EDIT:
I started a new topic
http://www.mrexcel.com/board2/viewtopic.php?t=254808
thanks.
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

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