Macro Question: Syntax Error (Long)

M

Me

I previously posted this to the Microsoft Communities:
_____________________
When I open a previously saved file and cancel options to enter a new number
and form name, a debugging error message comes up (Run-time error ‘4605’).
When the "end" option within that dialog box is selected, my cursor is placed
within the header instead of in the document, and when I close out of the
header/footer toolbar, I’m taken back to the document, but the entire
document is selected instead of me simply being within a certain cell or row.

This debugging error does not happen when the form is being created as
normal; this is only an issue when previously saved forms are opened to be
edited.

When the "debug" option within that dialog box is selected, I am taken into
the Visual Basic editor and show the following macro:

* * * * * *
Sub AutoOpen()

Selection.HomeKey Unit:=wdStory
Selection.WholeStory
Selection.Fields.Update

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If

If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Or
ActiveWindow.ActivePane.View.Type _
= wdMasterView Then
ActiveWindow.ActivePane.View.Type = wdPageView
End If

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveWindow.ActivePane.View.NextHeaderFooter

Selection.WholeStory
Selection.Fields.Update
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.HomeKey Unit:=wdStory

End Sub
* * * * * *
The macro bombs out at “ActiveWindow.ActivePane.View.NextHeaderFooter.â€
________________________________

The answer I received was to change the coding to this:

Sub AutoOpen()

Selection.HomeKey Unit:=wdStory
Selection.WholeStory
Selection.Fields.Update

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If

If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Or
ActiveWindow.ActivePane.View.Type _
= wdMasterView Then
ActiveWindow.ActivePane.View.Type = wdPageView
End If

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
On Error resume next
ActiveWindow.ActivePane.View.NextHeaderFooter

If err.number = 0 then
on error goto 0
Selection.WholeStory
Selection.Fields.Update
End iF
on error goto 0
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.HomeKey Unit:=wdStory

End Sub


I did this, however, the new code works fine when functioning only with
Word, but when the code tries to execute via an Access macro, we receive a
“Compile error: Syntax error†message. I believe the line
“ActiveWindow.ActivePane.View.NextHeaderFooter†generates this message.

This Word macro runs via a macro initialized by a “button†inside an Access
file. The Access macro is responsible for opening the Word document that
will be utilized. The Word document is not opened prior to running the
Access macro.

Any help would be greatly appreciated.
 
D

Doug Robbins - Word MVP

You can almost certainly achieve the same result by using

With ActiveDocument
.PrintPreview
.Close PrintPreview
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jezebel

The code goes in place of all that rubbish you currently have in the
Auto_Open sub. Your code is currently trying to update fields in the
document including in headers and footers. The key problem is that it's
making assumptions about the structure of your document (ie what headers and
footers are defined), and the error message is just demonstrating the
invalidity of those assumptions. On top of which, it is seriously lousy
code.

Doug's method will normally work, because switching to PrintPreview mode
normally updates all fields anyway. However it will fail if you have no
printer installed (unlikely); and it can be ugly if the active printer is
Acrobat and the document is unsaved.

A fail-safe method is this code --

Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next

This is independent of your printer, it works even if Word is not visible,
and it doesn't muck around with the user interface.
 
D

Doug Robbins - Word MVP

That code (using prange) may need to be extended using the syntax in the
article "Using a macro to replace text where ever it appears in a document
including Headers, Footers, Textboxes, etc.":

http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jezebel

That code uses precisely the method described in the MVP article; just more
succintly.
 
M

Me

I will try this.

Thank you very much. :)


Jezebel said:
The code goes in place of all that rubbish you currently have in the
Auto_Open sub. Your code is currently trying to update fields in the
document including in headers and footers. The key problem is that it's
making assumptions about the structure of your document (ie what headers and
footers are defined), and the error message is just demonstrating the
invalidity of those assumptions. On top of which, it is seriously lousy
code.

Doug's method will normally work, because switching to PrintPreview mode
normally updates all fields anyway. However it will fail if you have no
printer installed (unlikely); and it can be ugly if the active printer is
Acrobat and the document is unsaved.

A fail-safe method is this code --

Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next

This is independent of your printer, it works even if Word is not visible,
and it doesn't muck around with the user interface.
 
D

Doug Robbins - Word MVP

Or so subtly that I overlooked it <g>

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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

Similar Threads

Sub AutoNew Question 2
Update fields in footers 2
Run-time error '4605' 6
Macro working in Word 97 & 2000, but not in 2003 3
Bookmark problem 0
Watermark macro 3
Macro error 5941 1
Macro assitance for Letterhead 2

Top