Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Insert excel as ole object into word doc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Garth M - 02 Dec 2007 21:21 GMT
Hi there,

I'm merging word documents into a word doc report using selection.insertfile
command. I also want to insert excel files as ole objects nto the word
report. How do I go about it?

Thanks
Garth
Lene Fredborg - 02 Dec 2007 22:42 GMT
See the VBA help on AddOLEObject.

Example: The following code will insert the active sheet of the file
C:\MyExcelFile.xls as an InlineShape, i.e. with wrapping style "In line with
text":

   Selection.InlineShapes.AddOLEObject _
       FileName:="C:\MyExcelFile.xls", _
       LinkToFile:=False, _
       DisplayAsIcon:=False

If you want the Excel file to be inserted with another wrapping style than
"In line with text", it must be inserted as a Shape:

   ActiveDocument.Shapes.AddOLEObject _
       FileName:="C:\MyExcelFile.xls", _
       LinkToFile:=False

Signature

Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word

> Hi there,
>
[quoted text clipped - 4 lines]
> Thanks
> Garth
Jay Freedman - 02 Dec 2007 22:47 GMT
>Hi there,
>
[quoted text clipped - 4 lines]
>Thanks
>Garth

If you want it positioned inline with text, use this kind of code:

Sub demo()
   Dim path As String
   Dim Ils As InlineShape
   
   path = "C:\temp\somefile.xls"
   
   Set Ils = ActiveDocument.InlineShapes.AddOLEObject( _
       FileName:=path, LinkToFile:=False, _
       DisplayAsIcon:=False, Range:=Selection.Range)
End Sub

If you want it to float, you need something different:

Sub demo()
   Dim path As String
   Dim Shp As Shape
   
   path = "C:\temp\somefile.xls"
   
   Set Shp = ActiveDocument.Shapes.AddOLEObject( _
       FileName:=path, LinkToFile:=False, _
       DisplayAsIcon:=False, Anchor:=Selection.Range)
   With Shp
       .WrapFormat.Type = wdWrapSquare
       .RelativeHorizontalPosition = _
            wdRelativeHorizontalPositionMargin
       .RelativeVerticalPosition = _
            wdRelativeVerticalPositionParagraph
       .Left = InchesToPoints(1.5)
       .Top = InchesToPoints(0.25)
   End With
End Sub

There are other parameters you can set, but this gives you the flavor
of the technique.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.