Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Macro Question: Syntax Error (Long)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Me - 14 Nov 2005 19:59 GMT
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.
Doug Robbins - Word MVP - 14 Nov 2005 21:01 GMT
You can almost certainly achieve the same result by using

With ActiveDocument
   .PrintPreview
   .Close PrintPreview
End With

Signature

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

>I previously posted this to the Microsoft Communities:
> _____________________
[quoted text clipped - 93 lines]
>
> Any help would be greatly appreciated.
Me - 14 Nov 2005 22:05 GMT
Where would that code go?  I have little/no experience in VB coding.

Thank you.

> You can almost certainly achieve the same result by using
>
[quoted text clipped - 100 lines]
> >
> > Any help would be greatly appreciated.
Jezebel - 14 Nov 2005 22:33 GMT
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.

> Where would that code go?  I have little/no experience in VB coding.
>
[quoted text clipped - 112 lines]
>> >
>> > Any help would be greatly appreciated.
Doug Robbins - Word MVP - 15 Nov 2005 05:27 GMT
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

Signature

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

> 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
[quoted text clipped - 142 lines]
>>> >
>>> > Any help would be greatly appreciated.
Jezebel - 15 Nov 2005 06:15 GMT
That code uses precisely the method described in the MVP article; just more
succintly.

> 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
[quoted text clipped - 151 lines]
>>>> >
>>>> > Any help would be greatly appreciated.
Doug Robbins - Word MVP - 15 Nov 2005 20:34 GMT
Or so subtly that I overlooked it <g>

Signature

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

> That code uses precisely the method described in the MVP article; just
> more succintly.
[quoted text clipped - 156 lines]
>>>>> >
>>>>> > Any help would be greatly appreciated.
Me - 15 Nov 2005 18:37 GMT
I will try this.

Thank you very much.  :-)

> 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
[quoted text clipped - 138 lines]
> >> >
> >> > Any help would be greatly appreciated.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.