Solved

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

Posted on 2008-09-30
13
1,851 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
  • 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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 250 total points
ID: 22611953
You mean this?
Test2.xls
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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