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!

Open MS Word with a variable for a filename?

Status
Not open for further replies.

cjhepburn

Technical User
Mar 9, 2001
8
US
I'm trying to open MS Word with a file My code looks something like this.

This works but I need the opening file ("c:\conversion.doc") to be a variable

Sub WordTest()
Call Shell("c:\Program Files\Microsoft Office\Office\winword.exe c:\conversion.doc", 1)
End Sub

I'm looking for a working verision of this:

Sub WordTest()
Dim test As String
test = "c:\conversion.doc"
Call Shell("c:\Program Files\Microsoft Office\Office\winword.exe test", 1)
End Sub

The idea is to open an RTF file and export it as text and close Word. I can then open it in excel.

If anyone has any other ideas or a fix for this problem, I would appreciate it.
 
I've copied over an answer I gave in VBScript. You do not have to shell out or write any Word VBA.

I've never done it in VBScript :) so I had to prove I knew of which I spoke.
This script works on a Win '95 / Word '97 machine.
Code:
Dim Document
Dim Application
Dim blnError
    Set Application = CreateObject("Word.Application")
    With Application
        .Visible = TRUE
        on error Resume next  
            .Documents.Open("D:\VBForums\Hello World.doc")
            if Err.Number <> 0 then blnError
        On error goto 0
        '* Do your own ERROR
        .Selection.WholeStory
        .Selection.Font.Bold = true
    End With
' WORD Stays open unless you QUIT.
'''''Application.ActiveWindow.Close false ' Close, Do not save changes
'''''Application.Quit false ' do not save changes
CreateObject always creates a new instance.
 
Dear it very easy nothing to be worried as there is single command is used as given bellow.
Take any variable...

sds=shell(&quot;msworld.exe&quot;,3)

it will open msword, and 3 for normal window. if you want open some word file then please write file name behind exe .
 
You need to do this:

Sub WordTest()
Dim test As String
test = &quot;c:\conversion.doc&quot;
Call Shell(&quot;c:\Program Files\Microsoft Office\Office\winword.exe &quot; & test, 1)
End Sub

The test is a VB variable so it needs to be outside the &quot;&quot;
The & puts the two strings together so it reads:

c:\Program Files\Microsoft Office\Office\winword.exe c:\conversion.doc

You could shorten it to:

Sub WordTest()
Dim test As String
test = &quot;c:\conversion.doc&quot;
Call Shell(&quot;winword.exe &quot; & test, 1)
End Sub

This is because Windows know where to find winword.exe. it is up to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top