Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-08-2023, 03:18 PM
NicoleJones NicoleJones is offline VBA detecting style used Windows 10 VBA detecting style used Office 2016
Novice
VBA detecting style used
 
Join Date: Jan 2020
Posts: 9
NicoleJones is on a distinguished road
Default VBA detecting style used


Is it possible to write VBA code to detect the style currently used? I am creating a macro to insert and format a table and image caption, however the style used for these captions depend on where it is in the document e.g. the caption sits further in if there is a heading above, for paragraph text I have created an 'indented normal' style, so I am wanting the code to see the 'indented normal' and apply an 'indented caption' style.
Reply With Quote
  #2  
Old 03-08-2023, 05:07 PM
Guessed's Avatar
Guessed Guessed is offline VBA detecting style used Windows 10 VBA detecting style used Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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

Yes it is possible but you haven't provided enough information in order to create the code.

How is the macro going to work out which section it is in? The macro can look backwards or forwards to see if a certain piece of content is above or below it but we don't know what this logic is from what you described. The 'heading above' may be one or more paragraphs above and we don't know what the scope of that is yet.

Maybe you should post a sample document showing what it is you want to achieve and what the styles are that should be factors
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 03-09-2023, 03:41 AM
NicoleJones NicoleJones is offline VBA detecting style used Windows 10 VBA detecting style used Office 2016
Novice
VBA detecting style used
 
Join Date: Jan 2020
Posts: 9
NicoleJones is on a distinguished road
Default

Hi Andrew, I am ok to apply a style, I just don't know how to write the code to determine what style is being used on the active selected text.
Reply With Quote
  #4  
Old 03-09-2023, 03:35 PM
Guessed's Avatar
Guessed Guessed is offline VBA detecting style used Windows 10 VBA detecting style used Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
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

It is pretty much the same way. At its simplest you could follow this pattern
Code:
Sub GetStyled()
  Dim sSty As String
  sSty = Selection.Style
  If sSty = "Normal" Then
    Selection.Style = "Heading 2"
  Else
    Selection.Style = "Heading 3"
  End If
End Sub
If your selection includes multiple styles (eg more than one paragraph selected or character style + paragraph style) then you may need to complicate the code to get the right results.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 03-11-2023, 03:59 AM
ChrisGreaves ChrisGreaves is offline VBA detecting style used Windows 10 VBA detecting style used Office 97-2003
Novice
 
Join Date: Nov 2022
Location: Bonavista, Newfoundland
Posts: 22
ChrisGreaves is on a distinguished road
Default

Quote:
Originally Posted by NicoleJones View Post
Is it possible to write VBA code to detect the style currently used?
Hi Nicole. I have a slew of utility procedures for manipulating styles. The attached function may give you some ideas.
This is a self-testing function; drag the commented macro outside of the function then de-comment the macro. Run the test macro until it works, then make changes to the function and insert new test lines and rerun the test macro. Comment the macro and drag it back into the function.
Cheers, Chris
Attached Files
File Type: txt Public Function blnStyleFoundInUseI.txt (1.5 KB, 9 views)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA detecting style used Need help detecting the reason for a strange behavior of a UDF soroush.kalantari Excel Programming 9 08-12-2022 07:05 PM
Detecting Write Access Password azizrasul PowerPoint 0 04-05-2021 10:56 AM
Detecting an active instance of Excel from Word VBA Sorcerer13 Word VBA 17 06-02-2014 02:48 AM
Detecting last cell within a block Funk Excel 12 10-01-2013 04:26 AM
VBA detecting style used detecting triggers yisuz PowerPoint 1 05-15-2012 09:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:44 PM.


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