Documents.Add fails

R

Rodney Atkins

Hi all.

The macro below works on some machines but not others, and there seems
to be no rhyme or reason to whether it works or not. I've been able to
determine that when it does not work, it's because the Documents.Add
command fails. In other words, a new document is not created, which
then blows the rest of the macro.

It works on my work machine (Word 97 SR2 on Win Xp Pro), but not on
other machines with the same setup. It works on my home machine (Word
2000 on Win XP Home). It does not work on one machine with Word 2002
on Win 2000; it does work on another with Word 2002 and Win XP Home.

I was wondering if anyone had any ideas why this might be occurring.

Thanks.

Rodney


Sub Reforder()

Dim MySelection, DocName, NewRefs
MySelection = Selection.Text
DocName = ActiveDocument.name
Application.ScreenUpdating = False
Documents.Add
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText MySelection
DoSort
Selection.WholeStory
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
NewRefs = Selection.Text
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Documents(DocName).Activate
Application.ScreenUpdating = True
Selection.TypeText NewRefs
End Sub

Sub DoSort()
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "; "
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", "
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.WholeStory

Selection.Sort ExcludeHeader:=False, FieldNumber:="Field 2",
SortFieldType _
:=wdSortFieldNumeric, SortOrder:=wdSortOrderAscending,
FieldNumber2:= _
"Field 1", SortFieldType2:=wdSortFieldAlphanumeric,
SortOrder2:= _
wdSortOrderAscending, Separator:=wdSortSeparateByTabs

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^t"
.Replacement.Text = ", "
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "; "
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

With Selection.Find
.Text = "; ^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " ; "
.Replacement.Text = "; "
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

I would suggest that you declare an object variable as a document and then
load the new document into that variable using Set varname= Documents.Add
and then refer to the document by that variable.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
J

Jay Freedman

Hi, Rodney,

In addition to Doug's suggestion, you should add error handling that would
tell you something about why VBA failed to execute the command. Try
something like this:

Sub Reforder()
Dim MySelection, DocName, NewRefs
Dim MyDoc As Document ' <= new
MySelection = Selection.Text
DocName = ActiveDocument.name
Application.ScreenUpdating = False

On Error GoTo ErrHandler ' <= new

Set MyDoc = Documents.Add ' <= changed

Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText MySelection
DoSort
Selection.WholeStory
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
NewRefs = Selection.Text
MyDoc.Close SaveChanges:=wdDoNotSaveChanges ' <= changed
Documents(DocName).Activate
Application.ScreenUpdating = True
Selection.TypeText NewRefs

Exit Sub ' <= new from here to end
ErrHandler:
MsgBox "Error " & Err.Number & vbCr & Err.Description
End Sub

Once you get the error number and description, that may tell you what to do
to fix it, or you can post back here for advice (please keep it within the
same thread).

--
Regards,
Jay Freedman
Microsoft Word MVP

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL
 
R

Rodney Atkins

Jay,

I tried your changes to the macro exactly as it appears below, and
there was no change in its behavior. It faills at the same spot and
gives me the same error as before, which I didn't mention because it's
clear that the error is generated by the DoSort routine attempting to
sort the entire active document. The error is "Run-time Error '9125':
String passed can't be parsed", which is examined in this post:

http://groups.google.com/[email protected]

Do you think it would help to have an empty file, kind of a "scratch"
file, and open that particular file every time I want to do this sort?
Then I could call a particular file by name and close that particular
file when I'm done.

Thanks.

Rodney
 
J

Jay Freedman

Hi, Rodney,

I wasn't expecting a change in the macro's behavior, I was expecting
information about how/why it fails, and you've now supplied that. :)

I think you're right that the error is generated by DoSort, and that
completely contradicts the assertion that the error is caused by the
Document.Add statement. If there was a problem with Document.Add, the macro
would never get as far as calling DoSort. That also implies that keeping a
"scratch" document and replacing Document.Add with Document.Open is not
going to fix the problem.

Looking at the post you referred to, click the "Complete Thread" link and
read the last post from 'splash'. He found that the error was caused by
having empty lines in the text that he was sorting. (I don't know why that
caused an error -- the empty lines should simply sort to the top of the
file -- but sometimes you just hit one of those flaky things.) In the post
just before that, I listed some other things that could cause the same error
message.

