Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1883
  • Last Modified:

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

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
Or_A
Asked:
Or_A
  • 6
  • 5
  • 2
2 Solutions
 
CSecurityCommented:
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
 
Or_AAuthor Commented:
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
 
CSecurityCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CSecurityCommented:
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
 
Or_AAuthor Commented:
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
 
CSecurityCommented:
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
 
Or_AAuthor Commented:
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
 
CSecurityCommented:
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
 
GrahamSkanCommented:
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
 
Or_AAuthor Commented:
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
 
CSecurityCommented:
You mean this?
Test2.xls
0
 
GrahamSkanCommented:
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
 
Or_AAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now