Thursday, January 22, 2009

VBA Tips & Tricks: Embed Existing Word File to Spreadsheet using Excel VBA

VBA Tips & Tricks: Embed Existing Word File to Spreadsheet using Excel VBA: "Insert Existing File (Word Document) to Spreadsheet using VBA

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

If you want to embed other document like PDF etc, you can do the same by

ActiveSheet.OLEObjects.Add Filename:= 'C:\VBADUD\Sample_CH03.pdf', Link:=False, DisplayAsIcon:= False"

No comments: