![]() |
|
|
|
#1
|
|||
|
|||
|
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) What am I doing wrong? How can I make it work?
|
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
Thanks for the suggestion! It works with a simple example. Now I will try to incorporate it into my main code.
|
|
#4
|
|||
|
|||
|
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?
|
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
Thank you! It looks like the solution I need. I will test it asap.
|
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
Scaffold: Please don't start multiple threads on closely related issues. Threads merged.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#9
|
|||
|
|||
|
I didn't know they are closely related. Even the issues themselves were different.
|
|
#10
|
||||
|
||||
|
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 |
|
#11
|
|||
|
|||
|
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
|
|
#12
|
||||
|
||||
|
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 |
|
#13
|
|||
|
|||
|
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. |
|
|
|
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 |
oshp.TextFrame.TextRange.Font <The specified error is out of range>
|
ghumdinger | PowerPoint | 5 | 12-09-2011 10:36 PM |