Solved Error 424 Object Required when creating word template (1 Viewer)

Gasman

Enthusiastic Amateur
Local time
Today, 16:13
Joined
Sep 21, 2011
Messages
7,489
Since you've gone this far, it would not be hard to pass it an invalid file name to test my suggestion?
Hi Micron,
I did that and got an error message that it was unable to read the file. Nothing about an object.
 

Micron

AWF VIP
Local time
Today, 11:13
Joined
Oct 20, 2018
Messages
3,471
Thanks. Just so you know, I was going by this:
You supplied a valid object qualifier, but some other portion of the call contained an error. An incorrect path as an argument to a host application's File Open command could cause the error. Check arguments.

I surmised that an incorrect path might apply to the file as well. I guess that rules out users not having the same drive letter mapped (F) as well.
 

IceDarkness

New member
Local time
Today, 16:13
Joined
May 25, 2020
Messages
13
The templates are on a mapped network drive actually. I know some colleagues do have trouble with F/H drive disappearing and they have to go to the network address instead. But that's never happened to me. F/H Drive has always been there.

I tried changing the template name to something else and I got a different error saying file not found.

You declare a variable wdDoNotSaveChanges as an implicit Variant. I guess this is because you are using late-binding.

Oh, I added the Option explicit to the sub to see if it would find any errors and just put that in to bypass it.
 

Isaac

Lifelong Learner
Local time
Today, 08:13
Joined
Mar 14, 2017
Messages
3,273
Ahh, Didn't see that on the original code, must have missed it.
 

Isaac

Lifelong Learner
Local time
Today, 08:13
Joined
Mar 14, 2017
Messages
3,273
I think cheekybuddha's post bears repeating and adding to in the sense that you are declaring a bunch of things like:
Dim var1, var2, var3 as string

Almost 100% of the time when people do that, it is a misunderstanding. That doesn't mean all 3 variables are being typed as String. It only means the last one is.

Definition worth correcting regardless of bearing on problem. Strongly typing makes the picture clearer to digest when coding and when troubleshooting.
 

cheekybuddha

AWF VIP
Local time
Today, 16:13
Joined
Jul 21, 2014
Messages
557
Looking at IceDarkness' variables, they seem to be declared as they should be (WRT datatypes), albeit without much sense of order!

@IceDarkness - I'm not sure either what is causing your anomaly.

Perhaps try a slightly different tack using a singleton object - not too different from what you have already. In a standard module:
Code:
Option Explicit
Option Compare Database

Private m_oWord As Object

Function GetWord(blVisible As Boolean) As Object
On Error Resume Next

  If Not m_oWord Is Nothing Then
    Set m_oWord = GetObject(, "Word.Application")
    If Err Then
      Err.Clear
      Set m_oWord = CreateObject("Word.Application")
    End If
  End If
  Set GetWord = m_oWord

End Function

Function ReleaseWord() As Boolean

  If Not m_oWord Is Nothing Then
    Set m_oWord = Nothing
  End If
  ReleaseWord = m_oWord Is Nothing

End Function

Then, in your main code, substitute CreateWord() with GetWord(). When done with the object just make a call to ReleaseWord()

I don't know whether it will make any difference, but it will solve any issue if the problem is that the object variable has gone out of scope.
 

IceDarkness

New member
Local time
Today, 16:13
Joined
May 25, 2020
Messages
13
All I seem to be getting with that is error 91, object variable or with block variable not set.
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:13
Joined
Sep 21, 2011
Messages
7,489
Right, bearing in mind your code worked fine for me, you could try a decompile/compile, also try a compact/repair after backing up DB.
 

IceDarkness

New member
Local time
Today, 16:13
Joined
May 25, 2020
Messages
13
I have a found a little work around. I've altered the Error function just to create Word again. I know it's not ideal but it does work fine with this.

Code:
Exit_MayCauseAnError:
Screen.MousePointer = 0
Exit Sub


ErrHandler:
'I don't know why Error 424 keeps popping up every 3 or 4 button presses but this is a workaround to try creating word again.
If Err.Number = 424 Then
Call LogError("RenewalsForComms ChaseAdminButton", False)
Debug.Print "Error 424"
Set oWord = CreateWord(True)
Set oDoc = oWord.Documents.Add("F:\whatever.dotx")
Resume Next
Else
LogError ("RenewalsForComms ChaseAdminButton")
Resume Exit_MayCauseAnError
End If

I just wish I could figure out why sometimes oWord is failing.
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:13
Joined
Sep 21, 2011
Messages
7,489
I'd put Now() in the debug.print to show you the frequency.?
 

Isaac

Lifelong Learner
Local time
Today, 08:13
Joined
Mar 14, 2017
Messages
3,273
for what it's worth, I've had very mixed luck with GetObject() on Office apps. I generally always create a new one and deal with it from there.
 

IceDarkness

New member
Local time
Today, 16:13
Joined
May 25, 2020
Messages
13
I did actually solve this issue. The problem was putting the link to the template inside the oWord.Documents.Add(). When I put the template inside of a variable first, then called the variable in the function, it worked fine.

Code:
Dim strFilename As String
   strFilename = "F:\whatever.dotx"
  Set oDoc = oWord.Documents.Add(strFilename)
 

Isaac

Lifelong Learner
Local time
Today, 08:13
Joined
Mar 14, 2017
Messages
3,273
Glad to hear it's working!
 

Users who are viewing this thread

Top Bottom