I'm trying to create a macro that will pop up a input box where you can enter the x coord and create a vertical guideline at that location. So far I can get the input to pop up, but after that I get no errors but the guideline is not created. Here's the code I'm currently using. Any suggestions would be greatly appreciated.
Sub CreateGuidelineAtXCoord() Dim xCoord As Double Dim activePage As Page
' Get the active page Set activePage = ActiveDocument.activePage
' Prompt the user for the X-coordinate xCoord = InputBox("Enter X-coordinate for the guideline:", "Create Guideline")
' Check if the user entered a valid number If IsNumeric(xCoord) Then ' Create a vertical guideline at the specified X-coordinate activePage.CreateGuideline cdrVerticalGuideline, xCoord Else ' Display a message if the user entered an invalid X-coordinate MsgBox "Invalid X-coordinate entered. Please enter a numeric value.", vbExclamation, "Macro Error" End IfEnd Sub
On theme with this macro, I've made a move object to x-coords which works very well. However for the macro I made to move an object to y-coord half works. Just like the move object to x-coords macro, it centres the object to the x value. But this code will not, it is always out by the height of the object, eg, object dim is 100 x 100mm. I send it to 300 and it will land centred at 400mm. Any suggestions would be very helpful, Here is the code for move object to y coord. If you want the move object to x-coord code just let me know if it helps.
Sub MoveObjectToYCoordCentered() Dim targetYCoordInMillimeters As Double Dim targetYCoordInPoints As Double Dim selectedShape As Shape
' Check if any shape is selected If ActiveSelection.Shapes.Count > 0 Then ' Get the selected shape Set selectedShape = ActiveSelection.Shapes(1)
' Ask the user for the target Y-coordinate in millimeters targetYCoordInMillimeters = InputBox("Enter the target Y-coordinate (in millimeters):", "Move Object")
' Check if the user provided a valid numeric value If IsNumeric(targetYCoordInMillimeters) Then ' Convert millimeters to CorelDRAW internal units (points) targetYCoordInPoints = targetYCoordInMillimeters / (ActiveDocument.Unit * 25.4)
' Calculate the new Y-coordinate to center the object Dim currentYCoord As Double currentYCoord = selectedShape.PositionY + selectedShape.SizeHeight / 2 Dim newYCoord As Double newYCoord = targetYCoordInPoints - selectedShape.SizeHeight / 2
' Move the selected shape to the new Y-coordinate while keeping its center aligned selectedShape.PositionY = newYCoord Else MsgBox "Invalid input. Please enter a numeric value.", vbExclamation, "Error" End If Else MsgBox "No object selected. Please select an object and run the macro again.", vbExclamation, "Error" End IfEnd Sub
Considering that you are on the Corel 2022 forum, you have a Shape object property available in the API that returns centerX and one for CenterY, while the positionX and positionY properties are relative to the document's reference point
I used centerY which works very, Thanks. Here is the code:
' Calculate the new Y-coordinate to center the object Dim centerY As Double centerY = selectedShape.CenterY Dim newYCoord As Double newYCoord = targetYCoordInPoints - selectedShape.SizeHeight / 2
' Move the selected shape to the new Y-coordinate while keeping its center aligned selectedShape.CenterY = newYCoord + selectedShape.SizeHeight / 2 Else MsgBox "Invalid input. Please enter a numeric value.", vbExclamation, "Error" End If Else MsgBox "No object selected. Please select an object and run the macro again.", vbExclamation, "Error" End IfEnd Sub