simple macro woe

D

dan

Can anyone help???

I'm trying to write a simple macro which will replace text in my document
with a file. The text in the document is the filename to be inserted.

This is what I've written so far:



Sub INSERT_FILE()
'
' INSERT_FILE Macro
' Macro recorded 11/10/2006 by daniel.mee
'

Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy

Dim myString As String
Dim myString2 As String


myString = Selection
myString2 = "C:\MyDocuments && selection &&"

Selection.InlineShapes.AddOLEObject ClassType:="Package",
FileName:=myString2, LinkToFile:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\NOTEPAD.EXE", IconIndex:=0, IconLabel:= _
myString
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub


It all falls apart where I try to assign the strings. I basically want to
paste the data which I've copied from the text into one string, have the path
in another string and then concatenate the two strings and use them to Fill
in the FileName field in the insert object pop-up box.

Any suggestions on how to achieve this?? I also want to rename the caption
of the inserted object to the filename (myString variable).

Thanks in advance
 
Y

Yurij Zagrebnoy

dan said:
Can anyone help???

I'm trying to write a simple macro which will replace text in my document
with a file. The text in the document is the filename to be inserted.

If you want treat selected text as file name and replace it with file
contents, following code may help you:

Sub ReplaceFile()
Dim FileName As String
FileName = Selection.Text

Dim Value As String, Line As String
Value = ""
Open FileName For Input As #1
While Not EOF(1)
Input #1, Line
Value = Value + Chr(13) + Chr(10) + Line
Wend
Close #1

Selection.Text = Value
End Sub

This works for TEXT files only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top