Selecting Text in an embedded OLE document

S

sphinney

I'm using Access 2007. I have an issue with the code below. The code is
from the Click event of a button control on a form. Also on the form is a
Bound Object Frame (called Word_bframe) and a Listbox (called TABLES_lbox).
When the user clicks the OPEN button, the code should:
1. Embed the Microsoft Word document designated in the WordDocPath variable
2. Set a reference to the activated Word application
3. Loop through all of the tables in the Word document
4. Select the first cell in each table
5. Add the contents of the first cell to a listbox

Private Sub OPEN_btn_Click()
Dim wrdAPP As Word.Application
Dim wrdDOC As Word.Document
Dim wrdSEL As Word.Selection
Dim wrdTBL As Word.Table

Me![WORD_bframe].OLETypeAllowed = acOLEEmbedded
Me![WORD_bframe].Class = "Word.Document"
Me![WORD_bframe].SourceDoc = WordDocPath
Me![WORD_bframe].SourceItem = ""
Me![WORD_bframe].Action = acOLECreateEmbed

Set wrdAPP = Me![WORD_bframe].Object.Application
Set wrdDOC = wrdAPP.Documents(1)
Set wrdSEL = wrdAPP.Selection

For Each wrdTBL In wrdDOC.Tables

wrdTBL.Cell(1, 1).Select
TABLES_lbox.RowSource = TABLES_lbox.RowSource &
TRIM_CELL(wrdSEL.Text) & ";"

Next wrdTBL

Set wrdSEL = Nothing
Set wrdDOC = Nothing
Set wrdAPP = Nothing

End Sub

The problem arises in the line "wrdTBL.Cell(1, 1).Select". The line does
not produce an error, but it also does not appear to work (i.e. it does not
actually select the cell). When the next line executes it errors because the
wrdSEL object is not defined (because nothing has been selected by the
previous line).

Why wont the "wrdTBL.Cell(1, 1).Select" line work?

Regards,
sphinney
 
S

sphinney

Hi, Alex:

Thanks for your response. I tried your suggestion but it didn't work.
Access tells me that "Text" is not a valid "Member or data member" of the
(Microsoft Word) Cell object.

I have successfully used code similar to what I provided in my first post
when using Word objects with out the bound object frame. When I define the
wrdAPP object as:

Set appWRD = CreateObject("Word.Application")

then the cell selection works. For some reason, when I define the wrdAPP
object as:

Set wrdAPP = Me![WORD_bframe].Object.Application

the cell selection doesn't work.

Any other ideas?

Thanks,
sphinney

Alex Dybenko said:
Hi,
not sure you need to select cell, try:
TABLES_lbox.RowSource = TABLES_lbox.RowSource & TRIM_CELL(wrdTBL.Cell(1,
1).Text) & ";"

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com

sphinney said:
I'm using Access 2007. I have an issue with the code below. The code is
from the Click event of a button control on a form. Also on the form is a
Bound Object Frame (called Word_bframe) and a Listbox (called
TABLES_lbox).
When the user clicks the OPEN button, the code should:
1. Embed the Microsoft Word document designated in the WordDocPath
variable
2. Set a reference to the activated Word application
3. Loop through all of the tables in the Word document
4. Select the first cell in each table
5. Add the contents of the first cell to a listbox

Private Sub OPEN_btn_Click()
Dim wrdAPP As Word.Application
Dim wrdDOC As Word.Document
Dim wrdSEL As Word.Selection
Dim wrdTBL As Word.Table

Me![WORD_bframe].OLETypeAllowed = acOLEEmbedded
Me![WORD_bframe].Class = "Word.Document"
Me![WORD_bframe].SourceDoc = WordDocPath
Me![WORD_bframe].SourceItem = ""
Me![WORD_bframe].Action = acOLECreateEmbed

Set wrdAPP = Me![WORD_bframe].Object.Application
Set wrdDOC = wrdAPP.Documents(1)
Set wrdSEL = wrdAPP.Selection

For Each wrdTBL In wrdDOC.Tables

wrdTBL.Cell(1, 1).Select
TABLES_lbox.RowSource = TABLES_lbox.RowSource &
TRIM_CELL(wrdSEL.Text) & ";"

Next wrdTBL

Set wrdSEL = Nothing
Set wrdDOC = Nothing
Set wrdAPP = Nothing

