Results 1 to 5 of 5

Thread: [RESOLVED] Opening a word document via VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Location
    Newcastle upon Tyne, UK
    Posts
    11

    Resolved [RESOLVED] Opening a word document via VBA

    Hello

    I am looking for help with a VBA macro I am writing in Excel. Basically I want to open a word document from inside an excel macro. I have tried:

    Sub open1()
    MsgBox "This form should be printed and taken to your local IT contact" & Chr(10) & Chr(10) & "Do not email this form to the team.", vbexclaimation, "L1"
    On Error GoTo BadShow
    Dim oWord As Object
    Set oWord = CreateObject("Word.application")
    oWord.Documents.Open "\\server\path\myfolder\forms\L1.doc"
    oWord.Visible = True
    AppActivate oWord
    Set oWord = Nothing
    Exit Sub
    BadShow:
    oWord.Quit
    Set oWord = Nothing
    End Sub


    That I found by using google, but my document opens and instantly closes. I am a bit of a novice with these things so any help would be greatly appreciated

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Opening a word document via VBA

    Thread moved to Office Development/VBA forum (note that the "VB Editor" in Office programs is actually VBA rather than VB, so the VB6 forum is not really apt)

    Welcome to VBForums

    I don't know what the problem is, but I can see why you aren't being explicitly told about it (and the document is closing).

    It is happening like that because of the code you have got to deal with errors - it deliberately hides any error messages, and also closes Word.

    To stop that from happening, comment out the line "On Error GoTo BadShow" by adding the ' character (or the word REM) before it.

    You should then get a message telling you what the issue is, and a line of the code should be highlighted too.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Location
    Newcastle upon Tyne, UK
    Posts
    11

    Thumbs up Re: Opening a word document via VBA

    Thanks for the help, I will try that when I get to work tomorrow and psot the results (in the right board this time )

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Opening a word document via VBA

    appactivate oword will most likely generate an error
    appactivate needs a string of the caption passed to it, not an object as you are doing
    try
    appactivate "Word"
    or
    appactivate oword.name
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Location
    Newcastle upon Tyne, UK
    Posts
    11

    Re: Opening a word document via VBA

    Thanks westconn1 works now

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