View Full Version : Solved: How to open a Specific file MsWord( example C:\MyW.doc) from MsEXcel...
Nader
01-22-2011, 07:57 AM
How to open A cpecific fie MsWord( example C:\MyW.doc) from MsEXcel... (http://www.vbaexpress.com/forums/t/163758.aspx)
mdmackillop
01-22-2011, 01:55 PM
2 methods
Sub HLink()
ActiveWorkbook.FollowHyperlink "C:\AAA\Test.doc"
End Sub
Sub WObject()
Dim Wd As Object
Dim Doc As Object
Set Wd = CreateObject("Word.Application")
Set Doc = Word.Documents.Open("C:\AAA\Test.doc")
Wd.Visible = True
End Sub
Nader
01-22-2011, 03:44 PM
I inserted the code in the command but it shwoed this error debug object requierd at the this line Set Doc = Word.Documents.Open("C:\AAA\Test.doc")
Private Sub CommandButton1_Click()
WObject
End Sub
Sub WObject()
Dim Wd As Object
Dim Doc As Object
Set Wd = CreateObject("Word.Application")
Set Doc = Word.Documents.Open("C:\AAA\Test.doc")
Wd.Visible = True
End Sub
Artik
01-22-2011, 04:42 PM
Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")
Artik
Nader
01-23-2011, 12:26 AM
Thank you for help it succeed with me this code by add Test.docx not doc becauseI'm using Ms2007
Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")
surveyasbe
01-23-2011, 01:51 AM
Even better, as I bet you've found out, you can open .doc files created by Microsoft Word in Pages without a problem. Saving Pages docs in Word format, however, isn't quite as obvious as you may imagine, because it's not a "Save As" it's an "Export".
mdmackillop
01-23-2011, 06:38 AM
Wd.Documents.Open
Apologies for the typo.
Artik
01-23-2011, 04:20 PM
mdmackillop
And so I'm lying and I think. :think: When you create object variables that go beyond the application, at the end of the procedure should probably destroy them.
Similarly like here:Sub WObject()
Dim Wd As Object
Dim Doc As Object
Set Wd = CreateObject("Word.Application")
Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")
Wd.Visible = True
Set Wd = Nothing
Set Doc = Nothing
End Sub Is there no such requirement?
Artik
Nader
01-24-2011, 08:07 AM
The problem that I faced is that show me a message that the file is being used . and ask me if I want to open it only for reading so I clikc ok and it open it.
Artik
01-24-2011, 09:49 AM
You probably have the file open in another instance of Word. Perhaps it is hidden. Kill all processes Word.exe in Task Manager.
Artik
Kenneth Hobs
01-24-2011, 10:47 AM
You probably did as Artik said and left an instance open in your testing.
If someone else has it open, you can check for that if needed. http://www.vbaexpress.com/forum/showthread.php?t=3641
As for not setting the object variables to Nothing, most would consider it good practice to do so. I saw a blog from a Microsoft employee once that said that it is not needed.
JP2112
01-24-2011, 12:57 PM
One more method:
Sub OpenFile()
Dim wscrip As Object ' WshShell
Set wscrip = CreateObject("WScript.Shell")
wscrip.Run "path and filename"
End Sub
Kenneth Hobs
01-24-2011, 02:39 PM
And,
Shell "cmd /c x:\msword\Letter1.doc", vbHide
Nader
01-26-2011, 07:51 AM
mdmackillop
And so I'm lying and I think. :think: When you create object variables that go beyond the application, at the end of the procedure should probably destroy them.
Similarly like here:Sub WObject()
Dim Wd As Object
Dim Doc As Object
Set Wd = CreateObject("Word.Application")
Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")
Wd.Visible = True
Set Wd = Nothing
Set Doc = Nothing
End Sub Is there no such requirement?
Artik
i tried this code work well now, but it show me the file minimize on the toolbar :think:
Try adding:
Wd.Application.WindowState = 1
...after the Wd.Visible line.
Nader
01-26-2011, 01:52 PM
Try adding:
Wd.Application.WindowState = 1 ...after the Wd.Visible line.
I tried your line after Wd.Visible
but still the same problem(Minimize..)
Kenneth Hobs
01-26-2011, 05:38 PM
I doubt that the MSWord Application is really minimized unless you already had it opened. What you want I suspect is to set focus or "Activate" it?
In my example, you can delete or comment out the early binding method and uncomment the late binding method or set the reference if you want to use it. I included both methods as early binding makes intellisense work for you.
Sub OpenDoc()
Dim s As String
s = ThisWorkbook.Path & "\test.docx"
OpenDoc2 s
End Sub
'Set Reference to Microsoft Word xx.x Object Libarary for Early Binding method.
Sub OpenDoc2(sDoc As String)
'Early binding method.
Dim Wd As Word.Application
Dim Doc As Word.Document
'Late Binding method.
'Dim Wd As Object
'Dim Doc As Object
If Dir(sDoc) = "" Then
MsgBox "Error, file does not exist." & vbLf & sDoc, vbCritical, "File is Missing"
Exit Sub
End If
On Error Resume Next
Set Wd = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set Wd = CreateObject("Word.Application")
End If
On Error GoTo errorHandler
Set Doc = Wd.Documents.Open(sDoc)
Wd.Visible = True
'Wd.Activate
errorExit:
Set Doc = Nothing
Set Wd = Nothing
Exit Sub
errorHandler:
MsgBox "Unexpected error: " & Err.Number & vbLf & Err.Description
Resume errorExit
End Sub
Nader
02-04-2011, 03:09 PM
Thank you all for help!!!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.