+ Reply to Thread
Results 1 to 13 of 13

How to open an existing Word document from Excel

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    How to open an existing Word document from Excel

    I have not been able to write code that will open an existing word document from within Excel. I can open a new document, but that doesn't help!
    Can anyone please help me?
    John

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: How to open an existing Word document from Excel

    Try.
        With CreateObject("word.Application")
            .documents.Open "C:\Documents and Settings\mydoc.doc" 'change path and name to suit
            .Visible = True
        End With
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to open an existing Word document from Excel

    I have now managed to open my existing word document. I am still in trouble though because I cannot make code which refers to that document 9but is in Excel) work.
    I think I need a link.
    The code I am using is
    Sub Open_Word_Document() 'Open an existing Word Document from Excel
    Dim objWord As Object
    
    
    Set objWord = CreateObject("Word.Application")
    
    objWord.Visible = True 'Change the directory path and file name to the location
    objWord.Documents.Open "U:\EVERYBODY!\Antalis\AntalisBarCode.docx"  'of the document you want to open from Excel  b
    
    MakeLabels
    End Sub
       
    Sub MakeLabels(MMDocument)
    '
    ' Macro2 Macro this code is at heart a macro
    MMDocument.Activate
    '
        ActiveDocument.MailMerge.MainDocumentType = wdMailingLabels
        ActiveDocument.MailMerge.OpenDataSource Name:= _
            "U:\EVERYBODY!\Antalis\Antalis_BarCodes.xlsm", ConfirmConversions:=False, _
             ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
            PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
            WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
            Connection:= _
            "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=U:\EVERYBODY!\Antalis\Antalis_BarCodes.xlsm;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37;Jet OLEDB:Database Loc" _
            , SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="", SubType:= _
            wdMergeSubTypeAccess
        Selection.TypeText Text:=" "
        
        Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="CODE"
        Selection.TypeBackspace
        Selection.Font.Name = "EAN-13"
        Selection.Font.Size = 72
        With ActiveDocument.MailMerge
            .Destination = wdSendToPrinter
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=False
        End With
    End Sub
    The MakeLabels call at the end of the first procedure doesn't work of course.

    Is this the right approach? Can someone help me here.
    John

  4. #4
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to open an existing Word document from Excel

    Bakerman2,
    Thanks for your help. I am sorry that my additional post and yours have crossed.
    I think that your code and mine are much the same, but yours is neater so I will use it.
    Can you help on my follow up post ? I am still very much stuck here. Some how I need to tell the MakeMerge sub which is the active document, and I have not yet found a way od doing that.
    John

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to open an existing Word document from Excel

    I admit I have not tested the following but I believe it should fix the syntax issues
    Sub Open_Word_Document() 'Open an existing Word Document from Excel
    Dim objWord As Object
    Dim objDoc As Object
    
    Set objWord = CreateObject("Word.Application")
    
    objWord.Visible = True 'Change the directory path and file name to the location
    Set objDoc = objWord.Documents.Open("U:\EVERYBODY!\Antalis\AntalisBarCode.docx")  'of the document you want to open from Excel  b
    
    MakeLabels objDoc
    End Sub
       
    Sub MakeLabels(MMDocument As Object)
        '
        ' Macro2 Macro this code is at heart a macro
        '
        
        Const wdMailingLabels As Long = 1
        Const wdOpenFormatAuto As Long = 0
        Const wdMergeSubTypeAccess = 1
        Const wdAlignParagraphCenter = 1
        Const wdSendToPrinter As Long = 1
        Const wdDefaultFirstRecord As Long = 1
        Const wdDefaultLastRecord As Long = -16
        
        With MMDocument
            .MailMerge.MainDocumentType = wdMailingLabels
            .MailMerge.OpenDataSource Name:= _
                "U:\EVERYBODY!\Antalis\Antalis_BarCodes.xlsm", ConfirmConversions:=False, _
                ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
                PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
                WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
                Connection:= _
                "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=U:\EVERYBODY!\Antalis\Antalis_BarCodes.xlsm;Mode=Read;" & _
                "Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37;Jet OLEDB:Database Loc", _
                SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
        
            With .Application.Selection
                .ParagraphFormat.Alignment = wdAlignParagraphCenter
                MMDocument.MailMerge.Fields.Add Range:=.Range, Name:="CODE"
                .Font.Name = "EAN-13"
                .Font.Size = 72
            End With
            With .MailMerge
                .Destination = wdSendToPrinter
                .SuppressBlankLines = True
                With .DataSource
                    .FirstRecord = wdDefaultFirstRecord
                    .LastRecord = wdDefaultLastRecord
                End With
                .Execute Pause:=False
            End With
        End With
    End Sub
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to open an existing Word document from Excel

    Hi John,

    Do you have a reference set to the Word library? If you do not, you cannot make use of Word's native constants without declaring them explicitly. I would guess you do not since your code is late bound?

    There are several other issues with your object references that I will endeavour to fix shortly.

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to open an existing Word document from Excel

    xlNitWit

    I can't thank you enough for your efforts. Your code worked perfectly as written. I did not have a reference to Word and did not understand the effect this would have.

    There is one problem left. The Excel data source is in the form of a dynamic named range which is a list of a given number of barcodes. (The whole exercise has been to enable packers to first identify the required code and then print out the right number of labels with this code.) . Both my manual mail merge and the code print out the requested barcode labels and then continue to print out empty labels for several sheets. If I look at Edit recipient List I find that this number of blank rows are checked as well as the required ones.
    It occurs to me that I could cure this by modifying SQLstatement:= "SELECT * FROM 'BARCODE_Table' where this is the named range. Is this a goer? I,m going to try anyway.
    John

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to open an existing Word document from Excel

    In my experience dynamic ranges do not work with queries, although this may not be true if the container workbook is already opened.

  9. #9
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to open an existing Word document from Excel

    You are right it doesn't work. Would it be better if I changed the source to a fixed range? Otherwise I can see mountains of label stock on the floor at the end of the day.
    John

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to open an existing Word document from Excel

    You can use a WHERE clause to only return records where a field is not blank

    SELECT * FROM `Sheet1$` WHERE NOT [field name] Is Null

  11. #11
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to open an existing Word document from Excel

    I am not very familiar with SQL. Should the field name "CODE" be written as ['CODE'] or without the brackets or as "CODE"

    I have tried the first variation and it was ignored.

    Could the source of the problem in fact be that:-
    Const wdDefaultLastRecord As Long = -16
    Unfortunately wdDefaultLastRecord is not a constant but depends on the number of labels I need to print. I have tried to vary this by passing through the number of labels and calculating the number of sheets of 6 labels needed.
    If I try and use this number in the Const definition I am told that a constant is required. Is there a way round this.?

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to open an existing Word document from Excel

    It would be written as [CODE] without the quote marks.

    Anything beginning with 'wd' is a built in constant from the Word application. You should not try to change that- it simply means the merge should use all the records available.

  13. #13
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: How to open an existing Word document from Excel

    Since you already have a document for the mailmerge, why are you using this code:
            With .Application.Selection
                .ParagraphFormat.Alignment = wdAlignParagraphCenter
                MMDocument.MailMerge.Fields.Add Range:=.Range, Name:="CODE"
                .Font.Name = "EAN-13"
                .Font.Size = 72
            End With
    Whatever fields you need should already be there in your document, all appropriately formatted. Moreover, since you're apparently trying to do a label merge, simply adding this field to the document is unlikely to produce the desired results - at best your field would be added to a single label and the rest of the sheet would be wasted.

    The Constant declarations are not the problem, neither was the lack of a reference to Word. Your code is written for use with late binding, which is why the reference to Word isn't required but the Constant declarations are. Declaring Constants in this way allows your code to be read as if it were using early binding (for which a reference to Word would be required). Using late binding can be an advantage if your code is to be used with different Office versions.

    If you want to print a particular range of labels, you need code to solicit the start and/or end numbers. For example:
                With .DataSource
                    .FirstRecord = InputBox("What is the first record to output?", "First record input", wdDefaultFirstRecord)
                    .LastRecord = InputBox("What is the last record to output?", "Last record input", .RecordCount)
                    If .FirstRecord < 1 Then .FirstRecord = wdDefaultFirstRecord
                    If .FirstRecord > .RecordCount Then .FirstRecord = .RecordCount
                    If .LastRecord > .RecordCount Then .LastRecord = wdDefaultLastRecord
                    If .LastRecord < .FirstRecord Then .LastRecord = .FirstRecord
                End With
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Trying to open an existing word document from Excel
    By j_Southern in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2016, 06:55 AM
  2. A word document that executes an SQL query on an existing excel workbook
    By gauravkmr33 in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 09-11-2013, 03:45 AM
  3. open excel source document in background when i open word document
    By hootiebsc in forum Word Programming / VBA / Macros
    Replies: 5
    Last Post: 03-22-2013, 07:50 PM
  4. Add text to Existing Word Document if it Doesn't exist create Word Document
    By unstable81 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-10-2013, 12:15 AM
  5. Copy excel data to existing form in word document
    By jarredladera in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-05-2012, 12:25 PM
  6. How do I open an existing excel 2003 document
    By kitchenguy in forum Excel General
    Replies: 2
    Last Post: 02-13-2006, 10:10 PM
  7. how do I transfer information from excel to an existing word document using a macro
    By sweetiez1114 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2005, 09:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1