Log in

View Full Version : Solved: Identifier under cursor not recognised



georgeeasten
06-13-2011, 05:27 AM
I have been using various macros which are initiated by various methods for quite some time now. I have not made any amendments to them and they work fine on other peoples computers?!?!

I have errors showing as identifier not recognised for End If, ActiveDocument.Save and Case vbNo.

The codes are as follows:
(Case vbNo)
Sub Format_Bill()

Application.ScreenUpdating = False

'*****
'Find and Replace
'*****
If ActiveDocument.Name = "04 - Bill.doc" Then
CreationDate = ActiveDocument.BuiltInDocumentProperties("Creation Date")
Select Case MsgBox("Do you want to create a formatted Bill of Costs?" & vbCrLf & vbCrLf & "Note: This may overwrite a previously formatted version", vbYesNo)

Case vbNo
'Do nothing
End

Case vbYes
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Underline = wdUnderlineNone
With Selection.Find
.Text = "^l"
.Replacement.Text = "^p^t^t"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory

.Text = "Engaged"
.Replacement.Text = "Engaged -"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory

.Text = "% *"
.Replacement.Text = "%"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory
'*****
'delete ordinal suffix
'*****
.Text = "([0-9]{1,2})([dhnrst]{2})( [JFMASOND][anuryebchpilgstmov]{2,8} [12][0-9]{3}>)"
.Replacement.Text = "\1\3"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll

End With
Selection.WholeStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
Selection.HomeKey Unit:=wdStory
'*****
'Change SaveAs file path
'*****
ChangeFileOpenDirectory ActiveDocument.path
Options.DefaultFilePath(wdDocumentsPath) = CurDir
'*****
'SaveAs formatted
'*****
ActiveDocument.SaveAs fileName:="04 - Bill (Formatted).doc", FileFormat:=wdFormatDocument

Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting

End Select
'*****
'Change SaveAs file path
'*****
ChangeFileOpenDirectory ActiveDocument.path
Options.DefaultFilePath(wdDocumentsPath) = CurDir
'*****
'SaveAs formatted
'*****
ActiveDocument.SaveAs fileName:="04 - Bill (Formatted).doc", FileFormat:=wdFormatDocument

Selection.EscapeKey
Selection.EscapeKey
Selection.EscapeKey
Selection.HomeKey Unit:=wdStory

Application.ScreenUpdating = True

ActiveDocument.CheckGrammar
Selection.EscapeKey
Selection.EscapeKey
Selection.EscapeKey
Selection.HomeKey Unit:=wdStory

ActiveDocument.Save

End If

End Sub

(ActiveDocument.Save and End If)Sub AutoOpen()

'*****
'SpellCheck Documents
'*****
If ActiveDocument.Name = "03 - Front Sheet.doc" Then

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.CheckGrammar
ActiveDocument.Save

ElseIf ActiveDocument.Name = "08 - Schedules.doc" Then
Selection.WholeStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
Selection.HomeKey Unit:=wdStory
ActiveDocument.CheckGrammar
ActiveDocument.Save

ElseIf ActiveDocument.Name = "09 - Certificates.doc" Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
Selection.HomeKey Unit:=wdStory
ActiveDocument.CheckGrammar
ActiveDocument.Save

ElseIf ActiveDocument.Name = "10 - Back Sheet.doc" Then
ActiveDocument.CheckGrammar
ActiveDocument.Save

End If

End Sub


I am rather puzzled because it seems to have come out of nowhere and there should be no reason for it?!?!

Any help appreciated.

Thanks

Frosty
06-13-2011, 09:51 AM
So your code doesn't compile (in the VBIDE, Debug > Compile)?

Or when you run it with showing all errors, it stops executing at stuff like ActiveDocument.Save?

If it is a compile error, and it isn't recognizing some basic commands, it sounds like a corrupt word installation.

You can try re-installing. You can also try referencing the object more explicitly (although I don't know how you do that with End If).

I've heard of cStr failing on certain computers, but VBA.cStr would not (as an example).

I've never heard of a logical construct like End If failing. It definitely doesn't have anything to do with your code-- although I would remove your use of the End command, which is generally bad practice for other reasons, but wouldn't cause this kind of issue.


Public Sub yada yada

'some code, and then in a logical construct

GoTo l_exit

'bunch of code

l_exit:
End Sub

Is a "better" way to do it, although heavy use of goto is also something I try to avoid too...

Not to derail the thread, but End does all sorts of other things in addition to simply stopping your current code execution dead. Or, actually, it does absolutely nothing but stopping your current code execution. So programmatic concepts like "garbage collection" don't get run, you can't nest your calling routines, and you will lose all kinds of things you (or others) might be holding in global memory, including, at times, event classes being used to capture other word events and run extra code.

georgeeasten
06-13-2011, 10:11 AM
When I run it, it stops executing.

All new code runs fine because I have been calling all sorts of functions trying to help someone on here (In fact I think you are in on that also (Acrobat)).

Seems as though it is a bit of an anomaly. The only thing I can think of is that I did a defrag the night before?!

I think I will see what happens and if I need to I will reinstall.

Thanks for your comments (by the way End has been removed!)

georgeeasten
06-13-2011, 11:55 PM
It seems that this issue has been miraculously resolved following a good night's sleep and a further reboot.

Most odd!