As a troubleshooting step, go into the VBA editor and set a breakpoint on
the line in DoSort that actually does the sorting (Selection.Sort
ExcludeHeader...). Run the main macro. When the debugger stops at the
breakpoint, examine the document being sorted. If it contains blank lines,
try changing the first find/replace in DoSort to this:

With Selection.Find
.Text = "[^13]{2,}"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
R

Rodney Atkins

Jay,

The problem *IS* that Documents.Add (or the new version) is not
working, and I'll explain why. The purpose of this macro is to sort
reference citations into the desired order, which is oldest to newest,
and then alphabetically. For instance, if the reference citation is:

Smith, 1999; Jones et al., 1992; Green and Brown, 1992; Smith and
Wesson, 1972

the desired order would be:

Smith and Wesson, 1972; Green and Brown, 1992; Jones et al., 1992;
Smith, 1999

The user selects the text to be sorted and runs the macro. When run on
the "correct" machines, the macro opens a new window, pastes the text
into it, does a little cleaning (changes comma space to tab, etc.),
does the sort, reverses the cleaning, grabs the new text, closes the
new window, and pastes the new text in place of the old in the
original document.

Now, the first command in the DoSort function is Selection.WholeStory.
So, what happens is that when Documents.Add (or whatever) fails, the
DoSort function selects the ENTIRE original document and tries to sort
it, at which point the error message occurs.

Therefore, the clear problem is that the new window does not appear.
The error message is a secondary consequence of that failure.

Does that now make sense?

Rodney




Hi, Rodney,

I wasn't expecting a change in the macro's behavior, I was expecting
information about how/why it fails, and you've now supplied that. :)

I think you're right that the error is generated by DoSort, and that
completely contradicts the assertion that the error is caused by the
Document.Add statement. If there was a problem with Document.Add, the macro
would never get as far as calling DoSort. That also implies that keeping a
"scratch" document and replacing Document.Add with Document.Open is not
going to fix the problem.

Looking at the post you referred to, click the "Complete Thread" link and
read the last post from 'splash'. He found that the error was caused by
having empty lines in the text that he was sorting. (I don't know why that
caused an error -- the empty lines should simply sort to the top of the
file -- but sometimes you just hit one of those flaky things.) In the post
just before that, I listed some other things that could cause the same error
message.

As a troubleshooting step, go into the VBA editor and set a breakpoint on
the line in DoSort that actually does the sorting (Selection.Sort
ExcludeHeader...). Run the main macro. When the debugger stops at the
breakpoint, examine the document being sorted. If it contains blank lines,
try changing the first find/replace in DoSort to this:

With Selection.Find
.Text = "[^13]{2,}"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
J

Jay Freedman

Hi, Rodney,

Yes, that makes sense, and is a different situation than I understood from
the earlier part of the thread.

You might try an extra Activate command, although it shouldn't be needed:

Sub Reforder()
Dim MySelection, DocName, NewRefs
Dim MyDoc As Document
MySelection = Selection.Text
DocName = ActiveDocument.name
Application.ScreenUpdating = False

On Error GoTo ErrHandler

Set MyDoc = Documents.Add

MyDoc.Activate ' <= new

Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText MySelection
DoSort

If it's a timing problem, that may give the new document more time to become
'ready'. It should have no visible effect on the machines where the macro
already works.
 
R

Rodney Atkins

Jay:

I tried that, and it had no effect.

However, I changed back to my original version, and I decided to be
very explicit about the document to be added:

Documents.Add Template:=Application.NormalTemplate.FullName, _
NewTemplate:=False

Bingo!

That seems to have done the trick on the systems I've tested it on so
far. Still don't know WHY it works... :)

Thanks for all your help.

Rodney
 
J

Jay Freedman

Hi, Rodney,

I don't know why it works either, though I'm glad you finally found
something.

The documentation indicates that omitting the Template parameter should use
the Normal.dot template as the basis of the added document. Just a
thought -- search the problem PCs for the possibility they have more than
one Normal.dot file. Word *should* use only the one in the folder selected
as the User Templates folder (Tools > Options > File Locations), but there
have been reports of it getting "confused" by having others elsewhere. [The
image of Word getting confused reminds me of my cat -- how could you tell
that from the usual situation? <g>]
 

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