Open word 97 doc with doc name as variable 
Author Message
 Open word 97 doc with doc name as variable

Hi

I am trying to load a word doc, the document name and path is contained in a
field "link"

The code is this:

Private Sub linktext_Click()
Dim DocStr As String
Dim objWord As Word.Application, WordRunning As Boolean

    If IsNull(Me.link) = True Then
        MsgBox "No document to go to", , "No Doc"
        Exit Sub
    End If

DocStr = Me.link
DocStr = Chr(34) & DocStr & Chr(34)

'Check to see if word is running
' Needed for error trapping.
      On Error Resume Next

      ' Establish communication with Word
      ' Clear the error variable.
      Err.Clear

      ' Check to see whether Word 97 is running.
      Set objWord = GetObject(, "Word.Application.8")
      If Err.Number <> 0 Then
         WordRunning = False
      Else
         WordRunning = True
      End If

      ' Reset the error variable.
      Err.Clear

    If WordRunning = True Then
    Set objWord = GetObject("Word.Application")
    Else
    Set objWord = CreateObject("Word.Application")
    End If

With objWord

   Documents.Open (DocStr)
    objWord.Activate

End With

End Sub

I just can't get word to work. It is fine if a put a real string in
Documents.Open("c:\docs\filename.doc")

Any ideas?
--
Roger



Fri, 08 Feb 2002 03:00:00 GMT  
 Open word 97 doc with doc name as variable
You shouldn't need to put quotes around the DocStr variable
DocStr = Chr(34) & DocStr & Chr(34)
because it was Dimmed as a string.  Maybe it thinks that you're trying
to open a document called ""c:\docs\filename.doc""
try taking that part out and see if it works.



Quote:
> Hi

> I am trying to load a word doc, the document name and path is
contained in a
> field "link"

> The code is this:

> Private Sub linktext_Click()
> Dim DocStr As String
> Dim objWord As Word.Application, WordRunning As Boolean

>     If IsNull(Me.link) = True Then
>         MsgBox "No document to go to", , "No Doc"
>         Exit Sub
>     End If

> DocStr = Me.link
> DocStr = Chr(34) & DocStr & Chr(34)

> 'Check to see if word is running
> ' Needed for error trapping.
>       On Error Resume Next

>       ' Establish communication with Word
>       ' Clear the error variable.
>       Err.Clear

>       ' Check to see whether Word 97 is running.
>       Set objWord = GetObject(, "Word.Application.8")
>       If Err.Number <> 0 Then
>          WordRunning = False
>       Else
>          WordRunning = True
>       End If

>       ' Reset the error variable.
>       Err.Clear

>     If WordRunning = True Then
>     Set objWord = GetObject("Word.Application")
>     Else
>     Set objWord = CreateObject("Word.Application")
>     End If

> With objWord

>    Documents.Open (DocStr)
>     objWord.Activate

> End With

> End Sub

> I just can't get word to work. It is fine if a put a real string in
> Documents.Open("c:\docs\filename.doc")

> Any ideas?
> --
> Roger

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


Fri, 08 Feb 2002 03:00:00 GMT  
 Open word 97 doc with doc name as variable
Hi Chuck

Thanks a lot.

You put me on the right road. I changed the final bit to:

With objWord
   .Documents.Open (DocStr)
   .Visible = True
   .Activate
End With

And all is fine.

BTW

Do you know how to reverse the "On Error Resume Next" in code?

Would there be a better way of determining whether word is running that
didn't involve the "On Error Resume Next" code.
(I guess I could give this it's own sub)

--
Roger


Quote:

> Actually, if the doc has spaces in the name or the path, those quotes
> become fairly important.  Try it on a few documents in your "c:\my
> documents" folder, or on a file with a space in the name.  (Dang long
> file names messing everything up!  <grin>)

> Strings don't really have quotes around them by default, unless the
> programmer puts them there.  Which is what Roger was (correctly)
> doing.  (Good job Roger!  Most people miss that!)

> The problem is in how Roger is calling Documents.Open.  He should be
> using:
>   .Documents.Open(strDoc)

> (Note the dot =before= the statement.)

> Because he's also using "On Error Resume Next", the code isn't
> erroring out, and it's falling through to the objWord.Activate
> statement.  So Word pops up, empty, and oops!

> When debugging, the first step is to turn off global error checking
> like "On Error Resume Next" and "DoCmd.SetWarnings False" to see where
> the code is =actually= failing at.


> >You shouldn't need to put quotes around the DocStr variable
> >DocStr = Chr(34) & DocStr & Chr(34)
> >because it was Dimmed as a string.  Maybe it thinks that you're trying
> >to open a document called ""c:\docs\filename.doc""
> >try taking that part out and see if it works.



> >> Hi

> >> I am trying to load a word doc, the document name and path is
> >contained in a
> >> field "link"

> >> The code is this:

> >> Private Sub linktext_Click()
> >> Dim DocStr As String
> >> Dim objWord As Word.Application, WordRunning As Boolean

> >>     If IsNull(Me.link) = True Then
> >>         MsgBox "No document to go to", , "No Doc"
> >>         Exit Sub
> >>     End If

> >> DocStr = Me.link
> >> DocStr = Chr(34) & DocStr & Chr(34)

> >> 'Check to see if word is running
> >> ' Needed for error trapping.
> >>       On Error Resume Next

> >>       ' Establish communication with Word
> >>       ' Clear the error variable.
> >>       Err.Clear

> >>       ' Check to see whether Word 97 is running.
> >>       Set objWord = GetObject(, "Word.Application.8")
> >>       If Err.Number <> 0 Then
> >>          WordRunning = False
> >>       Else
> >>          WordRunning = True
> >>       End If

> >>       ' Reset the error variable.
> >>       Err.Clear

> >>     If WordRunning = True Then
> >>     Set objWord = GetObject("Word.Application")
> >>     Else
> >>     Set objWord = CreateObject("Word.Application")
> >>     End If

> >> With objWord

> >>    Documents.Open (DocStr)
> >>     objWord.Activate

> >> End With

> >> End Sub

> >> I just can't get word to work. It is fine if a put a real string in
> >> Documents.Open("c:\docs\filename.doc")

> ------
> Please Post Any Replies To This Message Back To the Newsgroup.
> There are "Lurkers" around who can benefit by our exchange!



Sat, 09 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. reformat word 2000 doc into word 97 format

2. Auto close open doc when opening another doc

3. Opening a Word Doc from Access with File Name Stored in a Table

4. Get the file name of an already open word doc

5. Saving a doc with name from doc text

6. Page count in a Word 97 doc reverts to wrong number on close

7. PC DOCS doc number footer for Word 97

8. Word 97 doc in VB6

9. VB/Word 97 Merge Doc Question.

10. Looking for an OCX that can display a WORD 97 doc

11. Start a word doc template from Access 97

12. pub doc and word doc on same screen

 

 
Powered by phpBB® Forum Software