UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Error 462 On Second Pass Of Code, Office 2010    
 
   
BuzyG
post Aug 8 2017, 05:29 AM
Post#1



Posts: 303
Joined: 20-September 12
From: Cornwall UK


Hi I'm hoping Daniel is about today.

I have been trying to addapt the following code to work with word document we have. The code is running from an Access 2010 module.

Unfortunatley I get a Error 462, after the first pass of the code.

Any help would be much appriciated.


CODE
' Procedure : Export2DOC
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Export a recordset to a MS Word table in a new document
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).

Function Export2DOC(QryStr As String, FileStr As String, RepNum As String)
  Dim oWord           As Word.Application
  Dim oWordDoc        As Word.Document
  Dim oWordTbl        As Object
  Dim oSection        As Word.Section
  Dim oRange          As Word.Range
  Dim bWordOpened     As Boolean
  Dim db              As DAO.Database
  Dim rst             As DAO.Recordset
  Dim iCols           As Integer
  Dim iRecCount       As Integer
  Dim iFldCount       As Integer
  Dim q               As Integer
  Dim PageCount       As Integer
  Dim LineCount       As Integer
  Dim LocStr As String
  Const wdPrintView = 3
  Const wdWord9TableBehavior = 1
  Const wdAutoFitFixed = 0
  'Start Word
  On Error Resume Next
  Set oWord = GetObject(, "Word.Application")   'Bind to existing instance of Word
  Set oWordDoc = oWord.Documents.Open(FileStr, ReadOnly:=True)
  If Err.Number <> 0 Then    'Could not get instance of Word, so create a new one
    Err.Clear
    Set oWordDoc = Nothing
    Set oWord = Nothing
    On Error GoTo Error_Handler
    Set oWord = CreateObject("Word.application")
    Set oWordDoc = oWord.Documents.Open(FileStr, ReadOnly:=True)
    bWordOpened = False
  Else    'Word was already running
    bWordOpened = True
  End If
  On Error GoTo Error_Handler
  oWord.Visible = False 'Keep Word hidden until we are done with our manipulation
  Set db = CurrentDb
  'q = oWord.Selection.Sections.Count
  q = ActiveDocument.Sections.Count'  ******************** The Error 462 occurs here on all but the fisrt pass of the code ********************
  ' lots of bypast code in here not causing the error
  If bWordOpened = False Then
    oWord.Quit
  End If
Error_Handler_Exit:
  On Error Resume Next
  oWord.Visible = True   'Make Word visible to the user
  Set oRange = Nothing
  Set oSection = Nothing
  Set oWordTbl = Nothing
  Set oWordDoc = Nothing
  Set oWord = Nothing
  rst.Close
  Set rst = Nothing
  Set db = Nothing
  Exit Function
  
Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: Export2DOC" & vbCrLf & _
           "Error Description: " & Err.Description _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function

--------------------
Live to Surf
Go to the top of the page
 
ScottGem
post Aug 8 2017, 07:03 AM
Post#2


UtterAccess VIP / UA Clown
Posts: 32,114
Joined: 21-January 04
From: LI, NY


What is the exact error message and what line of code does it error out on?

--------------------
Scott <>
Scottgem's Blog
Microsoft Access MVP since 2007
Author: Microsoft Office Access 2007 VBA
Go to the top of the page
 
BuzyG
post Aug 8 2017, 07:30 AM
Post#3



Posts: 303
Joined: 20-September 12
From: Cornwall UK


Hi Scott

The break point is clearly shown in the OP code above.

q = ActiveDocument.Sections.Count

The message given is "The remote server machine does not exist or is unavailable."

Further note:- If the Word Document is already open prior to execution of the above code, the error does not flag.
This post has been edited by BuzyG: Aug 8 2017, 07:33 AM

--------------------
Live to Surf
Go to the top of the page
 
River59
post Aug 8 2017, 08:22 AM
Post#4



Posts: 1,209
Joined: 7-April 10
From: Detroit, MI


QUOTE
Set oWord = GetObject(, "Word.Application") 'Bind to existing instance of Word
Set oWordDoc = oWord.Documents.Open(FileStr, ReadOnly:=True)
If Err.Number <> 0 Then 'Could not get instance of Word, so create a new one


Looks like you are opening an instance of Word, then opening a file before you are checking to see if Word is already open. The error "The remote server machine does not exist or is unavailable." is telling you that Word is open in task manager.

Try checking to see if Word is open first, then if not, create an instance and open your file.


Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = New Word.Application
WordWasNotRunning = True
End If

