2 methods
[VBA]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
[/VBA]
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
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")
[vba]
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
[/vba]
[VBA]Set Doc = Wd.Documents.Open("C:\AAA\Test.doc")[/VBA]
Artik
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")
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".
Apologies for the typo.Wd.Documents.Open
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
mdmackillop
And so I'm lying and I think. When you create object variables that go beyond the application, at the end of the procedure should probably destroy them.
Similarly like here:[vba]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[/vba] Is there no such requirement?
Artik
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.
You probably have the file open in another instance of Word. Perhaps it is hidden. Kill all processes Word.exe in Task Manager.
Artik
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.
One more method:
[VBA]Sub OpenFile()
Dim wscrip As Object ' WshShell
Set wscrip = CreateObject("WScript.Shell")
wscrip.Run "path and filename"
End Sub[/VBA]
Regards,
JP
Read the FAQ
Getting free help on the web
My website
Please use [vba][/vba] tags when posting code
And,
[VBA]Shell "cmd /c x:\msword\Letter1.doc", vbHide[/VBA]
i tried this code work well now, but it show me the file minimize on the toolbarOriginally Posted by Artik
Try adding:
...after the Wd.Visible line.Wd.Application.WindowState = 1
I tried your line after Wd.VisibleOriginally Posted by GTO
but still the same problem(Minimize..)
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.
[VBA]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
[/VBA]
Thank you all for help!!!