I've been working on this code... But I need input!
You know how you work on a code so long you're brain fries? Well that's where I am. I feel this should be easy but leave it to me to make thing complicated.
Okay - I have existing text on page. I select text on page, type new text in textbox and click "add to page" Which should add to page OR if text is selected replace text with new text.
ISSUE : Code replaces anything I have selected. If I have a rectangle selected it will be replaced with new text. I only need the code to do 2 things. Add to page if nothing is selected OR replace live text if live text is selected.
Set sr = ActiveSelectionRange sr.GetPosition X, Y Optimization = True Set textShape = ActiveLayer.CreateArtisticText(ActiveLayer.Page.CenterX, 0, TextBox.Text, 0, cdrCharSetMixed, , 40) On Error Resume Next textShape.Text.Story.font = ListAllFonts textShape.Text.Story.Size = sr(1).Text.Story.Size textShape.Fill.UniformColor = sr(1).Fill.UniformColor textShape.Text.Story.CharSpacing = 15 textShape.Text.Story.WordSpacing = 100 textShape.OrderToFront
If sr.Count = 1 Then ' here might be my issue? I don't know... textShape.SetPosition X, Y Else textShape.AlignAndDistribute 3, 3, 2, 0, False, 2 End If sr(1).Delete
Maybe code below can help
Sub ReplaceText()Dim txtFIND As String Dim txtREPLACE As String 'The new word to replace the oldDim p As PageIf ActiveShape.Type <> cdrTextShape Then ActiveDocument.AddPages (1): Exit SubtxtFIND = ActiveShape.Text.SelectiontxtREPLACE = InputBox("New text", "Enter text", "")For Each p In ActiveDocument.Pagesp.TextReplace txtFIND, txtREPLACE, True, FalseNext pEnd Sub
Chieri,
I am simplified this down, you may add to it. I would check the ActiveSelectionRange and make sure that only one shape is selected, if not then display a message to the user and exit the code.
If only one shape is selected I would see if it is of type cdrTextShape. If it is then I would simply replace the text with the text in your TextBox.
If the shape is not text I would create a new line of artistic text at the location of the selected shape. Then I would delete the selected shape.
Here is what that looks like:
Sub ReplaceOrCreateText() Dim sr As ShapeRange Dim sText As Shape Set sr = ActiveSelectionRange If sr.Count > 1 Or sr.Count < 1 Then MsgBox "Please select only a single shape.", , "Replace or Create Text" If sr(1).Type = cdrTextShape Then sr(1).Text.Story = TextBox.Text Else Set sText = ActiveLayer.CreateArtisticText(sr(1).PositionX, sr(1).PositionY - sr(1).SizeHeight, TextBox.Text, 0, cdrCharSetMixed, , 40) sr.Delete End If End Sub
Hope that helps,
-Shelby