Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Puzzling: GetObject not working with word.application! 1

Status
Not open for further replies.

fsteeman

Programmer
Jul 17, 2002
103
DK
Hi there,

I can't get the following code to work. I keep on getting a run-time error 432: "File name or class name not found during Automation operation", pointing to the SetObj line.

However, I checked the file name and path and even pasted them in a folder navigation bar and it shows up without any problems.

This is the object path in the .ini file:
ContractTSTpl=\\appserver\zmshare\faeu\Database\webtemplates\ContractTSTpl.doc


Here follows the code:

Dim contractkind, difierstring As String
If OptionTSGC(0) = True Then contractkind = "TS"
If OptionTSGC(1) = True Then contractkind = "GC"
If contractkind = "" Then MsgBox "You must select a kind of contract": Exit Sub

'MsgBox contractkind
difierstring = "Contract" & contractkind & "Tpl"
Set WrdObj = GetObject(difierstring, "Word.Application")
WrdObj.Visible = True
WrdObj.Documents.Open CStr(IniReader(difierstring))


I hope someone can help me soon!

Cheers,

Fedor


 
Im sorry im not sure the answer to your question, I havent worked with word in a long time. Im not sure if your aware, but contractkind is declared as a variant. You need an as string after it for it to be declared as string.
 
I believe where you have difierstring, that is where the pathname goes. You must supply the entire path. Not just the document name. If you supply the correct path you will not need the progID( "Word.application").
 
Thanks for the replies! I have tried to change the code according to your tips. I first declared contractkind seperately as a string, the same error message occurred.
Then I made the difierstring into a path and removed the class argument (see code below). Now in the same line (Set WordObj) I get a type mismatch error!

The code:
Dim contractkind As String
Dim difierstring As String
If OptionTSGC(0) = True Then contractkind = "TS"
If OptionTSGC(1) = True Then contractkind = "GC"
If contractkind = "" Then MsgBox "You must select a kind of contract": Exit Sub

difierstring = "\\appserver\zmshare\Faeu\Database\webtemplates\Contract" & contractkind & "Tpl.doc"
Tpl.doc"
Set WrdObj = GetObject(difierstring)
WrdObj.Visible = True
WrdObj.Documents.Open CStr(IniReader(difierstring))


The document path:
\\appserver\zmshare\Faeu\Database\webtemplates\ContractTSTpl.doc


 
difierstring = "\\appserver\zmshare\Faeu\Database\webtemplates\Contract" & contractkind & "Tpl.doc"

Set WrdObj = GetObject(difierstring)
WrdObj.Visible = True
WrdObj.Documents.Open CStr(IniReader(difierstring))

as you did not post how you declared WrdObj, I am guessing that it was declared as Word.Application as you refer to it's 'Documents' and 'Visible' properties.

however, you are using GetObject with a path to a Word document as argument. This would return a Word.Document object.

maybe you could try something like this:

Code:
Dim WrdObj As Word.Application
Dim bWordWasOpen As Boolean

On Error Resume Next
Set WrdObj = GetObject("Word.Application")
bWordWasOpen = True
If Err.Number <> 0 Then
  If Err.Number = 432 Then
    bWordWasOpen = False
    Set WrdObj = CreateObject(&quot;Word.Application&quot;)
    Err.Clear
  End If
End If
On Error GoTo 0

WrdObj.Visible = True
WrdObj.Documents.Open difierstring
' do other stuff here
MsgBox &quot;done&quot;

If Not bWordWasOpen Then
  WrdObj.Quit
End If
Set WrdObj = Nothing

have to run; catch you tomorrow.
 
Fedor,

Have you tried what I suggested?
If so, what problems have you encountered?
I've automated Word lots of times using code similar to what I posted.
 
Hi Justin,

Sorry, I overlooked your post! I will try it right away!

Cheers,

Fedor
 
