Solved

Embed 2 page Word Doc in Excel Worksheet

Posted on 2011-02-25
7
492 Views
Last Modified: 2012-05-11
I am trying to embed a 2 page word document into an excel worksheet in a workbook
and am having a problem with the code.
Can you help
'This Code has the User open the CSR1 request form and
'then embeds it in a new excel sheet called CSR1 in the Fiscal Tool
Sub AddCSR1()
  Dim FullFile As Variant
  Dim work_book As Workbook
  Dim last_sheet As Worksheet
  Dim WS As Worksheet
  Dim OLEWd As OLEObject
  Dim WD As Document
  Application.ScreenUpdating = False
  Set work_book = Application.ActiveWorkbook
Set last_sheet = work_book.Sheets(work_book.Sheets.Count)
Set WS = work_book.Sheets.Add(After:=last_sheet)
WS.Name = "CSR1"
  FullFile = Application.GetOpenFilename _
(" WORD files(*.doc),*doc", 1, "SELECT and OPEN the CSR1 File", , False)
If VarType(FullFile) = vbBoolean Then
MsgBox "No File Specified", vbExclamation
Exit Sub
End If
With OLEWd
Set OLEWd = ActiveSheet.WS.OLEObjects.Add(Filename:=FullFile, Link:=False, DisplayAsIcon:=False)
OLEWd.Verb xlVerbOpen
OLEWd.Name = "CSR1"
'OLEWd.Width = 400
'OLEWd.Height = 800
'OLEWd.Top = 30
Set WD = OLEWd.Object
End With
Set WS = Nothing
Set FullFile = Nothing
Set OLEWd = Nothing
Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:llawrenceg
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34986064
There are two things

1) You need to set a reference to Microsoft Word Object Library for the line to work.

Dim WD As Document

2) There is an error in your code in this line

Set OLEWd = ActiveSheet.WS.OLEObjects.Add(Filename:=FullFile, Link:=False, DisplayAsIcon:=False)

You are specifying the sheet twice.

Try this code

Sub AddCSR1()
    Dim FullFile As Variant
    Dim work_book As Workbook
    Dim last_sheet As Worksheet, WS As Worksheet
    Dim OLEWd As OLEObject
    Dim WD As Word.Document
  
    Application.ScreenUpdating = False
    Set work_book = Application.ActiveWorkbook
    Set last_sheet = work_book.Sheets(work_book.Sheets.Count)
    Set WS = work_book.Sheets.Add(After:=last_sheet)
    
    WS.Name = "CSR1"
    
    FullFile = Application.GetOpenFilename _
    (" WORD files(*.doc),*doc", 1, "SELECT and OPEN the CSR1 File", , False)
    
    If VarType(FullFile) = vbBoolean Then
        MsgBox "No File Specified", vbExclamation
        Exit Sub
    End If
    
    With OLEWd
        Set OLEWd = ActiveSheet.OLEObjects.Add(Filename:=FullFile, Link:=False, DisplayAsIcon:=False)
        OLEWd.Verb xlVerbOpen
        OLEWd.Name = "CSR1"
        'OLEWd.Width = 400
        'OLEWd.Height = 800
        'OLEWd.Top = 30
        Set WD = OLEWd.Object
    End With
    
    Set WS = Nothing
    Set FullFile = Nothing
    Set OLEWd = Nothing
    Application.ScreenUpdating = True
End Sub

Open in new window


I have tested it and it works now  :)

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34986074
And Yes

To set a reference, In VBA Editor, Click on the Tools Menu~> References and then select the Microsoft Word Object xx.xx Library.

Sid
0
 

Author Comment

by:llawrenceg
ID: 34988698
 Sid:
My problem now is that the document I want to embed is 2 pages long and this code will only embed page one. Now if I click on the page to edit it  it will open to 2 pages, however I need both pages to show wheen initially embedded
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34989945
I am not sure if you can do it.

There is an alternative though but might not be what you actually looking at. Insert two instances of the same Ole Object. In One show the 1st page and in the other show the other.

Sid
0
 

Author Comment

by:llawrenceg
ID: 34990068
that approach seems more reasonable that the others I am looking into ... convert to PDF or convert to XML.
How would I code  so that the second page would show up and the two would set next to each other or one below the other?
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 34990108
Try this

Sub AddCSR()
    Dim FullFile As Variant
    Dim work_book As Workbook
    Dim last_sheet As Worksheet, WS As Worksheet
    Dim OLEWd As OLEObject
    Dim WD As Word.Document
  
    Application.ScreenUpdating = False
    Set work_book = Application.ActiveWorkbook
    Set last_sheet = work_book.Sheets(work_book.Sheets.Count)
    Set WS = work_book.Sheets.Add(After:=last_sheet)
    
    WS.Name = "CSR1"
    
    FullFile = Application.GetOpenFilename _
    (" WORD files(*.doc),*doc", 1, "SELECT and OPEN the CSR1 File", , False)
    
    If VarType(FullFile) = vbBoolean Then
        MsgBox "No File Specified", vbExclamation
        Exit Sub
    End If
    
    With OLEWd
        Set OLEWd = ActiveSheet.OLEObjects.Add(Filename:=FullFile, Link:=False, DisplayAsIcon:=False)
        OLEWd.Verb xlVerbOpen
        OLEWd.Name = "CSR1"
        OLEWd.Width = 400
        OLEWd.Height = 800
        OLEWd.Top = 30
        OLEWd.Left = 0
        Set WD = OLEWd.Object
    End With
    
    With OLEWd
        Set OLEWd = ActiveSheet.OLEObjects.Add(Filename:=FullFile, Link:=False, DisplayAsIcon:=False)
        OLEWd.Verb xlVerbOpen
        OLEWd.Name = "CSR2"
        OLEWd.Width = 400
        OLEWd.Height = 800
        OLEWd.Left = 500
        OLEWd.Top = 30
        Set WD = OLEWd.Object
    End With
    Set WS = Nothing
    Set FullFile = Nothing
    Set OLEWd = Nothing
    Application.ScreenUpdating = True
End Sub

Open in new window


In the second document, manually click it and delete the 1st page.

Sid
0
 

Author Closing Comment

by:llawrenceg
ID: 34994322
SID:
Thank you so much . I think I can figure out the rest from here
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
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 create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 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

11 Experts available now in Live!

Get 1:1 Help Now