Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-12-2022, 07:36 AM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Unhappy Using textframe value in comparison

Hello everyone!


I need a macro that would do something with a textframe if it contains a specific text.
I try using the following code for comparison:
Code:
Dim value As String
value = shp.TextFrame.TextRange.Text
Dim res As Integer
res = StrComp(value, "testvalue", 1)
The problem is that StrComp function does not consider my values as equal even if they are 100% equal. In other words, it never returns 0.
What am I doing wrong? How can I make it work?
Reply With Quote
  #2  
Old 03-12-2022, 07:51 AM
gmayor's Avatar
gmayor gmayor is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Try the following
Code:
Sub Macro1()
Dim oShape As Shape
Dim TestValue As String
    TestValue = "test"
    For Each oShape In ActiveDocument.Shapes
        If InStr(1, oShape.TextFrame.TextRange, TestValue) > 0 Then
            'do something with the shape e.g.
            MsgBox oShape.TextFrame.TextRange
        End If
    Next oShape
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-12-2022, 09:02 AM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Default

Thanks for the suggestion! It works with a simple example. Now I will try to incorporate it into my main code.
Reply With Quote
  #4  
Old 03-13-2022, 10:00 AM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Default Obtaining part of the textframe value as range

Hello everyone!
I need to select part of the text contained in the textframe as range to modify its appearance. But I can't understand how to do this. Basically, I need to select the text starting from the second character and until the end. Any advice?
Reply With Quote
  #5  
Old 03-13-2022, 03:11 PM
Guessed's Avatar
Guessed Guessed is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

I'll assume the textframe in question is the one you already have selected.
Code:
Sub Demo()
  Dim aRng As Object
  Selection.ShapeRange(1).TextFrame.TextRange.Select
  Set aRng = Selection.Range
  aRng.MoveStart Unit:=wdCharacter, Count:=1
  aRng.Select
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 03-13-2022, 03:33 PM
macropod's Avatar
macropod macropod is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

Scaffold: Please don't start multiple threads on closely related issues. Threads merged.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-13-2022, 10:56 PM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Scaffold: Please don't start multiple threads on closely related issues. Threads merged.
I didn't know they are closely related. Even the issues themselves were different.
Reply With Quote
  #8  
Old 03-13-2022, 10:57 PM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
I'll assume the textframe in question is the one you already have selected.
Code:
Sub Demo()
  Dim aRng As Object
  Selection.ShapeRange(1).TextFrame.TextRange.Select
  Set aRng = Selection.Range
  aRng.MoveStart Unit:=wdCharacter, Count:=1
  aRng.Select
End Sub
Thank you! It looks like the solution I need. I will test it asap.
Reply With Quote
  #9  
Old 03-14-2022, 04:11 AM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
I'll assume the textframe in question is the one you already have selected.
Code:
Sub Demo()
  Dim aRng As Object
  Selection.ShapeRange(1).TextFrame.TextRange.Select
  Set aRng = Selection.Range
  aRng.MoveStart Unit:=wdCharacter, Count:=1
  aRng.Select
End Sub
it works, but I have one more question: how do I unselect the range? After the macro finihses working, the selection remains active. I tried Application.Selection.EndOf and Selection.Collapse but it doesn't help. Can it be related to the fact that my textframes are contained in the header/footer area?

Last edited by Scaffold; 03-14-2022 at 11:42 AM.
Reply With Quote
  #10  
Old 03-14-2022, 03:07 PM
Guessed's Avatar
Guessed Guessed is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

Post your code and a sample document. The earlier question was how to select something so it makes no sense to unselect it in the same macro if you do nothing with the selection.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #11  
Old 03-14-2022, 11:14 PM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Default

Yes, with pleasure. I can't share the whole project, but the excerpt I'm sending reproduces the problem. I do nothing special with the selected range, just push it into subscript. The problem is that I don't know how to cancel the selection that sticks after the macro finished working. Please find the file "example.docm" in the attachement. The code is the following:
Code:
Sub TestMacro()

    Application.ScreenUpdating = False
    ActiveDocument.PrintPreview
    ActiveDocument.ClosePrintPreview
    
    Dim shp As Shape
    
    For Each shp In ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
    
        If shp.Type = msoTextBox Then
    
            Set aRng = shp.TextFrame.TextRange
            aRng.MoveStart Unit:=wdCharacter, Count:=1
            aRng.Select
            aRng.Font.Subscript = True
        
        End If
   
    Next
    
    Application.ScreenUpdating = True
        
End Sub
Attached Files
File Type: docm example.docm (25.2 KB, 7 views)
Reply With Quote
  #12  
Old 03-14-2022, 11:40 PM
Guessed's Avatar
Guessed Guessed is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

The most logical thing is not to select it in the first place
Code:
Sub TestMacro()
  Dim shp As Shape, aRng As Range
  For Each shp In ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
    If shp.Type = msoTextBox Then
        Set aRng = shp.TextFrame.TextRange
        aRng.MoveStart Unit:=wdCharacter, Count:=1
        aRng.Font.Subscript = True
    End If
  Next
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #13  
Old 03-15-2022, 12:16 AM
Scaffold Scaffold is offline Using textframe value in comparison Windows 10 Using textframe value in comparison Office 2016
Advanced Beginner
Using textframe value in comparison
 
Join Date: Oct 2009
Location: Moscow, Russia
Posts: 36
Scaffold is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
The most logical thing is not to select it in the first place
This is one big mighty "Oops"! I normally program with C# and for the different API. I'm struggling with the MS Office API.
Thank you very much! This solves it for me.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Document Comparison Thefirstfish` Word VBA 0 04-10-2017 01:07 PM
Comparison (with styles) Macro aarona Word VBA 0 03-03-2017 11:18 PM
Saving & reopening comparison MarshallAbrams Word 0 03-29-2012 06:19 AM
Comparison jumps to end MarshallAbrams Word 2 03-21-2012 04:04 AM
Using textframe value in comparison oshp.TextFrame.TextRange.Font <The specified error is out of range> ghumdinger PowerPoint 5 12-09-2011 10:36 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:03 AM.


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