#1
|
|||
|
|||
handle textbox, pictures, and all kind of shapes
hello dears
i found this macro which remove textbox and rewrite its content in body text, but i need to develope it more to make the same in opictures or other shapes (sometimes people put picture in word and type text on it and this make problem for me) Code:
Sub RemoveTextBox2() Dim shp As Shape Dim oRngAnchor As Range Dim sString As String For Each shp In ActiveDocument.Shapes If shp.Type = msoTextBox Then ' copy text to string, without last paragraph mark sString = Left(shp.TextFrame.TextRange.Text, _ shp.TextFrame.TextRange.Characters.Count - 1) If Len(sString) > 0 Then ' set the range to insert the text Set oRngAnchor = shp.Anchor.Paragraphs(1).Range ' insert the textbox text before the range object oRngAnchor.InsertBefore _ "Textbox start << " & sString & " >> Textbox end" End If shp.delete End If Next shp End Sub thanks Last edited by macropod; 02-24-2017 at 08:21 PM. Reason: Added code tags |
#2
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Shp As Shape, Rng As Range With ActiveDocument For Each Shp In .Shapes With Shp If Not .TextFrame Is Nothing Then If Len(Trim(.TextFrame.TextRange.Text)) > 1 Then Set Rng = .Anchor With Rng .InsertBefore "Textbox start << " .Collapse wdCollapseEnd .InsertAfter " >> Textbox end" .Collapse wdCollapseStart End With Rng.FormattedText = .TextFrame.TextRange.FormattedText End If .Delete End If End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
hello macropod
thanks for your help, is this code will work only on office 2010? also i notice from code it will insert text only from textbox, and what i need to retype text from all word arts (textbox, ppicture, autoshapes ...etc), also removing any background picture. thanks very much. |
#4
|
||||
|
||||
The code works in all Word versions. If the text is part of an image, it is no longer text, but pixels in an image; in that case, you need OCR software, not a macro.
Since the code you posted only concerned textboxes that were shape objects, that is what I showed you how to extend; you did not mention the need to process inlineshapes. The mere fact the code reports all the extracts as being from textboxes is inconsequential; outputting each & every shape type as well would require considerably more code - there are 24 different shape types. WordArt is normally not a Shape object but an InlineShape object; if you want to extract such text, you need to process InlineShape objects as well. I suggest you have a go at that - it's not a major undertaking.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
hello macropod
am sorry for any mesunderstod, i'm visually impaired so i not use such objects normal / inline shapes just i want to remove all shapes inline / normal without lose its text, sure i know the different between text inside image which need ocr and text which written above these objects in word file. also, i tried your code in a document contains textbox and other shapes it not extract text from textbox or delete them for this asked you about office version. now hope you help me in this macro or guide me to handel it by myself if you not have time. again thanks very much for your efforts with me. |
#6
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim i As Long, Rng As Range, StrType With ActiveDocument For i = .Shapes.Count To 1 Step -1 With .Shapes(i) If Not .TextFrame Is Nothing Then Select Case .Type Case msoAutoShape: StrType = "AutoShape" Case msoCallout: StrType = "Callout" Case msoCanvas: StrType = "Canvas" Case msoChart: StrType = "Chart" Case msoComment: StrType = "Comment" Case msoDiagram: StrType = "Diagram" Case msoEmbeddedOLEObject: StrType = "EmbeddedOLEObject" Case msoFormControl: StrType = "FormControl" Case msoFreeform: StrType = "Freeform" Case msoGroup: StrType = "Group" Case msoInk: StrType = "Ink" Case msoInkComment: StrType = "InkComment" Case msoLine: StrType = "Line" Case msoLinkedOLEObject: StrType = "LinkedOLEObject" Case msoLinkedPicture: StrType = "LinkedPicture" Case msoMedia: StrType = "Media" Case msoOLEControlObject: StrType = "OLEControlObject" Case msoPicture: StrType = "Picture" Case msoPlaceholder: StrType = "Placeholder" Case msoScriptAnchor: StrType = "ScriptAnchor" Case msoShapeTypeMixed: StrType = "ShapeTypeMixed" Case msoTable: StrType = "Table" Case msoTextBox: StrType = "TextBox" Case msoTextEffect: StrType = "TextEffect" End Select If Len(Trim(.TextFrame.TextRange.Text)) > 1 Then Set Rng = .Anchor With Rng .InsertBefore StrType & " start << " .Collapse wdCollapseEnd .InsertAfter " >> end " & StrType .Collapse wdCollapseStart End With Rng.FormattedText = .TextFrame.TextRange.FormattedText End If .Delete End If End With Next For i = .InlineShapes.Count To 1 Step -1 With .InlineShapes(i) If Not .TextEffect Is Nothing Then If Len(Trim(.TextEffect.Text)) > 1 Then Select Case .Type Case wdInlineShapeChart: StrType = "InlineChart" Case wdInlineShapeDiagram: StrType = "InlineDiagram" Case wdInlineShapeEmbeddedOLEObject: StrType = "InlineEmbeddedOLEObject" Case wdInlineShapeHorizontalLine: StrType = "InlineHorizontalLine" Case wdInlineShapeLinkedOLEObject: StrType = "InlineLinkedOLEObject" Case wdInlineShapeLinkedPicture: StrType = "InlineLinkedPicture" Case wdInlineShapeLinkedPictureHorizontalLine: StrType = "InlineShapeLinkedPictureHorizontalLine" Case wdInlineShapeLockedCanvas: StrType = "InlineLockedCanvas" Case wdInlineShapeOLEControlObject: StrType = "InlineOLEControlObject" Case wdInlineShapeOWSAnchor: StrType = "InlineOWSAnchor" Case wdInlineShapePicture: StrType = "InlinePicture" Case wdInlineShapePictureBullet: StrType = "InlinePictureBullet" Case wdInlineShapePictureHorizontalLine: StrType = "InlinePictureHorizontalLine" Case msoLinkedOLEObject: StrType = "LinkedOLEObject" Case wdInlineShapeScriptAnchor: StrType = "InlineScriptAnchor" End Select Set Rng = .Range With Rng .Collapse wdCollapseStart .InsertBefore StrType & " start << " .Collapse wdCollapseEnd .InsertAfter " >> end " & StrType .Collapse wdCollapseStart End With Rng.Text = .TextEffect.Text End If .Delete End If End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
dear macropod
kindly note that not all forum feature accessible for me with screen reader i not able to use add reputation. but for sure you and others in this forum help me more. am so sorry sir if i not able to show my thanks in ur profile. |
#8
|
|||
|
|||
dear macropod
again thanks for your efforts in your code it gave me errowr in compiling "sub or function not defined" and i able to fix it the problem was in this line: With InlineShapes(i) i fixed to: With .InlineShapes(i) but i found runtime errowr when use macro in a document erowr 5917 and am try to fix it, If Len(Trim(.TextFrame.TextRange.Text)) > 1 Then wish help me in this and i'll try it on another ofice version. many thanks for your advice. Last edited by romanticbiro; 02-27-2017 at 03:25 PM. Reason: correcting reply |
#9
|
||||
|
||||
Since the expression:
If Len(Trim(.TextFrame.TextRange.Text)) > 1 Then is unchanged from the previous working version of the macro, I can't see why you'd get a error 5917 unless you changed something else in the code. What is the error message and what does StrType contain at that point?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
dear macropod
run time errowr 5917 (this object does not support attached text) this happenned with all shapes strType but work only with textbox & inlineShapes also i tried to convert shapes to inlineShapes cause it give me better result but not success i used .convertToInlineShape in general or even tried with msoPicture both failed but i able to do it throw my program not macro, so at least if possible need solution to this 5917 errowr in line if len(trim ... thanks |
#11
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim i As Long, Rng As Range, StrType With ActiveDocument For i = .Shapes.Count To 1 Step -1 With .Shapes(i) If Not .TextFrame Is Nothing Then On Error GoTo SkipShp If Len(Trim(.TextFrame.TextRange.Text)) > 1 Then Select Case .Type Case msoAutoShape: StrType = "AutoShape" Case msoCallout: StrType = "Callout" Case msoCanvas: StrType = "Canvas" Case msoChart: StrType = "Chart" Case msoComment: StrType = "Comment" Case msoDiagram: StrType = "Diagram" Case msoEmbeddedOLEObject: StrType = "EmbeddedOLEObject" Case msoFormControl: StrType = "FormControl" Case msoFreeform: StrType = "Freeform" Case msoGroup: StrType = "Group" Case msoInk: StrType = "Ink" Case msoInkComment: StrType = "InkComment" Case msoLine: StrType = "Line" Case msoLinkedOLEObject: StrType = "LinkedOLEObject" Case msoLinkedPicture: StrType = "LinkedPicture" Case msoMedia: StrType = "Media" Case msoOLEControlObject: StrType = "OLEControlObject" Case msoPicture: StrType = "Picture" Case msoPlaceholder: StrType = "Placeholder" Case msoScriptAnchor: StrType = "ScriptAnchor" Case msoShapeTypeMixed: StrType = "ShapeTypeMixed" Case msoTable: StrType = "Table" Case msoTextBox: StrType = "TextBox" Case msoTextEffect: StrType = "TextEffect" End Select Set Rng = .Anchor With Rng .InsertBefore StrType & " start << " .Collapse wdCollapseEnd .InsertAfter " >> end " & StrType .Collapse wdCollapseStart End With Rng.FormattedText = .TextFrame.TextRange.FormattedText .Delete End If SkipShp: On Error GoTo 0 End If End With Next For i = .InlineShapes.Count To 1 Step -1 With .InlineShapes(i) If Not .TextEffect Is Nothing Then On Error GoTo SkipiShp If Len(Trim(.TextEffect.Text)) > 1 Then Select Case .Type Case wdInlineShapeChart: StrType = "InlineChart" Case wdInlineShapeDiagram: StrType = "InlineDiagram" Case wdInlineShapeEmbeddedOLEObject: StrType = "InlineEmbeddedOLEObject" Case wdInlineShapeHorizontalLine: StrType = "InlineHorizontalLine" Case wdInlineShapeLinkedOLEObject: StrType = "InlineLinkedOLEObject" Case wdInlineShapeLinkedPicture: StrType = "InlineLinkedPicture" Case wdInlineShapeLinkedPictureHorizontalLine: StrType = "InlineShapeLinkedPictureHorizontalLine" Case wdInlineShapeLockedCanvas: StrType = "InlineLockedCanvas" Case wdInlineShapeOLEControlObject: StrType = "InlineOLEControlObject" Case wdInlineShapeOWSAnchor: StrType = "InlineOWSAnchor" Case wdInlineShapePicture: StrType = "InlinePicture" Case wdInlineShapePictureBullet: StrType = "InlinePictureBullet" Case wdInlineShapePictureHorizontalLine: StrType = "InlinePictureHorizontalLine" Case msoLinkedOLEObject: StrType = "LinkedOLEObject" Case wdInlineShapeScriptAnchor: StrType = "InlineScriptAnchor" End Select Set Rng = .Range With Rng .Collapse wdCollapseStart .InsertBefore StrType & " start << " .Collapse wdCollapseEnd .InsertAfter " >> end " & StrType .Collapse wdCollapseStart End With Rng.Text = .TextEffect.Text .Delete End If SkipiShp: On Error GoTo 0 End If End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
evening macropod
unfortionatly the errowr 5917 still the same before i write to you i tried some solution to solve this problem this line do the trick if .textframe.has text then if len(trim ... but the problem now with it it process only shapes which support text and leave others like (lines, groups, freeforms .. etc) in a document next part of macro which process inlineShapes works without errowrs even without use your last modification. fore sure practice let me learn more, thanks for support. |
#13
|
||||
|
||||
I can't see how that can be so when you say:
What shapes are still causing 5917 errors? And, if a shape or inlineshape has no text, what do you expect to be done with it?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
What are the minimum margins most printers can handle? | 20GT | Word | 3 | 11-08-2014 10:40 PM |
Display result in textbox based on the input of another textbox | scarymovie | Word VBA | 5 | 05-16-2012 07:05 PM |
Shapes don't have green handle | rdy4trvl | Drawing and Graphics | 4 | 05-15-2012 09:53 PM |
How does Powerpoint handle resolution of images? | noni | PowerPoint | 2 | 08-09-2010 03:32 PM |
How to handle resources that have Bid on project | rrmccabe | Project | 0 | 03-21-2010 12:39 PM |