--------------------
Remember ... Armstrong, Aldrin and Collins flew to the moon and back with a computer system less complex than a modern, programmable toaster ...
Go to the top of the page
 
LPurvis
post Aug 8 2017, 08:33 AM
Post#5


UtterAccess Editor
Posts: 16,051
Joined: 27-June 06
From: England (North East / South Yorks)


Hi

Well, it's actually pretty standard.
Yes, a Word application instance is being opened if one is not already open, otherwise the subsequent line can't execute:
But I wouldn't attempt the document opening - just the Word application acquiring.
i.e.
CODE
  Set oWord = GetObject(, "Word.Application")   'Bind to existing instance of Word

  If Err.Number <> 0 Then    'Could not get instance of Word, so create a new one
    Err.Clear
    On Error GoTo Error_Handler
    Set oWord = CreateObject("Word.application")
    bWordOpened = False
  Else    'Word was already running
    bWordOpened = True
  End If
  Set oWordDoc = oWord.Documents.Open(FileStr, ReadOnly:=True)

Ultimately, either way you have a Word instance. Or oWord.Documents.Open will fail - long before the reported error line. (By the way "bWordOpened" as a variable name for the flag suggests to me the opposite to how you seem to be using it. If it was named bWordOpen then that might suggest it was indeed already open.)

However, you have an unqualified Word object reference in your code.
i.e. you went from
 'q = oWord.Selection.Sections.Count
to
  q = ActiveDocument.Sections.Count
The former uses a fully referenced Word object, the second requires implicit object creation. (Which it will do once, and then not again - hence the failure on subsequent execution. It's a classic sign of this issue.)
You have a object reference to the document in question - so just use that.
CODE
  q = oWordDoc.Sections.Count


Cheers

--------------------
Go to the top of the page
 
BuzyG
post Aug 8 2017, 09:05 AM
Post#6



Posts: 303
Joined: 20-September 12
From: Cornwall UK


Thanks for that reply Leigh. You clearly understand what is happening here. smile.gif


When I use Oword.Selection.sections.count I get the wrong answer. It returns q=1, where there are 4 sections in the document I am working with. This is the causing me other problems further down my code, as it can't then ammend the footers in section 2 through 4. I won't complecate matters by posting all the code, as the issue seems to be right here. When the document is first opened.


I found when I use, q = ActiveDocument.Sections.Count it gives the correct section count of 4. The interger q is just part of my debugging. It is not actualy used in the final code. Alas this causes the Error 462.

So the question now becomes. Why does Oword.Selection.sections.count return 1 when there are 4 sections in the document and how is that corrected?

The "bWordOpened" logic was copied with Daniel's original code. I Agree with you. But it works so It's in there. smile.gif
This post has been edited by BuzyG: Aug 8 2017, 09:18 AM

--------------------
Live to Surf
Go to the top of the page
 
LPurvis
post Aug 8 2017, 09:44 AM
Post#7


UtterAccess Editor
Posts: 16,051
Joined: 27-June 06
From: England (North East / South Yorks)


Hi

You're referring to using:
q = Oword.Selection.sections.count

But I suggested that you use:
q = oWordDoc.Sections.Count

As that is the object variable referencing the document. (Not the entire Word application.)
That should hold the result you want as it's effectively the same document as the ActiveDocument property returns, all things being equal.

Cheers

--------------------
Go to the top of the page
 
BuzyG
post Aug 8 2017, 09:57 AM
Post#8



Posts: 303
Joined: 20-September 12
From: Cornwall UK


Thanks Leigh notworthy.gif

I have spent almost the entire day chasing the wrong problem. cryhard.gif laugh.gif All is now working perfectly. Should have asked this morning, instead of digging myself a hole. acclaim.gif

--------------------
Live to Surf
Go to the top of the page
 
River59
post Aug 8 2017, 10:25 AM
Post#9



Posts: 1,209
Joined: 7-April 10
From: Detroit, MI


We have all fallen down more than one rabbit hole, BuzyG. laugh.gif

--------------------
Remember ... Armstrong, Aldrin and Collins flew to the moon and back with a computer system less complex than a modern, programmable toaster ...
Go to the top of the page
 
LPurvis
post Aug 9 2017, 01:43 AM
Post#10


UtterAccess Editor
Posts: 16,051
Joined: 27-June 06
From: England (North East / South Yorks)


No worries, it happens. You'll dig a more shallow hole next time - and eventually, no holes at all. :-D

Cheers

--------------------
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    18th August 2017 - 10:42 PM