Activate Word Document (vba)

  • Hi. How to write to a Word document which is open already?


    I want to paste from Excel to Word, and the file may already exist or not. By searching existing threads I have solved this for creating a new file, for newly opening an existing file, but I don´t succeed when the document is opened by Word already.


    Here´s some simplified code:



    So the name WDoc of the document seems to be o.k. as it works with Open - but it does not work with GetObject.
    Any ideas?


    Thanks a lot.
    Dasha.

  • Re: Activate Word Document (vba)


    This should open/use word and open/use document
    [vba]Sub wdTest()

    ' same behaviour if defined:
    'Dim WordApp As Word.Application

    Dim WordApp As Object
    Dim wrdDoc As Object
    Dim tmpDoc As Object
    Dim WDoc As String
    Dim myDoc As String

    myDoc = "myTest"
    WDoc = ThisWorkbook.Path & "\" & myDoc & ".doc.docx"

    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    If WordApp Is Nothing Then
    ' no current word application
    Set WordApp = CreateObject("Word.application")
    Set wrdDoc = WordApp.Documents.Open(WDoc)
    WordApp.Visible = True
    Else
    ' word app running
    For Each tmpDoc In WordApp.Documents
    If StrComp(tmpDoc.FullName, WDoc, vbTextCompare) = 0 Then
    ' this is your doc
    Set wrdDoc = tmpDoc
    Exit For
    End If
    Next
    If wrdDoc Is Nothing Then
    ' not open
    Set wrdDoc = WordApp.Documents.Open(WDoc)
    End If
    End If

    End Sub[/vba]

    [h4]Cheers
    Andy
    [/h4]

  • Re: Activate Word Document (vba)


    Hi Andy.


    Thanks for the fast reply - it works "nearly". Also pasting to the document is o.k.


    Quote from Andy Pope


    [vba]
    WDoc = ThisWorkbook.Path & "\" & myDoc & ".doc.docx"


    WordApp.Visible = True
    End Sub[/vba]


    I did remove the ".docx" - will this be necessary for Excel 2007 or why did you put it?


    I moved the "Visible" to the end, but the behaviour is "strange". When the document is newly opened, a window comes up but stays in the back behind other windows open. When the document was open before, no window appears at all. How can I get the window with the Word document to the front in any case? (this was what happened when I tested before)


    Thanks and kind regards.
    Dasha.

  • Re: Activate Word Document (vba)


    Hi Andy - thanks again. It was a good step forward, though the last one was missing. It needs one more line:


    Code
    WordApp.Activate
    
    
    WordApp.Windows(wrdDoc).WindowState = wdWindowStateNormal


    Now it´s o.k. - and thanks also for making me aware that for delivering this to Excel 2007 users I have to add another extension when creating a new file (if you don´t mind to answer: is this true or would xls still be accepted?).


    Anyhow: great help. Thanks again.
    Best regards.
    Dasha.

  • Re: Activate Word Document (vba)


    If the file format is the new 2007 version it will be .docx
    You can however save to the old 2003 format from 2007 in which case it will be a .doc


    With my fat figures I tested a 2007 format and saved the file as mytest.doc and word helpfully add the 2007 format extension of .docx
    So whilst my double extension is not incorrect it is sloppy and I should have sorted it out.


    If you have a 2003 file the 2007 version of the program should open the file in compatibility mode.


    The above is true for excel extensions .xls and .xlsx

    [h4]Cheers
    Andy
    [/h4]

  • Re: Activate Word Document (vba)


    Hi Andy - thanks a lot for the reply. It´s really important for me: I´m developing an application which has to run on all systems from Excel 2000 to 2007.


    Quote from Andy Pope

    If the file format is the new 2007 version it will be .docx
    You can however save to the old 2003 format from 2007 in which case it will be a .doc


    With my fat figures I tested a 2007 format and saved the file as mytest.doc and word helpfully add the 2007 format extension of .docx


    So, as you know, in my code I have to check for a full file name, so it makes a difference whether Excel 2007 saves it as .doc or .docx. Would you rather check for the Excel version inside the code (how?) and use the appropriate extension, or can I force Excel 2007 to save as .doc?


    Quote from Andy Pope

    So whilst my double extension is not incorrect it is sloppy and I should have sorted it out.


    I´m very happy you didn´t.
    Thanks and best regards.
    Dasha.


    P.S.: Maybe I should upload my final procedure for pasting a selected area from Excel to Word in this forum? Excel file and/or just code? How and where to?

  • Re: Activate Word Document (vba)


    If your app is multi version you will have to aim for the lowest/oldest version.


    So when saving files save them in xl2000 format. Depending on what is in the files xl2003 may be enough.


    Do not save in xl2007 version (.docx .xlsx) as older version will not copy unless the user has done loaded special data transformers.


    Application.Version will return the version number of the app
    xl2007 is 12.0
    xl2003 is 11.0



    You can attach files to a post. There is a size restriction so if your files are large zip them first.

    [h4]Cheers
    Andy
    [/h4]

  • Re: Activate Word Document (vba)


    Hi Andy - thanks again.


    Quote from Andy Pope

    So when saving files save them in xl2000 format. Depending on what is in the files xl2003 may be enough.


    So far I thought that from xl2000 to xl2003 the format is the same, just some functionality (like rotation for shapes) will not work in xl2000. Where´s the difference really? (when searching the forum I found a link for saving tricks, but it didn´t work anymore)


    Quote from Andy Pope

    You can attach files to a post. There is a size restriction so if your files are large zip them first.


    O.K., I´ll add the code for the version and a bit more and then post it.


    Thanks again for your support which is really helpful for me.
    Kind regards.
    Dasha.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!