Solved

Embed 2 page Word Doc in Excel Worksheet

Posted on 2011-02-25
7
501 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
[X]
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
  • 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
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!

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

751 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