Errors In Opening Text Document

5

562487

From numerous newsgroups I'm able to piece together the below VBA
macro. The document I launch the macro from has a table with 2
columns, the first with old data and the second with new data. The
dialog box opens but I can't seem to pass the file name of what I just
opened to ChangeDoc variable. The line ChangeDoc = Documents.Open
blows up. How can I fix this?

562487


My intentions are listed:
a. Launch macro
b. Open diaglog box to select a *.seq file (text file)
c. Copy contents in a table in the Word document to matching words
in the Opened file
d. Close the file


Option Explicit


Sub ParseIt()
'

'
Dim ChangeDoc As Document, RefDoc As Document
Dim ctable As Table
Dim oldpart As Range, newpart As Range
Dim i As Long

Dim a As Long

Set RefDoc = ActiveDocument

With Dialogs(wdDialogFileOpen)
.Name = "*.seq"
.Display
If Right(LCase(.Name), 3) <> "seq" Then
MsgBox "You can only open files with a "".seq"" extension."
Else
ChangeDoc = Documents.Open

End If
End With

Set ctable = ChangeDoc.Tables(1)

RefDoc.Activate

For i = 2 To ctable.Rows.Count
Set oldpart = ctable.Cell(i, 1).Range
oldpart.End = oldpart.End - 1
Set newpart = ctable.Cell(i, 2).Range
newpart.End = newpart.End - 1
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute findText:=oldpart, ReplaceWith:=newpart,
Replace:=wdReplaceAll, MatchWildcards:=False, Forward:=True,
Wrap:=wdFindContinue
End With
Next i

ChangeDoc.Close wdDoNotSaveChanges

End Sub
 
J

Jay Freedman

Replace the statement

ChangeDoc = Documents.Open

with

ChangeDoc = Documents.Open(FileName:=.Name)

to tell VBA which file to open. When you look at the Help topic (put the
cursor on the word Open and press F1), you'll find that the FileName
parameter is required and all the other parameters are optional.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
R

Russ

562487,

To elaborate about what Jay said about the filename being required, change
that line to:
ChangeDoc = Documents.Open(.Name)
 
R

Russ

Oops,
Sorry, Jay did already say that and had the proper syntax, too.
ChangeDoc = Documents.Open(FileName:=.Name)
I missed that fact that he was using .Name
 

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