VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

codemonkey

I have a a requirement to open a Word doc from Excel and substitute text in a Text Box in the Word doc with text from the Excel spreadsheet.

I recorded a Macro with Word and it said to use ActiveDocument, however when I try to use this in my code within Excel, I get a Variable not defined error.

Here is my code -

Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

objWord.Documents.Open "C:\Users\kmccorma\Desktop\ReportPage.doc"

ActiveDocument.Shapes("Text Box 12").Select

I tried objWord.Shapes but I get an error saying Object doesnt support this property.

I can see the Shapes property is available with ActiveChart, do I need to do something with ActiveChart to get this to work?

Still relatively new to VB, so any help would be much appreciated.

Siddharth Rout

That is because, Excel doesn't recognize what ActiveDocument is. I would recommend using Objects. See this example.

Dim objWord As Object, objDoc As Object

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("C:\Users\kmccorma\Desktop\ReportPage.doc")

With objDoc.Shapes("Text Box 12")
    '~~> Do Something
    '.Select
End With

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

From Dev

How do I change text in a Word Doc from Excel VBA when Word Doc is located on a server?

From Dev

Exported excel file from datagridview shows error when opened VB

From Dev

Excel vb script crashes When trying to print or clear sheets

From Dev

VB.Net - when shall I use "New" word?

From Dev

VB.Net - when shall I use "New" word?

From Dev

Opening a word doc from excel, print a selection and a string on the same page

From Dev

Excel GoogleGeocode VB Error

From Dev

Excel GoogleGeocode VB Error

From Dev

Excel VBA modifying word doc from excel - collection member error

From Dev

Why do I get error when replacing datagridview values in vb?

From Dev

VB get data from excel - validation

From Dev

when completed with form on VB for excel my data doesnt get entered into excel

From Dev

Encoding error when opening an Excel file with xlrd

From Dev

XML file to Excel, error when opening

From Dev

Run time error 5981 when trying to create MS Word doc files from Access

From Dev

Run time error 5981 when trying to create MS Word doc files from Access

From Dev

How can I stop Excel from opening a second blank window when opening a file from Explorer?

From Dev

Excel not opening when exporting from reports manager

From Dev

VB Script If Statement - Opening Excel Workbook

From Java

Error when trying to read excel file from web site

From Dev

Why do I get an undefined variable error when trying to use PHP in two sections of a page?

From Dev

Word doc print from Excel Error Named argument not found

From Dev

Excel add-in not loading when opening Excel from VBA

From Dev

Error "Select method of Range class failed" when selecting range in VB.Net Excel workbook

From Dev

'DBNull' Error when attempting to set a range in VB.net. Database is Excel

From Dev

Excel 2007 macro: vb throws runtime '91' error when storing value in an array

From Dev

vb.net error when im trying to insert 2 statement

From Dev

Format Exception when trying to get date in VB.Net

Related Related

  1. 1

    VB Excel - When opening a word doc from VB with Excel I get a 'Variable not defined' error when trying to use ActiveDocument

  2. 2

    How do I change text in a Word Doc from Excel VBA when Word Doc is located on a server?

  3. 3

    Exported excel file from datagridview shows error when opened VB

  4. 4

    Excel vb script crashes When trying to print or clear sheets

  5. 5

    VB.Net - when shall I use "New" word?

  6. 6

    VB.Net - when shall I use "New" word?

  7. 7

    Opening a word doc from excel, print a selection and a string on the same page

  8. 8

    Excel GoogleGeocode VB Error

  9. 9

    Excel GoogleGeocode VB Error

  10. 10

    Excel VBA modifying word doc from excel - collection member error

  11. 11

    Why do I get error when replacing datagridview values in vb?

  12. 12

    VB get data from excel - validation

  13. 13

    when completed with form on VB for excel my data doesnt get entered into excel

  14. 14

    Encoding error when opening an Excel file with xlrd

  15. 15

    XML file to Excel, error when opening

  16. 16

    Run time error 5981 when trying to create MS Word doc files from Access

  17. 17

    Run time error 5981 when trying to create MS Word doc files from Access

  18. 18

    How can I stop Excel from opening a second blank window when opening a file from Explorer?

  19. 19

    Excel not opening when exporting from reports manager

  20. 20

    VB Script If Statement - Opening Excel Workbook

  21. 21

    Error when trying to read excel file from web site

  22. 22

    Why do I get an undefined variable error when trying to use PHP in two sections of a page?

  23. 23

    Word doc print from Excel Error Named argument not found

  24. 24

    Excel add-in not loading when opening Excel from VBA

  25. 25

    Error "Select method of Range class failed" when selecting range in VB.Net Excel workbook

  26. 26

    'DBNull' Error when attempting to set a range in VB.net. Database is Excel

  27. 27

    Excel 2007 macro: vb throws runtime '91' error when storing value in an array

  28. 28

    vb.net error when im trying to insert 2 statement

  29. 29

    Format Exception when trying to get date in VB.Net

HotTag

Archive