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'sFile 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.
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.
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.
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.
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.
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)