Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Word as ReadOnly 1

Andrzejek

Programmer
Jan 10, 2006
8,547
US
Something that used to be very easy: in Excel VBA, open Word document as ReadOnly file, do something, close Word doc, move to the next Word doc.

So, I have this code in Excel (with the Reference to Word):

Code:
Option Explicit

Sub OpenWordDocs()
Dim wdApp As New Word.Application
Dim wdDoc As New Word.Document

Dim strPath As String
Dim strFile As String

'strPath = GetFolder & "\"
strPath = "C:\Temp\"

wdApp.DisplayAlerts = wdAlertsNone
wdApp.Visible = True

strFile = Dir(strPath & "*.docx")
Do While strFile <> ""
    Debug.Print strFile
    Set wdDoc = wdApp.Documents.Open(strPath & "\" & strFile, ReadOnly:=True)
    Debug.Print strFile & " has " & wdDoc.Paragraphs.Count & " Paragraphs"
    wdDoc.Close
    strFile = Dir()
Loop

wdApp.Quit
Set wdApp = Nothing

End Sub

But no matter what I try, Word wants me to Save file [banghead]
Even when I said: do not display any alerts (see code)
Even outside VBA, when I just open Word and just look at it, and close it - Word asks me to save it.

What am I doing wrong?
(Excel 365 Apps for Enterprise Version 2408, if that helps)
 
Thank you Herman, works like a charm in VBA (y)

Another (not VBA) question remains - why Word asks me to save the file when I open it and try to close it, without any changes?
 

Part and Inventory Search

Sponsor

Back
Top