How to activate a shape by name and change text, font ... ?

I recorded some macros to automate my workflow to laser some wood with names. Now i try to control Corel Draw X7 by c#. Amazing ...
I found some code in the internet and a lot of very efficient code snippets from shelby, by not one snippet with my (beginnsers) problem.

There is a draw with two shapes, one with some lines, one (named "Ebene 1") with the text to change.
The code
doc.ActivePage.TextReplace ("John Doe", "blubb", false, true);
is working, if the shape "John Doe" is selected. But not, if the draw is opened without a selection.
My wish is to change every text on shape "Ebene 1" to the text "blubb" and set a textfont and size and, for luxury, to get a response via com about the result.

Is someone there to take my hand?
regards and a big "thank you" for all the support you give also at the other forums!
Ralf

  • As you don’t show full code I can assume that it could look like following
    (source here http://community.coreldraw.com/sdk/f/42/p/53130/254341#254341)

    code check if selected object is text shape and if yes then ask for new text that will replace selected text an all pages of active document

    Sub ReplaceText()
    Dim txtFIND As String
    Dim txtREPLACE As String 'The new word to replace the old
    Dim p As Page
    If ActiveShape.Type <> cdrTextShape Then ActiveDocument.AddPages (1): Exit Sub

    txtFIND = ActiveShape.Text.Selection
    txtREPLACE = InputBox("New text", "Enter text", "")
    For Each p In ActiveDocument.Pages
    p.TextReplace txtFIND, txtREPLACE, True, False
    Next p
    End Sub

    you can change code as shown below - then on first step you will enter text to find and on next step text that will replace text to find

    Sub ReplaceText()
    Dim txtFIND As String
    Dim txtREPLACE As String
    txtFIND = InputBox("Text to replace", "Enter text", "")
    Dim p As Page
    txtREPLACE = InputBox("New text", "Enter text", "")
    For Each p In ActiveDocument.Pages
    p.TextReplace txtFIND, txtREPLACE, True, False
    Next p
    End Sub

    • Tanks for your Support, Mek!

      in c# the code

      doc.ActiveShape.Type <> cdrTextShape is not possible. But why?

      Here is the complete function:

             private void CorelBtnClicked()
              {
                  Type pia_type = Type.GetTypeFromProgID("CorelDRAW.Application.17");
                  Corel.Interop.VGCore.Application app = Activator.CreateInstance(pia_type) as Corel.Interop.VGCore.Application;
                  app.Visible = true;
                  Document doc = app.ActiveDocument;
                  if (doc == null)
                      doc = app.CreateDocument();

       

      doc.ActivePage.TextReplace("John Doe", "blubb", false, true);

      }

      I don't find a way to select a unselected shape. If i select the shape manually, it is working.

      My workaround for the complete function is to start a lot of macros like this way

      app.GMSManager.RunMacro("GlobalMacros", "RecordedMacros.EineZeileRalf");
      System.Threading.Thread.Sleep(500);

      app.GMSManager.RunMacro("GlobalMacros", "RecordedMacros.kopieverschieben");
       System.Threading.Thread.Sleep(500);

      • try to look at

        How do I set the Text in a Shape with C#

        CorelDRAW Community
        I am attempting to edit the text in a shape using C#.In VBA you can set the shape.Text or the shape.Text.Story. However, in C# shape.Text is a get; only property so there is no way to set it. I tried to using the Shape PlaceTextInside(Shape TextShape...

        https://msdn.microsoft.com/cs-cz/library/fk49wtc1(v=vs.110).aspx

        • I got it! (It is not my code -> many thanks to Markus!!!!)

          Layer lr = app.ActiveDocument.Pages[1].Layers.Find("Ebene 1");
          Shape txt = lr.FindShape("text");
          txt.Text.Contents = "John Doe";

          lr.Shapes.All().CreateSelection();

          // export as HPGL must be a corel vba macro because of the HPGL parameters
          app.GMSManager.RunMacro("GlobalMacros", "RecordedMacros.MyMacro_ExportToHPGL")

          // dokument close
          app.ActiveDocument.Dirty = false;
          app.ActiveDocument.Close();