![]() |
|
#1
|
|||
|
|||
![]()
This has to do with an Avery Label table in VBA.
Is there any way in VBA to access the active cell in a Word table? I would like to copy the text out of the active cell into a string variable. I can't find any documentation on this. All I can find is Active Cell in Excel, which doesn't work for Word. BTW, I have the latest MS Word installed. Thanks |
#2
|
||||
|
||||
![]()
There are a couple of tricks involved here. First is to avoid an error if the selection doesn't include any table elements. Second is to avoid having the end of cell marker as part of your string.
Code:
Sub Macro1() Dim str As String, aCell As Cell If Selection.Tables.Count > 0 Then str = Selection.Cells(1).Range.Text str = Left(str, Len(str) - 2) End If MsgBox "Text in first selected cell: " & str End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
||||
|
||||
![]()
Better (IMHO)
Code:
Sub Demo() Application.ScreenUpdating = False Dim Rng As Range With Selection If .Information(wdWithInTable) = True Then Set Rng = .Cells(1).Range Rng.End = Rng.End - 1 MsgBox Rng.Text Else MsgBox "The selection is not in a table", vbCritical End If End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
||||
|
||||
![]()
Certainly cleaner than my version.
I would happily debate the value of turning off/on screen updating though since the code is not changing any view or content. ![]()
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
![]()
Ahhh - Selection -- got it
That worked -- Macropod -- did not get to try yours yet -- didn't see yours until I after I tried Guessed's macro. Thanks both of you for the info -- I have something to work with now. ![]() |
#6
|
||||
|
||||
![]()
All depends on what's going to be done with the found string.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Move active cell down | Marcia | Excel Programming | 2 | 06-22-2022 04:10 PM |
![]() |
Peterson | Excel Programming | 3 | 03-07-2021 12:19 PM |
Cell equal value new value of active cell which is a drop down | hassanmoukadem | Excel Programming | 1 | 04-13-2020 07:54 PM |
Active cell not the one I select with curser. | hayesfam3 | Excel | 0 | 08-15-2019 05:14 AM |
![]() |
FUGMAN | Excel Programming | 7 | 02-05-2017 08:37 AM |