Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-24-2022, 10:42 AM
littlepeaks littlepeaks is offline access active cell in word vba Windows 10 access active cell in word vba Office 2010 32bit
Novice
access active cell in word vba
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default access active cell in word vba

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
Reply With Quote
  #2  
Old 08-24-2022, 04:12 PM
Guessed's Avatar
Guessed Guessed is offline access active cell in word vba Windows 10 access active cell in word vba Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 08-24-2022, 04:59 PM
macropod's Avatar
macropod macropod is offline access active cell in word vba Windows 10 access active cell in word vba Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #4  
Old 08-24-2022, 05:16 PM
Guessed's Avatar
Guessed Guessed is offline access active cell in word vba Windows 10 access active cell in word vba Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 08-24-2022, 07:09 PM
littlepeaks littlepeaks is offline access active cell in word vba Windows 10 access active cell in word vba Office 2010 32bit
Novice
access active cell in word vba
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 08-24-2022, 09:14 PM
macropod's Avatar
macropod macropod is offline access active cell in word vba Windows 10 access active cell in word vba Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Guessed View Post
I would happily debate the value of turning off/on screen updating though since the code is not changing any view or content.
All depends on what's going to be done with the found string.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Move active cell down Marcia Excel Programming 2 06-22-2022 04:10 PM
access active cell in word vba Change active cell after running macro in active cell via selection change 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
access active cell in word vba Clear all cell colors within a range starting at cell A8 and change row of active cell to yellow FUGMAN Excel Programming 7 02-05-2017 08:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft