Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to embed word doc inside excel file and then control it

Posted on 2008-09-30
13
Medium Priority
?
1,874 Views
Last Modified: 2012-05-05
Hi,
how can I embed inside an Excel file a Word file, and then with VB module launch the word file (In MS-Word, not as an Excel object) and control it?

untill now i just used 2 seperate files - the Excel and the Word, and the user had to direct the Excel (with a form i made) to the Word's file path (the 'strpath' argument that you can see in the snippet). then, the excel launched the Word file and made changes to it. you can see in the snippet an example of how I did that. by embeding the Word inside the Excel, i want to avoid all of that user directing phase, and automate the described process.

Appreciate your help!
Sub CreateReport(strpath As String)
    Dim Objdoc As Object
    Dim Wks As Excel.Worksheet
    Set Objdoc = GetObject(strpath, "Word.Document")
    Set Wks = ThisWorkbook.Sheets(1)
 
'An example of how I use the code to manipulate the Word file 
    Objdoc.Tables(1).Range.Rows.last.Delete
 
    Set Objdoc = Nothing
    Set Wks = Nothing
End Sub

Open in new window

0
Comment
Question by:Or_A
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 17

Expert Comment

by:CSecurity
ID: 22609988
Change to this:

Sub CreateReport()
    Dim Word As Object
    Dim Wks As Excel.Worksheet
   Set Word = CreateObject("Word.Application")
    Word.Visible = TRUE
    Word.Documents.Open("C:\My Documents\myfile.doc")

    Set Wks = ThisWorkbook.Sheets(1)
 
'An example of how I use the code to manipulate the Word file
    Objdoc.Tables(1).Range.Rows.last.Delete
 
    Set Objdoc = Nothing
    Set Wks = Nothing
End Sub

Now you can do everything to DOC file using Word Object
0
 

Author Comment

by:Or_A
ID: 22610039
Hi, thank for your comment,
but that not what i'm searching for - as i've written, i want to embed the Word file itself inside the Excel (if there's a way of doing so), and than write a code so that the Excel can create an instance of MS-word, and lanch the embeded file. i dont know how to embed + what should the code be like.
I assume there's a way of doing so?
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 22610058
Try this code attached given from:
http://vbadud.blogspot.com/2008/07/embed-existing-word-file-to-spreadsheet.html

It embeds link to existing DOC file, you want this?
Sub Insert_File_To_sheet()
 
Dim oWS As Worksheet ' Worksheet Object
 
Dim oOLEWd As OLEObject ' OLE Word Object
 
Dim oWD As Document ' Word Document Object (Use Microsoft Word Reference)
 
Set oWS = ActiveSheet
 
' embed Word Document
 
Set oOLEWd = oWS.OLEObjects.Add(Filename:="C:\VBADUD\Chapter 1.doc")
 
oOLEWd.Name = "EmbeddedWordDoc"
 
oOLEWd.Width = 400
 
oOLEWd.Height = 400
 
oOLEWd.Top = 30
 
' Assign the OLE Object to Word Object
 
Set oWD = oOLEWd.Object
 
oWD.Paragraphs.Add
 
oWD.Paragraphs(oWD.Paragraphs.Count).Range.InsertAfter "This is a sample embedded word document"
 
oOLEWd.Activate
 
End Sub

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:CSecurity
ID: 22610065
and if you want to open microsoft word as an external application and have control on it, what I said in previous post will do the job:

   Set objWord = CreateObject("Word.Application")
    objWord.Visible = TRUE
    objWord.Documents.Open("C:\My Documents\myfile.doc")

It will open new visible Word application with myfile.doc and you can have access to it using objWord object
0
 

Author Comment

by:Or_A
ID: 22610076
I think not, because i dont want to LINK to the file, i want it ITSELF inside my Excel. the reason is that it will be of use with many users, and the path for the Word file is not constant. the path will change from computer to computer.
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 22610095
You can load it with Word OLE object, look example file I've attached.

Double click on TEST text and Microsoft Word will be opened in SAME window in SAME Sheet... You want this?
Test.xls
0
 

Author Comment

by:Or_A
ID: 22610116
that not what im looking for, because that way as you can see no instance of the MS-word application gets opened with the file. it just show as an object inside excel. also i have no clue how to control this object as my example does.
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 22610158
1) You can initialize a Word ActiveX object, load a word document in hidden and do what you want, like storing data in it and save it in somewhere, you can keep it hidden and it will be totally hidden. You can do that with code I told you without objWord.Visible = True

2) Set objWord.Visible to True so new Word application will be loaded.

3) Initialize a OLE word object with other link and example code I gave you, except modify it to not load pyhsical path file, just new OLE embedded Word document and then you can control it, but all will understand it's a Word document loaded in Excel.

You can't load, modify, work on a doc without using Word and without user's knowledge about it, it will be obvious that Word is embedded in Excel in anyway...

There is no other way or I don't understand what you want...
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22610207
This seems to do it.
    Dim wdDoc As Word.Document
    Dim objOLE As OLEObject
    Set objOLE = ActiveSheet.OLEObjects.Add(Filename:= _
        "C:\Documents and Settings\...\My Documents\MyFile.doc", Link _
        :=False, DisplayAsIcon:=False)
        objOLE.Verb Verb:=xlOpen
        Set wdDoc = objOLE.Object
    MsgBox wdDoc.Name
    '...

Open in new window

0
 

Author Comment

by:Or_A
ID: 22610626
thank you Graham,
but again this is not what im searching. because as i said, i dont want to use constant path to the file.

I think i'm close to find my solution:
i added the Word file as an object to the Excel, and In the object's "Convert" menu i selected - "Activate as Microsoft Word Document" + display as icon.
then i inserted the folowing code that just allmost does what i wanted - it opens the embeded file, saves it in a selected path with a selected name, but the one last problem is that it dose'nt show on screen the new saved copy, but shows the created object instance, named "Documetn in test".
how can i show the new saved copy and terminate the "Document in test"?
attached is the snippet and also an example file.

thanks
Private Sub CommandButton1_Click()
    Worksheets(1).Shapes(2).OLEFormat.Activate
    Dim ObjAppWord As Object
    Dim Objdoc As Object
    Set ObjAppWord = GetObject(, "Word.Application")
    Set Objdoc = ObjAppWord.ActiveDocument
    Set ObjAppWord = Objdoc.Application
    Objdoc.SaveAs ("C:\Documents and Settings\user\Desktop\report.doc")
    ObjAppWord.Visible = True
    ObjAppWord.Activate
End Sub

Open in new window

Test.xls
0
 
LVL 17

Accepted Solution

by:
CSecurity earned 1000 total points
ID: 22611953
You mean this?
Test2.xls
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 1000 total points
ID: 22612314
That code showed you how to programatically create an embedded document from an existing one and how to open it directly. Once there, the file is independent of the original (Link = False).

This new snippet separates the embedding and editing into separate procedures. There are two embedding procedures. One shows how to copy an existing document. The other creates a new, blank document.
 
To find the OLEObject again might be more complex if you have more than one in your sheet, but I think that is a problem for later.

Your code gets the current ActiveDocument from the first found instance a Word Application. I think that it might sometimes get the wrong document.
Option Explicit
 
Sub EmbedExistingWordDocument()
    Dim objOLE As OLEObject
    Set objOLE = ActiveSheet.OLEObjects.Add(Filename:= _
        "C:\MyFolder\MyFile.doc", _
        Link:=False, DisplayAsIcon:=False)
End Sub
Sub EmbedNewWordDocument()
    Dim objOLE As OLEObject
    Set objOLE = ActiveSheet.OLEObjects.Add(ClassType:="Word.Document.8", Link:=False, _
        DisplayAsIcon:=False)
End Sub
 
 
Sub EditEmbeddedWordDocument()
    Dim wdDoc As Word.Document
    Dim objOLE As OLEObject
    Set objOLE = ActiveSheet.OLEObjects(1)
    objOLE.Verb Verb:=xlOpen
    Set wdDoc = objOLE.Object
    wdDoc.Range.Text = "This is a test"
    wdDoc.SaveAs "C:\MyFolder\MyFile2.doc"
    '...
End Sub
    
    

Open in new window

0
 

Author Closing Comment

by:Or_A
ID: 31501749
I actually used the last file that CSecurity attached, as this is what i wanted, but i gained lots of future knowledge thanks to both of you, so i'm spliting the points. thanks guys
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question