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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automation Error!

Status
Not open for further replies.

LJtechnical

Technical User
Aug 7, 2002
90
GB
I have a button that should open a word template and continue with the vb code embedded in the template. Occasionally it works but for some reason I am getting Automation errors, I am lost as to why! Any Ideas?

Private Sub Command42_Click()
On Error GoTo Err_Command42_Click

Dim wrdApp As Object
Dim strFileNm As String
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
With FileSearch
Do

.NewSearch
strFileNm = "Mapimport"
.MatchTextExactly = True
.LookIn = "C:\Program Files\Microsoft Office\Templates"
.SearchSubFolders = False
.Filename = Dir(strFileNm)
.Execute
Loop While .FoundFiles.Count = 0
End With

Documents.Add Template:="C:\Program Files\Microsoft Office" _
& "\Templates\Mapimport.dot"

Exit_Command42_Click:
Exit Sub

Err_Command42_Click:
MsgBox Err.Description
Resume Exit_Command42_Click

End Sub
 
I think your code should read :-

strFileNm = "Mapimport"
With FileSearch
Do

(etc)


Regards
BrianB
** Let us know if you get something that works !
================================
 
I took a quick glance at your code. The first search-part I don't understand. I don't understand the meaning of your search.

But why do you use Documents.Add? As I understand the Word template already exists. Why don't you use Documents.Open. I must admit, I never tried this command on a Word template, but if Documents.Open works comparable to Workbooks.open (Excel), the command shouldn't open the template, but a copy, based on the template.

The code that I mostly use to automate Word is:
**************************************************
On Error Resume Next
Dim wdApp As Word.Application
Dim SourceDoc As Word.Document


Set wdApp = GetObject(, "Word.Application")
'In case of Error, start a new instance of Word
If Err.Number <> 0 Then Set wdApp = New Word.Application

Set SourceDoc = wdApp.Documents.Open(FileName:=strPathPlusFilename)

wdApp.Activate
wdApp.Visible = True
**************************************************
As far as I understand 'wdApp.Activate' is needed to give Word the focus. But I must admit, I don't fully understand this command.

Good luck, Bart Verlaan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top