counting word tables above a cursor location

A

akh2103

Hello I am writing a macro that is trying to import a variable number
of word tables from the middle of a many-table document into excel. My
macro starts with a sub procedure that selects the first table to be
imported via an excel userform that searches the word document and
finds a cell with certain identifying text. What I want to do is then
import a certain variable number of tables following that found table.
I am somewhat unfamiliar with programming in word, so I have no idea
how to proceed. Can anybody suggest a path to do this? Thanks.
-Abe
 
J

Jonathan West

Hello I am writing a macro that is trying to import a variable number
of word tables from the middle of a many-table document into excel. My
macro starts with a sub procedure that selects the first table to be
imported via an excel userform that searches the word document and
finds a cell with certain identifying text. What I want to do is then
import a certain variable number of tables following that found table.
I am somewhat unfamiliar with programming in word, so I have no idea
how to proceed. Can anybody suggest a path to do this? Thanks.
-Abe

iTablesAfterSelection = ActiveDocument.Range(Selection.End, _
ActiveDocument.Range.End).Tables.Count

iTablesBeforeSelection = ActiveDocument.Range(0,
Selection.Start).Tables.Count


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

akh2103

Thanks--I am glad to know it is possible. However, when I try, VB is
giving me a
"Run-time Error '450':
wrong number of arguments or invalid property assignment"

I have copied and pasted in your code. DO you have any idea what is
going wrong? Thanks for your help.
-Abe
 
A

akh2103

Hi Jonathan--thanks for getting back to me. I have changed the word
wrap on your code to proper VB form in excel, but i am still having
trouble. I think it has something to do with the count property because
when I delete the "itablesbeforecount=" bit VB gives me an "object
doesn't support this property of method error." This, to me, doesn't
make sense as I have used count on activedocuments.tables without
trouble before. Any idea what else could be the trouble. I am stumped,
both on this and on my project. Take care. -Abe
 
A

akh2103

Hi Jonathan--thanks for getting back to me. I have changed the word
wrap on your code to proper VB form in excel, but i am still having
trouble. I think it has something to do with the count property because
when I delete the "itablesbeforecount=" bit VB gives me an "object
doesn't support this property of method error." This, to me, doesn't
make sense as I have used count on activedocuments.tables without
trouble before. Any idea what else could be the trouble. I am stumped,
both on this and on my project. Take care. -Abe
 
J

Jonathan West

Hi Jonathan--thanks for getting back to me. I have changed the word
wrap on your code to proper VB form in excel, but i am still having
trouble. I think it has something to do with the count property because
when I delete the "itablesbeforecount=" bit VB gives me an "object
doesn't support this property of method error." This, to me, doesn't
make sense as I have used count on activedocuments.tables without
trouble before. Any idea what else could be the trouble. I am stumped,
both on this and on my project. Take care. -Abe

Ah, I hadn't realised you were running the code from within Excel. These two
articles will describe the general principles of controlling one application
from another

Control Word from Excel
http://www.word.mvps.org/FAQs/InterDev/ControlWordFromXL.htm

Early vs. Late Binding
http://www.word.mvps.org/FAQs/InterDev/EarlyvsLateBinding.htm

If you choose early binding (setting a reference to Word in Tools
References) then the code should work with little or no modification. if you
choose late binding, you will need to prepend the name of the object
variable you use to reference your instance of Word, something like this
(supposing your object variable is called wrdApp)

iTablesAfterSelection = wrdApp.ActiveDocument.Range(wrdApp.Selection.End, _
wrdApp.ActiveDocument.Range.End).Tables.Count

iTablesBeforeSelection = wrdApp.ActiveDocument.Range(0,
wrdApp.Selection.Start).Tables.Count

(By the way, you probably won't need both of these lines of code. One counts
the tables between the selection and the end of the document, the other
counts the tables between the start of the document and the selection.
Choose the one you need)


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

akh2103

Thanks so much. Works great -Abe

Jonathan said:
Ah, I hadn't realised you were running the code from within Excel. These two
articles will describe the general principles of controlling one application
from another

Control Word from Excel
http://www.word.mvps.org/FAQs/InterDev/ControlWordFromXL.htm

Early vs. Late Binding
http://www.word.mvps.org/FAQs/InterDev/EarlyvsLateBinding.htm

If you choose early binding (setting a reference to Word in Tools
References) then the code should work with little or no modification. if you
choose late binding, you will need to prepend the name of the object
variable you use to reference your instance of Word, something like this
(supposing your object variable is called wrdApp)

iTablesAfterSelection = wrdApp.ActiveDocument.Range(wrdApp.Selection.End, _
wrdApp.ActiveDocument.Range.End).Tables.Count

iTablesBeforeSelection = wrdApp.ActiveDocument.Range(0,
wrdApp.Selection.Start).Tables.Count

(By the way, you probably won't need both of these lines of code. One counts
the tables between the selection and the end of the document, the other
counts the tables between the start of the document and the selection.
Choose the one you need)


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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