Problems starting Word from Excel VBA

Discussion in 'Excel Programming' started by jan120253@gmail.com, Apr 3, 2014.

  1. Guest

    I have this code (as part of a long automation code, that simulates mailmerge by substituting bookmarks in a Word template with information from a spreadsheet):

    On Error Resume Next
    Set Wdapp = GetObject(, "Word.application")
    If Err.Number <> 0 Then
    Set Wdapp = CreateObject("Word.Application")
    End If

    It was made for Excel 2003, and now - after having upgraded to 2007 it does not work. If an instance of Word is already open, when I run the code, it Works as it is supposed, but if Word is not already started, it doesn't. So apparently the
    Set Wdapp = CreateObject("Word.Application") par does not execute.

    I have references to

    Visual Basic for Applications
    Microsoft Excel 12.0 Object Library
    OLE Automation
    Microsoft Office 12.0 Object Library
    Microsoft Word 12.0 Object Library
    Microsoft Office 12 Authorization Control 1.0 Type Library
    Microsoft ActiveX Data Objects 2.8 Library
    Microsoft ActiveX Data Objects Recordset 6.0 Library

    What am I missing here?

    Jan
     
    , Apr 3, 2014
    #1
    1. Advertisements

  2. Claus Busch Guest

    Hi Jan,

    Am Thu, 3 Apr 2014 03:50:43 -0700 (PDT) schrieb :

    > It was made for Excel 2003, and now - after having upgraded to 2007 it does not work. If an instance of Word is already open, when I run the code, it Works as it is supposed, but if Word is not already started, it doesn't. So apparently the
    > Set Wdapp = CreateObject("Word.Application") par does not execute.


    you have to open an existing document or to add a new document:

    Set ObjWord = CreateObject("Word.Application")

    With ObjWord
    .Visible = True
    ' .documents.Open "E:\Excel_NG\Freigaben bei OneDrive.docx"
    .documents.Add
    End With


    Regards
    Claus B.
    --
    Vista Ultimate SP2 / Windows7 SP1
    Office 2007 Ultimate SP3 / 2010 Professional
     
    Claus Busch, Apr 3, 2014
    #2
    1. Advertisements

  3. Guest

    Thank you. But why did it Work in 2003 then? Or is this a new thing?

    Jan



    Den torsdag den 3. april 2014 13.06.54 UTC+2 skrev Claus Busch:
    > Hi Jan,
    >
    >
    >
    > Am Thu, 3 Apr 2014 03:50:43 -0700 (PDT) schrieb :
    >
    >
    >
    > > It was made for Excel 2003, and now - after having upgraded to 2007 it does not work. If an instance of Word is already open, when I run the code, it Works as it is supposed, but if Word is not already started, it doesn't. So apparently the

    >
    > > Set Wdapp = CreateObject("Word.Application") par does not execute.

    >
    >
    >
    > you have to open an existing document or to add a new document:
    >
    >
    >
    > Set ObjWord = CreateObject("Word.Application")
    >
    >
    >
    > With ObjWord
    >
    > .Visible = True
    >
    > ' .documents.Open "E:\Excel_NG\Freigaben bei OneDrive.docx"
    >
    > .documents.Add
    >
    > End With
    >
    >
    >
    >
    >
    > Regards
    >
    > Claus B.
    >
    > --
    >
    > Vista Ultimate SP2 / Windows7 SP1
    >
    > Office 2007 Ultimate SP3 / 2010 Prof
     
    , Apr 3, 2014
    #3
  4. GS Guest

    GetObject("Word.Application") hijacks a running instance of the
    application, and so if Word is not running then your reference *Is
    Nothing*!

    CreateObject("Word.Application") starts an automated instance of Word
    if it's installed on the machine. If not installed then your reference
    *Is Nothing*!

    I don't see where you check to see that the instance of Word you want
    to use actually exists...


    Dim oWD As Object

    Set oWD = GetObject("Word.Application")
    If oWD Is Nothing Then CreateObject("Word.Application")

    If Not oWD Is Nothing Then
    'if you got here then
    'you have a fully qualified ref
    'to a running instance of Word,
    'so do stuff!
    End If

    --
    Garry

    Free usenet access at http://www.eternal-september.org
    Classic VB Users Regroup!
    comp.lang.basic.visual.misc
    microsoft.public.vb.general.discussion
     
    GS, Apr 3, 2014
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fritznel

    Starting a userform upon starting the file

    Fritznel, Jul 28, 2003, in forum: Excel Programming
    Replies:
    1
    Views:
    171
    Heiko
    Jul 28, 2003
  2. utmb

    Starting add-in using vba

    utmb, Nov 4, 2004, in forum: Excel Programming
    Replies:
    2
    Views:
    98
    Jens Thiel
    Nov 5, 2004
  3. utmb

    Starting add-in using vba

    utmb, Nov 5, 2004, in forum: Excel Programming
    Replies:
    3
    Views:
    81
    keepITcool
    Nov 8, 2004
  4. utmb

    Starting add-in using vba

    utmb, Nov 5, 2004, in forum: Excel Programming
    Replies:
    0
    Views:
    72
  5. utmb

    Starting add-in using vba

    utmb, Nov 8, 2004, in forum: Excel Programming
    Replies:
    0
    Views:
    66
  6. dant
    Replies:
    3
    Views:
    68
    Tom Ogilvy
    May 13, 2005
  7. Valerie
    Replies:
    1
    Views:
    71
    Trevor Shuttleworth
    Aug 18, 2005
  8. Mark

    starting .bat file from excel VBA

    Mark, Dec 29, 2005, in forum: Excel Programming
    Replies:
    2
    Views:
    75
    Gary''s Student
    Dec 29, 2005
Loading...