End Sub

The problem arises in the line "wrdTBL.Cell(1, 1).Select". The line does
not produce an error, but it also does not appear to work (i.e. it does
not
actually select the cell). When the next line executes it errors because
the
wrdSEL object is not defined (because nothing has been selected by the
previous line).

Why wont the "wrdTBL.Cell(1, 1).Select" line work?

Regards,
sphinney
 
S

sphinney

I've solved my problem. Essentially I just eliminated the reference to the
Selection object and used the following code:

Private Sub OPEN_btn_Click()
Dim wrdAPP As Word.Application
Dim wrdTBL As Word.Table

Me![WORD_bframe].OLETypeAllowed = acOLEEmbedded
Me![WORD_bframe].Class = "Word.Document"
Me![WORD_bframe].SourceDoc = WordDocPath
Me![WORD_bframe].SourceItem = ""
Me![WORD_bframe].Action = acOLECreateEmbed

Set wrdAPP = Me![WORD_bframe].Object.Application

For Each wrdTBL In wrdAPP.Documents(1).Tables

wrdTBL.Cell(1, 1).Select

TABLES_lbox.RowSource = TABLES_lbox.RowSource &
TRIM_CELL(wrdAPP.Selection.Text) & ";"

Next wrdTBL

Set wrdAPP = Nothing

End Sub

I'm not sure why the reference to the Selection object didn't work, but my
code works now without it so there's no need to continue taking up people's
time.

Thanks to everyone who read this post.
sphinney

sphinney said:
Hi, Alex:

Thanks for your response. I tried your suggestion but it didn't work.
Access tells me that "Text" is not a valid "Member or data member" of the
(Microsoft Word) Cell object.

I have successfully used code similar to what I provided in my first post
when using Word objects with out the bound object frame. When I define the
wrdAPP object as:

Set appWRD = CreateObject("Word.Application")

then the cell selection works. For some reason, when I define the wrdAPP
object as:

Set wrdAPP = Me![WORD_bframe].Object.Application

the cell selection doesn't work.

Any other ideas?

Thanks,
sphinney

Alex Dybenko said:
Hi,
not sure you need to select cell, try:
TABLES_lbox.RowSource = TABLES_lbox.RowSource & TRIM_CELL(wrdTBL.Cell(1,
1).Text) & ";"

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com

sphinney said:
I'm using Access 2007. I have an issue with the code below. The code is
from the Click event of a button control on a form. Also on the form is a
Bound Object Frame (called Word_bframe) and a Listbox (called
TABLES_lbox).
When the user clicks the OPEN button, the code should:
1. Embed the Microsoft Word document designated in the WordDocPath
variable
2. Set a reference to the activated Word application
3. Loop through all of the tables in the Word document
4. Select the first cell in each table
5. Add the contents of the first cell to a listbox

Private Sub OPEN_btn_Click()
Dim wrdAPP As Word.Application
Dim wrdDOC As Word.Document
Dim wrdSEL As Word.Selection
Dim wrdTBL As Word.Table

Me![WORD_bframe].OLETypeAllowed = acOLEEmbedded
Me![WORD_bframe].Class = "Word.Document"
Me![WORD_bframe].SourceDoc = WordDocPath
Me![WORD_bframe].SourceItem = ""
Me![WORD_bframe].Action = acOLECreateEmbed

Set wrdAPP = Me![WORD_bframe].Object.Application
Set wrdDOC = wrdAPP.Documents(1)
Set wrdSEL = wrdAPP.Selection

For Each wrdTBL In wrdDOC.Tables

wrdTBL.Cell(1, 1).Select
TABLES_lbox.RowSource = TABLES_lbox.RowSource &
TRIM_CELL(wrdSEL.Text) & ";"

Next wrdTBL

Set wrdSEL = Nothing
Set wrdDOC = Nothing
Set wrdAPP = Nothing

End Sub

The problem arises in the line "wrdTBL.Cell(1, 1).Select". The line does
not produce an error, but it also does not appear to work (i.e. it does
not
actually select the cell). When the next line executes it errors because
the
wrdSEL object is not defined (because nothing has been selected by the
previous line).

Why wont the "wrdTBL.Cell(1, 1).Select" line work?

Regards,
sphinney
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top