Solved

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

Posted on 2008-09-30
13
1,837 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now