Using Excel VBA to control Word (open document)

K

Keith

Using Office2003 on WinXP

I have excel VBA code that does a bunch of stuff in Excel, and now needs to
open up an existing Word document, delete (or clear) some bookmarks
determined by the XL VBA code, then save the Word document under a new name.

#1
Right now, when it gets to the code to open the file, Excel hangs and (if I
close all the other windows) I see a dialog box that says "File in use;
[document] is locked for editing by [me], with options to open a read-only
copy, create a local copy, or wait for notification.

Read-only would be fine because I'll need to save under a different name
anyway- can anyone take a look at the code below and offer a tweak to open
the file without the warning?

#2
When I manually select to open the file as read only, either deleting the
bookmark or setting the text to "" in my loop, it loops several times then
gives an error- I suspect that deleting (and possibly emptying) the
bookmarks resets the bookmark count which means that I'm not deleting all
the ones I want, and I'm probably deleting the wrong ones along the way as
the earlier bookmaks are deleted.

Without pasting my array to a worksheet, what would be the best way to sort
the array (which includes some blank values) so I can delete the bookmarks
in decending order?

Many thanks,
Keith

Sub MakeGuideA()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
'wrdApp.Visible = True

Set wrdDoc = wrdApp.Documents.Open(CurrentPath & "\Interview_GuideA" &
".doc")

'word operations
With wrdDoc
For deleteComp = 1 To 18
If WordArray(deleteComp) <> "" Then
wrdDoc.Bookmarks(deleteComp).Range.Text = "" ' or
Range.Delete
End If
Next
wrdDoc.SaveAs (CurrentPath & ApplicantName & ".doc")
.Close ' close the document
End With

wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
ActiveWorkbook.Saved = True
End Sub
 
N

NickHK

Keith,
The 3rd argument of Word's open method is Read-Only. Try setting that to
True.

Or Word has the DisplayAlerts property the same as Excel, which can take one
of the values of the WdAlertLevel enum; I guess you need wdAlertsNone, which
has a value of 0, i.e. False.

NickHK
 

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