OK, I have tried to replace the original code with what you suggested, but now I get a &quot;Run-time error 91: Object Variable or With Block variable not set!&quot; in the line with &quot;WrdObj.Visible = True&quot; !

Here you can see the entire present code of the procedure (WrdObj is publically declared as Word.Application):


Private Sub ButtContract_Click()
If MainSet.EOF Or MainSet.BOF Then MsgBox &quot;No person selected&quot;: Exit Sub
Dim bWordWasOpen As Boolean

Dim contractkind As String
Dim difierstring As String
If OptionTSGC(0) = True Then contractkind = &quot;TS&quot;
If OptionTSGC(1) = True Then contractkind = &quot;GC&quot;
If contractkind = &quot;&quot; Then MsgBox &quot;You must select a kind of contract&quot;: Exit Sub

On Error Resume Next
Set WrdObj = GetObject(&quot;Word.Application&quot;)
bWordWasOpen = True
If Err.Number <> 0 Then
If Err.Number = 432 Then
bWordWasOpen = False
Set WrdObj = CreateObject(&quot;Word.Application&quot;)
Err.Clear
End If
End If
On Error GoTo 0

difierstring = &quot;\\appserver\zmshare\Faeu\Database\webtemplates\Contract&quot; & contractkind & &quot;Tpl.doc&quot;

WrdObj.Visible = True
WrdObj.Documents.Open difierstring

WordReplacer &quot;#name#&quot;, MainSet!firstname & &quot; &quot; & MainSet!lastname
WordReplacer &quot;#Address#&quot;, MainSet!street & vbCr & MainSet!zip & MainSet!city & vbCr & IIf(LVCountry.SelectedItem = &quot;Unknown&quot;, &quot;&quot;, LVCountry.SelectedItem)
WordReplacer &quot;#Group#&quot;, LVTaxon.SelectedItem
WordReplacer &quot;#spestim#&quot;, Nbr11
WrdObj.ActiveDocument.SaveAs IniReader(&quot;ContractPath&quot;) & MainSet!lastname & &quot;, &quot; & MainSet!firstname & &quot; contract.doc&quot;

If Not bWordWasOpen Then
WrdObj.Quit
End If
Set WrdObj = Nothing

End Sub


This is the original code that didn't work:


Private Sub ButtContract_Click()
If MainSet.EOF Or MainSet.BOF Then MsgBox &quot;No person selected&quot;: Exit Sub

Dim contractkind As String
Dim difierstring As String
If OptionTSGC(0) = True Then contractkind = &quot;TS&quot;
If OptionTSGC(1) = True Then contractkind = &quot;GC&quot;
If contractkind = &quot;&quot; Then MsgBox &quot;You must select a kind of contract&quot;: Exit Sub


difierstring = &quot;Contract&quot; & contractkind & &quot;Tpl&quot;
Set WrdObj = GetObject(difierstring, &quot;Word.Application&quot;)
WrdObj.Visible = True
'WrdObj.Documents.Open CStr(IniReader(difierstring))
WrdObj.Documents.Open difierstring

WordReplacer &quot;#name#&quot;, MainSet!firstname & &quot; &quot; & MainSet!lastname
WordReplacer &quot;#Address#&quot;, MainSet!street & vbCr & MainSet!zip & MainSet!city & vbCr & IIf(LVCountry.SelectedItem = &quot;Unknown&quot;, &quot;&quot;, LVCountry.SelectedItem)
WordReplacer &quot;#Group#&quot;, LVTaxon.SelectedItem
WordReplacer &quot;#spestim#&quot;, Nbr11
WrdObj.ActiveDocument.SaveAs IniReader(&quot;ContractPath&quot;) & MainSet!lastname & &quot;, &quot; & MainSet!firstname & &quot; contract.doc&quot;
Set WrdObj = Nothing

End Sub

 
I changed some more things and it works! However, now it is the Wordreplacer-procedure that has bugs... *sigh*


