Hello,
While I've come to a better understanding of manipulating curve shapes, text is still a big hurdle for me. I try pretty hard to figure this stuff out on my own but seriously 50+ debug attempts on what should be a very small and simple (stage within a larger) macro really breaks me down.
What I'm trying to do is select a collection of artistic text fields, add a comma to the end of each one, bring them all together into one field and reverse their order. I will briefly explain a bit better. The comma is enough, no space adding required. The "combine" function of DRAW works fine for bringing them all together into one field, but it puts them in the reverse order of what I need, which makes sense because C1 is created first, then C2 and so on - resulting in C10C9C8 etc. I thought I was on the right track in the Object Browser when I found the InsertAfter "class member", but I got nowhere with the VBA example provided in the (F1) CorelDRAW X7 Macros Help resource. The example at least showed me how to insert specified text at the end of a paragraph text, even though I use artistic text. That much isn't a big deal of course, but the real trouble is I do not understand stuff like "Set s = d..." followed by Set t =s..." and how they interact in this example. This is the VBA help example:
The following VBA example inserts the sentence This will be inserted last. after the paragraph text This is an example.
In my attempts to use this example, one of many troubles was that of course I don't need to create a new document as I'm already working in one, so I managed to do away with that without ruining the Set s = d part, though I've probably forgotten how by now. Then there's the problem that I am dealing with a selection range of Text, so that introduces the "t" aspect. When I try to do stuff like "for each s in sr" or "for each t in sr" (if that is even possible), I can't get it to work.
Here is an image of what I'm trying to achieve at this stage within a larger macro:
All help is appreciated!
Sub Macro4trey86
Dim s As Shape, sr As ShapeRange, ResultStr As String
Set sr = ActiveSelectionRange: If sr.Count = 0 Then Exit Sub 'working with selected text shapes
Set sr = sr.ReverseRange ' inverse order. This string you may remove
ResultStr = ""
For Each s In sr
If ResultStr <> "" Then ResultStr = ResultStr & "," 'adding comma
ResultStr = ResultStr & s.Text.Story.Text ' adding text. If you selected non-text shape then get error
Next s
Set s = Activelayer.CreateArtisicText(1,1, ResultStr) 'creating new shape with result
End Sub
Hey, Shark!
Thanks for looking into this with me. This will help me understand strings better, as that method is new to me. However, I may be doing something wrong, or I may have missed something in my explanation. I'm getting an error on that last line which prevents anything from playing in the macro. Screenshot:
If I understood strings better I may be able to spot the issue, but at this time I do not.
by the way, with the help of CorelDRAW VBA, you can directly access Excel cells, write any information there and then save the .xls file. Excel himself will not even be shown on the screen.
That sounds interesting, shark_. Do you have any further information or resources on how to go about doing that? It relates to another thing I do manually every day. Maybe you have a suggestion for this one too? Here is an image explaining the process. This is a small example of just six text objects. Its usually up between 15-30 and sometimes around half of them have this same issue of not combining into one line due to the space between multiple words, as the image explains for "SEA GLASS". Thus far, all I can figure is to manually get those into one line by removing the Enter/Return keystroke which put them down on that second line. Any ideas?
Sub Text_text()Dim sr As ShapeRange, s As Shape, A$ Set sr = ActiveSelectionRange.Shapes.FindShapes(, cdrTextShape) 'find text only For Each s In sr A = s.Text.Story.Text: A = Replace$(A, vbCr, " ") ' replace EOL to Space s.Text.Story.Text = A NextEnd Sub
trey86 said: Do you have any further information or resources on how to go about doing that?
I wrote a macro for myself that directly writes from CorelDRAW the data I need into the Excel table, for example, the size (width x height) and name of the files I printed. I can write the same macro for you
Shark_!
Sub Text_text works great. I tried take it a step further to go on to perform the same action with a different kind of text in this type of file I work on but I can't get it. I tried to use your code twice in one macro setting by first finding the black text in this example, copy,paste,move, getting them all one one line each via Space in how your macro works, then combining them to be one text field. Works great. But then I have no idea what I'm doing so I tried to "imitate" that for the other kind of text (the white numbers) and I'm sure you'll know why that wouldn't work, but I don't know enough to understand it. On top of that, somehow the Set sr = ActiveSelectionRange seems to tie that range to a specific instance or point in time, because once I tried to bring these individual sets of actions together (rather than just playing your Text_text on them separately) I get the error message "The referenced object no longer exists", which is why I say it seems it gets tied to a earlier point. So where I currently land at with the error is shown in the image:
I'm embarrassed to paste my dumb tinkering with the code trying to get around the error I get, but I thought maybe I needed to use B$ instead of A$, or sr2 instead of sr, if any of this could have been part of the reason for the error or "tying" to past instances - no luck. On top of that, I don't think the Space entry is the solution for this type of combining, but maybe you know? Even if I manually add a space to the end of each number, it still puts them all on one line together, separated by spaces, but needs to be the same result like the words you already helped me with - vertical alignment upon combining.
If you don't mind, I'm pasting it so you might tell me how I am thinking about this wrong and how to think about it correctly! Really appreciate all your help, it is invaluable.
Code segment below: (its becoming part of a larger macro but I'll just paste this part that is causing the problem, currently)
------------------------------------------------------------------------------------------------------------------------------------
Dim s As Shape, sr As ShapeRange, A$, sr2 As ShapeRange, B$, s2 As Shape ActivePage.Shapes.FindShapes(, , True, "@fill.color = cmyk(100,100,100,100)").AddToSelection ActiveSelection.Copy ActiveLayer.Paste ActiveSelection.Move 0, 50 Set sr = ActiveSelectionRange.Shapes.FindShapes(, cdrTextShape)For Each s In sr A = s.Text.Story.Text: A = Replace$(A, vbCr, " ") ' replace EOL to Space s.Text.Story.Text = A Next ActiveSelectionRange.Combine ActiveWindow.ActiveView.ToFitAllObjects ActiveDocument.ClearSelection ActivePage.Shapes.FindShapes(, , True, "@fill.color = cmyk(0,0,0,0)").CreateSelection ActiveSelection.Copy ActiveLayer.Paste ActiveSelection.Move 40, 50 Set sr2 = ActiveSelection.Shapes.FindShapes(, cdrTextShape)For Each s2 In sr B = s2.Text.Story.Text: B = Replace$(B, vbCr, " ") ' replace EOL to Space s2.Text.Story.Text = B Next ActiveSelectionRange.Combine
As for your offer to help me with getting information directly from CorelDRAW to Excel, that is very generous but I fear it may be much too annoying/complicated to get it from how you use it to how I would probably use it differently. If it works fairly universally though I wouldn't mind looking at what you've already got, since you are nice enough to offer! Either way, I guess I'd first have to get these text items I've been showing you to line up correctly, for it to even be beneficial to have VBA take them from DRAW to Excel no? Can't have them just pasting wild all over the place, hah.
if I correctly understood the task, you need place white text next to the black text, as a table
then try this code:
Sub ProcessBlack_n_White()Dim srB As ShapeRange, srW As ShapeRange, A$, z&, x#, y# Set srB = ActiveLayer.Shapes.FindShapes(, cdrTextShape, , "@fill.color = cmyk(0,0,0,100)") 'find black shapes Set srW = ActiveLayer.Shapes.FindShapes(, cdrTextShape, , "@fill.color = cmyk(0,0,0,0)") 'find white shapes ActiveDocument.Unit = cdrMillimeter x = 0: y = 0 ' start position For z = 1 To srB.Count srB(z).Text.Replace vbCr, " ", False srB(z).SetPosition x, y If z <= srW.Count Then srW(z).SetPosition x + 90, y 'white text put to the right y = y + 10 ' new line NextEnd Sub
notice - black color is represented as CMYK 0,0,0,100, not 100,100,100,100
Hey, now this is looking good!
I made just two little adjustments. I work with very large scale text and shapes so 90 millimeters was too small so that was easy. The black I use for that text actually is set to full CMYK, which is fine because none of that gets printed. Those are just labels and names for fabric as reference for the people cutting the shapes which the A1, A2, A3, etc. from the first part of this thread are assigned to. I didn't include any shapes in the screenshots since they aren't relevant to the text manipulation you've helped so much with! That said, I have the result I needed, thanks to you. Now I can very quickly get the aligned lists for copying and default (no formats/colors/outlines/etc.) pasting into Excel, and it is very nice. So now with one button I go from the image above with the numbered thumbnails and SKU# + fabric names to the right of each square, to just this:
So easy now! BTW, where did you learn to manipulate text like this so well in DRAW through VBA?? It is a bit more complicated than I thought it would be. As I said in the very beginning, I understand shapes/curve manipulation pretty OK now (not great) but wow text seems very different when I try to understand your coding. Its really awesome though, and many thanks.
Hi
trey86 said:BTW, where did you learn to manipulate text like this so well in DRAW through VBA??
CorelDRAW has quite good built-in help with examples, you can see the description of each function and the hierarchy of objects (: