View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Launchnet via OfficeKB.com Launchnet via OfficeKB.com is offline
external usenet poster
 
Posts: 47
Default My Problem - Help Please

THE NEXT STEP . . .

I needed the same procedure for opening a word document just like in Excel.
So, I modified the code and all works EXCEPT . . .PLEASE read my explaination
Can you give me a hand again ?
Thanks
Matt@Launchnet


Sub NewWordWithDocument()
Dim oWordApp As Object
Dim oWordDoc As Object
Set oWordApp = CreateObject("Word.Application")
' oWordApp.Visible = True
' Set oWordDoc = oWordApp.Documents.Open( _
' "C:\documents and settings\default\my documents\CompClassChurchBulletin.
doc")

'WITH THE PATH AND DOCUMENT HARD CODED ABOVE, IT WORKED, BUT HAD NO
TESTING.





'FROM HERE DOWN THE TESTING WORKS FINE.
'THEN, WHEN IT GETS TO THE LAST LINE OF CODE WHICH IS TO OPEN THE FILE
IN ACTIVECELL.
'AN ERROR MESSAGE IS DISPLAYED: "Type Mismatch"
'I CLICK DEBUG AND THE LAST LINE OF CODE IS HIGHLITED IN YELLOW.
'I DON'T KNOW WHAT I DID WRONG.




Dim testFileFind
Dim oWB As Object

ActiveCell.Offset(0, -1).Activate 'this moves the selected cell 1 cell to
the Left

'The following tests for a blank cell and ends processing
'It is needed because dir() function will not work with a blank.

If Len(Trim(ActiveCell)) = 0 Then
MsgBox "Active Cell " & ActiveCell.Address & " is blank. You have not
entered a Path & File Name."
End
End If

'The following tests for the existance of the file

testFileFind = Dir(ActiveCell)

'If the file is not found there will be nothing
'in the variable and processing ends.

If Len(testFileFind) = 0 Then
MsgBox "Invalid selection." & Chr(13) & _
"Filename " & ActiveCell & " not found"
End
End If

'THIS LINE OF CODE OPENS THE NEW INSTANCE OF WORD.
Set oWordApp = CreateObject("Word.Application")

'THIS LINE OF CODE MAKES THE NEW INSTANCE OF WORD VISIBLE.
oWordApp.Visible = True

Set oWordDoc = oWordApp.Documents.Open(ActiveCell)

End Sub


OssieMac wrote:
Try this to check that the file exists. Insert it as the first code in the
sub and then processing will terminate before it attempts to open anything if
the file does not exist. I should have used this method before.

Dim testFileFind

'The following tests for a blank cell and ends processing
'It is needed because dir() function will not work with a blank.

If Len(Trim(ActiveCell)) = 0 Then
MsgBox "Active Cell " & ActiveCell.Address & " is blank"
End
End If

'The following tests for the existance of the file

testFileFind = Dir(ActiveCell)

'If the file is not found there will be nothing
'in the variable and processing ends.

If Len(testFileFind) = 0 Then
MsgBox "Invalid selection." & Chr(13) & _
"Filename " & ActiveCell & " not found"
End
End If

Regards,

OssieMac

Hi Again, I think we are very close. I like your second suggestion best.

[quoted text clipped - 114 lines]

End Sub


--
Message posted via http://www.officekb.com