Private Sub ButtContract_Click()
If MainSet.EOF Or MainSet.BOF Then MsgBox &quot;No person selected&quot;: Exit Sub

Dim contractkind As String
Dim difierstring As String
Dim bWordWasOpen As Boolean
If OptionTSGC(0) = True Then contractkind = &quot;TS&quot;
If OptionTSGC(1) = True Then contractkind = &quot;GC&quot;
If contractkind = &quot;&quot; Then MsgBox &quot;You must select a kind of contract&quot;: Exit Sub

difierstring = &quot;\\appserver\zmshare\Faeu\Database\webtemplates\Contract&quot; & contractkind & &quot;Tpl.doc&quot;

On Error Resume Next
Set WrdObj = GetObject(difierstring, &quot;Word.Application&quot;)
bWordWasOpen = True
If Err.Number <> 0 Then
If Err.Number = 432 Then
bWordWasOpen = False
Set WrdObj = CreateObject(&quot;Word.Application&quot;)
Err.Clear
End If
End If
On Error GoTo 0

WrdObj.Visible = True
WrdObj.Documents.Open difierstring

WordReplacer &quot;#name#&quot;, MainSet!firstname & &quot; &quot; & MainSet!lastname
WordReplacer &quot;#Address#&quot;, MainSet!street & vbCr & MainSet!zip & MainSet!city & vbCr & IIf(LVCountry.SelectedItem = &quot;Unknown&quot;, &quot;&quot;, LVCountry.SelectedItem)
WordReplacer &quot;#Group#&quot;, LVTaxon.SelectedItem
WordReplacer &quot;#spestim#&quot;, Nbr11
WrdObj.ActiveDocument.SaveAs IniReader(&quot;ContractPath&quot;) & MainSet!lastname & &quot;, &quot; & MainSet!firstname & &quot; contract.doc&quot;

If Not bWordWasOpen Then
WrdObj.Quit
End If
Set WrdObj = Nothing

End Sub



 
This is the WordReplacer-function that now has problems (Run-time error 5: Invalid procedure or argument). I reckon there are still some problems with how the object was declared and set. I will look at it after lunch again, but feel free to come with comments....


Function WordReplacer(replace, repwith As String)
With WrdObj.Selection.Find
.ClearFormatting
.Text = replace
.Replacement.ClearFormatting
.Replacement.Text = repwith
.Execute replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
End Function
 
cannot see why the code would error on [tt].ClearFormatting[/tt] but

try changing

[tt]Function WordReplacer(replace, repwith As String)[/tt]

to

[tt]Function WordReplacer(replace As String, repwith As String)[/tt]

and see what errors come up
 
HI everyone,

I have been trying everything, but I still haven't found a solution to this problem. Can anyone help me, or should I just give up?? :-(

Cheers,

Fedor
 
Hmm, certainly is puzzling. The following test program, which more-or-less encapsulates all the functionality you mention, and uses most of your own code, works fine here:
[tt]
Option Explicit
Public wrdobj As Word.Application

Private Sub Command1_Click()
WordTest
End Sub

Public Sub WordTest()
Dim difierstring As String

difierstring = &quot;C:\wombat.doc&quot;
On Error Resume Next
Set wrdobj = GetObject(, &quot;Word.Application&quot;)
If Err = 429 Then
Set wrdobj = CreateObject(&quot;Word.Application&quot;)
End If
On Error GoTo 0
Beep
wrdobj.Visible = True
wrdobj.Documents.Open difierstring
WordReplacer &quot;#name#&quot;, &quot;Mike&quot; & &quot; &quot; & &quot;Strong&quot;

Set wrdobj = Nothing


End Sub

Private Function WordReplacer(replace as String, repwith As String)
With wrdobj.Selection.Find
.ClearFormatting
.Text = replace
.Replacement.ClearFormatting
.Replacement.Text = repwith
.Execute replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top