Does "Close" save changes?

E

Ed

I'm using the following in Word 2000:
Set doc = Documents.Open(fName & dName)
With doc
strDoc = .Content.Text
S = Replace(strDoc, "AB (B)", "AB-(B)", 1, -1, vbTextCompare)
doc.Close
End With

The docs open, the Replace runs, and the docs close. I never see an alert
for saving changes. I have not intentionally turned them off. What is the
default here - saving changes or not?

Ed
 
J

JE McGimpsey

The default is to save changes, but since you never make any changes in
the document, the doc will close silently.
 
G

Greg Maxey

Ummm...I thought the default was wdPromptToSaveChanges

and it silent in this case because like you say there have been no changes
made.
 
J

JE McGimpsey

Greg Maxey said:
Ummm...I thought the default was wdPromptToSaveChanges

and it silent in this case because like you say there have been no changes
made.


Sorry - I imprecisely responded to the OP's binary "saving changes or
not". I wasn't attempting to specify the default constant value, as he
mentioned "never see(ing) an alert".

In any case, it's definitely not "or not".

Thanks for the amplification.
 
C

Charles Kenyon

Won't doing a replace make changes?
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
J

JE McGimpsey

Charles Kenyon said:
Won't doing a replace make changes?

But no replace was done in the document.

The variable strDoc was modified, perhaps, by the VBA Replace method,
and the result put in the variable S.

But the doc wasn't touched.
 
E

Ed

So after this
strDoc = .Content.Text
S = Replace(strDoc, "AB (B)", "AB-(B)", 1, -1, vbTextCompare)
should I have
.Content.Text = S
Would that do what I need?
Ed
 
J

Jay Freedman

Hi Ed,

Adding that line will put the changed string back into the document,
and you will get the prompt to save.

However, this method of replacement has a side effect you may not
like. Because a string variable can't hold any kind of formatting, the
result will be that the entire document is formatted in the style of
the first paragraph and all other style-based and direct formatting
will disappear.

Instead, use the Find object of a Range, which preserves the
formatting of each occurrence:

Dim oRg As Range
Dim doc As Document
Set doc = Documents.Open(fName & dName)
Set oRg = doc.Range
With oRg.Find
.Text = "AB (B)"
.Replacement.Text = "AB-(B)"
.Format = False
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
doc.Close SaveChanges:=wdSaveChanges

Of course, if you know that the original text isn't formatted, or that
the formatting doesn't matter, then the two methods are pretty much
equivalent.
 
E

Ed

Thank you, Jay. Once again, you've saved me from myself!! I would indeed
have reformatted several documents, and that would not have been good!
Either to do or to explain the extra time to the boss.

With great thanks,
Ed
 

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