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,553
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)
 
wdApp.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
wdApp.Quit
Set wdApp= Nothing
 
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?
 
why Word asks me to save the file when I open it and try to close it, without any changes?
Actually, Word may repaginate document for different printer, update fields or styles.

NB, read-only document can be changed, next saved, but only with different name.

Another option to close changed document without saving:
Code:
wdDoc.Saved = True
wdDoc.Close
 
Good point, combo.
But I can open the same Word doc many times in a row, and it asks me to save it every time.
Excel does not do that.
 
Andy,

I ran into this when I was working on an Excel to Mail Merge project... ok still working on it lol. Somewhere in my research I found that even with a blank Word doc, it needs to save the first instance. Why? No clue.

I think if you turn AutoRecover off, it might resolve the issue. I don't recommend that though.

If I come across the article, I'll add it to the thread.
 
Have you tried setting the document file readonly?

Code:
SetAttr "your.docx", vbReadOnly

PS: This discussion about the reviewing, editing, and view feature might also be helpful:
 
Last edited:

Part and Inventory Search

Sponsor

Back
Top