Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Embed 2 page Word Doc in Excel Worksheet

Posted on 2011-02-25
7
498 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

860 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