I have the following code:
For Each DocPara In ActiveDocument.Paragraphs
If (DocPara.style = "Title 1") Then
...
Else
(if DocPara is LIST then)
...
(else if DocPara is TABLE then)
...
End If
Next DocPara
Is there any way to know if the current paragraph is an IMAGE.
The current paragraph cannot "be" an image because any image is always a character in a paragraph OR anchored to a paragraph. It would be necessary to count the number of images in / attached to the paragraph's range.
So a paragraph cannot be just an image, it will always contain at least one string character (ANSI 13, the paragraph mark) and may contain an unlimited number besides an image.
Word supports two kinds of images: InlineShapes and Shapes. The first are handled the same as characters; the latter have text-wrap formatting.
An image formatted with text-wrap may appear to be "in" a paragraph, but is not, and may not even be anchored to the paragraph where it appears. So when the type of image in question is a Shape it's not really possible to determine whether there's an image "in" a paragraph by querying the Paragraph object.
Here's the code to determine whether an InlineShape is in a paragraph and whether any Shapes are anchored to a paragraph. Based on the way your question is phrased I'm hoping your images are InlineShapes...
Dim rngPara as Word.Range
Set rngPara = DocPara.Range
If rngPara.InlineShapes.Count > 0 Then 'the paragraph contains an image
If rngPara.ShapeRange.Count > 0 Then 'an image is anchored to the paragraph
Related
I have code that searches for ^p and replaces with ^p^p to insert a blank line between every paragraph.
I'd like this to not apply to paragraphs that are entirely bold (i.e. subheadings).
So "search for ^p and replace with ^p^p except for cases where the entire paragraph is bold".
I have a sort-of fix where I search for entirely bold paragraphs and delete the paragraph immediately following (which should just be a blank line after the search ^p and replace ^p^p.
The code below deletes the character immediately following an entirely bold paragraph.
Dim para As Paragraph
Dim searchRange As Range
Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
If para.Range.Font.Bold = True And Not para.Range.Information(wdWithInTable) Then para.Range.Next.Delete
Next para
The problem is it does it in tables. So if I have a table header that is bold, it deletes the paragraph following, which will be the text in the next row of the table.
How can I get this bit of code to effectively check if a paragraph is entirely bold, and then delete the paragraph following it, UNLESS it is in a table?
Also if I could do the same but exempting if the following paragraph is a certain style, e.g. List Bullet.
The answer is in the code you are already using. To work with the following paragraph use para.Next, as you are already doing for the deletion.
Simply change:
And Not para.Range.Information(wdWithInTable)
to:
And Not para.Next.Range.Information(wdWithInTable)
I'm currently developing a Catia vba script to look for specific texts in a plan, within the Drawing workbench.
For this particular macro, I need to search bold text. But the thing is I know that the DrawingText object has an attributed called bold that evaluates to True if ALL the text is bolded, but not if just a word is bolded.
Example:
This is my text -> True
This is my text -> False
What would be a good way to indentify this partially bolded DrawingText objects?
aText.GetParameterOnSubString(catBold,startPos,numChars)
will return 1 if all the characters in the specifed substring are Bold, 0 otherwise.
So first check if your sub-string is in the text by using InStr(aText.Text,subString). and if it is, use this test to see if is in Bold.
Is there a way to count number of rows of texts inside a Visio shape? Such as linecount?
I've tried Rowcount on a Visio shape and it didn't return anything reflecting the text lines inside the Visio Shape! Below is that sample code I created
Sub something()
Dim intRows
Dim vsoShape As Visio.Shape
Set vsoShape = ActiveWindow.Selection.PrimaryItem
intRows = vsoShape.RowCount(Visio.visSectionProp)
MsgBox intRows
End Sub
There is no built-in way to count the number of lines of text, that I'm aware of.
The RowCount is for counting the number of rows in a particular shapesheet section.
You can call the BoundingBox method on a shape and get back the height and width of the text area for the shape, but you'd have to guess at how many lines that is, maybe as a function of the font size.
If you are able to enforce a standard font and character size on shapes in the diagrams you're working with, you should be able to tell how many lines there are based on the height of the text box.
I'm assuming you're asking after the number of line wraps that Visio has done, not the number of line breaks in the text.
I have a Word Document that was created by saving a PDF document as a Word Document. The PDF document has some text that was highlighted in Yellow. I need to detect the highlighted text. I guess the conversion is not perfect. Some of the text in the Word Document is showing that it is highlighted, and some just has a Yellow background but is not showing as being highlighted. I can select some of the text and click on the highlight button on the menu and it shows a highlight color, and for some of the words, it shows No Color.
Also in the VBA immediate window if I type
?selection.formattedtext.highlightcolorindex, I get 7 for some selections, and 0 for others.
For those selections that do not show up as highlighted, how can I detect them?
Update: I selected a "Highlighted" word and executed call Selection.ClearFormatting and the font changed, but the yellow highlight remained. So that tells me that the yellow highlight is not a format. I also selected No Color with the text selected and the yellow highlight remained. I have verified that it is neither Highlight or Shading. Hmmm... What could be causing the highlight?
Update: I discovered that if I delete the "highlighted text" that is not really highlighted, the text that is to the right of the deleted text moves over to the left and the highlight remains. So then I discovered that I can Select the highlight if there is no text under a portion of the highlight. Then I can drag the highlight to another portion of the page. So, this highlight is an object. If I right click on it, I have options such as Format Object.
So now I know that these "Highlights" are actually like objects. If there is no text under the object, I can grab it and drag it around. How can I, using VBA, find these types of objects?
What I really want is the entire paragraph under the highlight objects. Is that possible?
Well, I figured out how to do it. I used another StackOverflow question to lead me to the answer How do you select an image that was just pasted to MS Word via VBA
I am running this code from Microsoft Access on a Microsoft Word object.
Public Sub ReadWord2(strPath As String)
'Const wdColorRed = 255
Dim objWord As Object
Dim objDoc As Word.Document
Dim strParagraph As String
Dim intShapeCount As Integer
Dim Shape As Word.Shape
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open(strPath)
Set objSelection = objWord.Selection
intShapeCount = objDoc.Shapes.Count
'objDoc.Shapes.SelectAll
For Each Shape In objDoc.Shapes
'If Shape.AutoShapeType = 1 Then
Shape.Select
'Debug.Print Shape.Fill.BackColor
'Debug.Print Shape.AutoShapeType, Shape.Name
'Debug.Print Shape.TextFrame.TextRange.Text
objSelection.Expand wdParagraph
Debug.Print objSelection.Text, Shape.Fill.BackColor
'End If
Next
End Sub
There is actually more code in the sub, but I deleted the parts that are not relevant. Hopefully this will help someone else who runs into the same problem. The objSelection.Text will contain the paragraph text that the shape is part of. Now I just need to write code to handle the text and make sure that it is text that I want.
I am looking a way to convert the Bullets in Word document to simple text. E.g.
I have these kind of Bullets:
a)-> Apple
b)-> Orange
c)-> Mangoes
I want them to be like this:
a)Apple
b)Oranges
c)Mangoes
I am using this code but it removes the Bullets entirely:
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs()
Set r = oPara.Range
If r.ListFormat.RemoveNumbers = wdListBullet Then
r.ListFormat.ApplyListTemplate _
ListTemplate:=ListGalleries(wdNumberGallery) _
.ListTemplates(1)
End If
Set r = Nothing
Next
Is ActiveDocument.ConvertNumbersToText what you're after?
It can also be run on a specific list if you're not doing this globally.
ETA: It seems like ConvertNumbersToText takes a NumberType argument (this isn't documented by the 2010 spec that F1 brings up, but it is valid). Perhaps the default doesn't apply to all the bullets in your document. A combination of the three possibilities might work.
ActiveDocument.ConvertNumbersToText(wdNumberParagraph) 'Preset numbers you can add to paragraphs by selecting a template in the Bullets and Numbering dialog box.
ActiveDocument.ConvertNumbersToText(wdNumberListNum) 'Default value for LISTNUM fields.
ActiveDocument.ConvertNumbersToText(wdNumberAllNumbers) 'Default value for all other cases.
I tend to use the first one, but your case might be different.