CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2002
    Posts
    4

    Opening a word doc - strange error

    Hey everyone, I'm new here.

    I have a problem I hope you can assist me with.

    In the following code...

    Dim wordApp As Word.Application
    Set wordApp = New Word.Application

    wordApp.Documents.Open ("blah")
    .
    .
    .
    Word blows up every time I run this code *unless* I set a breakpoint on the .Open line and step/run from there. Any suggestions? Could this be related to the size of the word document (38 pages, ~1000 bookmarks)?. One thing I have tried is setting the "OpenAndRepair" option to false, to no avail. Thanks in advance.

    -Zach

  2. #2
    Join Date
    Jul 2002
    Posts
    4
    Oh by the way I forgot to mention that I'm referencing VBA and Microsoft Word 10.0.

    --Zach

  3. #3
    Join Date
    Aug 2001
    Posts
    1,447
    off-the-cuff thought; try putting doevents before and after the open statement.

    if the doc is only 38 pages, you should be able to ZIP it and post it and maybe someone here can try to open it the same way

    Also, please be specific; what EXACTLY do you mean when you say "Word blows up" ? Does you whole machine turn into a doorstop or what?

  4. #4
    Join Date
    Jul 2002
    Posts
    4

    thanks

    I've fixed the problem. DoEvents didn't do the trick. There is a delay when instantiating the Word object (starting word) unfortunately VB doesn't block long enough. Even using ObjPtr(wordObject) = 0 doesn't do the trick. I ended up just instantiating the word object early enough to make it difficult for someone to use it before Word was ready.

    Oh, what I meant by "Word blows up", is exactly that - Word blows up. The system is fine, but word is hanging there waiting for a response from the user.

  5. #5
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    You'll probably also need to check and see whether word is already open too....otherwise it hangs.

  6. #6
    Join Date
    Jun 2002
    Location
    Lyman ME - USA | Oneonta NY - USA
    Posts
    399
    yes i've just finished a program where i reference word constantly and you need to check that word is open to make sure you can open the file, or you need to open a new instance of word everytime.....which is wasteful
    so i'd check that word is open...i have some code i can post tomorrow if that will help you out...

    hope it helps

    - nc
    "In a world without walls and barriers, what need is there for windows and gates!" - a mac ad
    "What was the best thing before sliced bread and when did sliced bread go out of existence?" - me
    "Software is like sex, it's better when it's free." - Linus Torvalds <- gotten from Andreas Masur


    Live Penguine! - Tux the linux mascot
    Vivez le penguine!, ¡Viva en penguine!, Lang lebe der Pinguin!, Viva no penguine!, Viva sul penguine!

  7. #7
    Join Date
    Jul 2002
    Posts
    4

    word

    Ok, the problem was only solved in the VB run-time environment. Once I went to my VB-less test box it started happening again. The error happens when I try to open the word document with this line of code:

    wordApp.Documents.Open (doc)

    It does not throw an error that I can catch, but at runtime it quits with "Run-Time error 4198 Command Failed". MSDN only has one article on this error code, and it doesn't seen to be related to my issue.

    Also, this error happens EVERY time, no matter if word is open or not - so I don't think that checking if word is open will solve this problem.

    I will be happy to hear any suggestions you may have!

    --Zach

  8. #8
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Are you absolutely sure that word is closed? I have had a similar problem with excel, where it didn't show on the taskbar, but when I looked in task mangler, excel.exe was there. I managed to solve my prob (which is slightly different to yours as I was opening excel inside a dll) by moving all the opening & closing code out of the dll, and into the program.

  9. #9
    Join Date
    Jun 2002
    Location
    Lyman ME - USA | Oneonta NY - USA
    Posts
    399
    i'll assume you know how to invoke these...if you read the code all you need to invoke is checkWordOpen
    and that can be done with a cmdbtn...u need a list too...

    Code:
    'start taskList
    Private Declare Function GetWindow Lib "user32" _
                                        (ByVal hwnd As Long, _
                                        ByVal wCmd As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" _
                                                (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
                                                (ByVal hwnd As Long, _
                                                ByVal lpString As String, _
                                                ByVal cch As Long) As Long
    Const GW_HWNDFIRST = 0
    Const GW_HWNDNEXT = 2
    'end taskList
    
    Sub LoadTaskList()
        Dim CurrWnd As Long
        Dim Length As Long
        Dim TaskName As String
        Dim Parent As Long
        CurrWnd = GetWindow(frmMain.hwnd, GW_HWNDFIRST)
        List1.Clear
        While CurrWnd <> 0
            Parent = GetParent(CurrWnd)
            Length = GetWindowTextLength(CurrWnd)
            TaskName = Space$(Length + 1)
            Length = GetWindowText(CurrWnd, TaskName, Length + 1)
            TaskName = Left$(TaskName, Len(TaskName) - 1)
            
            If Length > 0 Then
                If TaskName <> Me.Caption Then
                    List1.AddItem TaskName
                End If
            End If
            CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
            DoEvents
        Wend
    End Sub
    
    Public Function checkWordOpen()
    On Error Resume Next
        LoadTaskList
        For intI = 0 To List1.ListCount - 1
            If Left(List1.List(intI), 14) = "Microsoft Word" Then
                If Right(Trim(List1.List(intI)), Len(Right(docTitle, InStrRev(docTitle, "/", , vbTextCompare)))) = _
                                                Right(docTitle, InStrRev(docTitle, "/", , vbTextCompare)) Then
                    Exit Function
                Else
                    ChDir Left(docTitle, Len(docTitle) - Len(Right(docTitle, InStrRev(docTitle, "/", , vbTextCompare))))
                    WordApp.Documents.Add Right(docTitle, InStrRev(docTitle, "/", , vbTextCompare))
                    Exit Function
                End If
            Else
                If intI = List1.ListCount - 1 Then
                    Set WordApp = CreateObject("Word.Application")
                    WordApp.Documents.Open (docTitle)
                    'Disable viewing the Word session and its document
                    WordApp.Visible = False
                    If docPrev Then WordApp.ActiveDocument.Content.Paste
                End If
            End If
        Next intI
    End Function
    this is what i created to check and see if a specific doument is open...it checks that Microsoft Word is the first Part and then checks that the Document title is there...ie. test.doc
    so the title of microsoft word would be

    Microsoft Word - test.doc

    if that is found then it stops...otherwise it will open a document with that title, and the way i've set it up is that if the document title is something other than temp.doc in a sertain directory then it is saved somewhere because noone is going to try and delete the file while the program is running...that would be the way to crash it but i don't care right now...heh you can code that

    so if the file is saved it opens that and then if there was code in there i have something else printing to the document and once it is done printing it pastes it to the clipboard...hopefully nobody screws with that either...but at anyrate you can enhance it how you need

    hope this helps

    - nc
    "In a world without walls and barriers, what need is there for windows and gates!" - a mac ad
    "What was the best thing before sliced bread and when did sliced bread go out of existence?" - me
    "Software is like sex, it's better when it's free." - Linus Torvalds <- gotten from Andreas Masur


    Live Penguine! - Tux the linux mascot
    Vivez le penguine!, ¡Viva en penguine!, Lang lebe der Pinguin!, Viva no penguine!, Viva sul penguine!

  10. #10
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090
    Try if the following code:
    Code:
    Private Sub Command1_Click()
       Dim wrd As Word.Application, doc As Word.Document
       On Error Resume Next
       Set wrd = GetObject(, "Word.Application")
       If Err <> 0 Then
          Set wrd = CreateObject("Word.Application")
          Err.Clear
       End If
       If Not wrd Is Nothing Then
          'Replace C:\Test.doc with the document you want to open.
          Set doc = wrd.Documents.open("C:\Test.doc")
          wrd.Visible = True
       End If
       Set wrd = Nothing
    End Sub

  11. #11
    Join Date
    Jan 2005
    Posts
    1

    Red face Re: Opening a word doc - strange error

    Hi,
    I'm also facing the same problem.... did u found any solution on this ? pls help me..... Only difference is I'm using C# DotNet....... and my code hangs the moment it reaches the line "Documents.Open"....... It does creates a word instance which is ONLY viewable in task bar and is not visible other wise.....

    This code works fine in windows application and console application but not in web application.

    pls help.

    Thanks,
    Nitu


    Quote Originally Posted by zaustin
    Hey everyone, I'm new here.

    I have a problem I hope you can assist me with.

    In the following code...

    Dim wordApp As Word.Application
    Set wordApp = New Word.Application

    wordApp.Documents.Open ("blah")
    .
    .
    .
    Word blows up every time I run this code *unless* I set a breakpoint on the .Open line and step/run from there. Any suggestions? Could this be related to the size of the word document (38 pages, ~1000 bookmarks)?. One thing I have tried is setting the "OpenAndRepair" option to false, to no avail. Thanks in advance.

    -Zach

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured