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
Here is the final code.
' Get the active page Set activePage = ActiveDocument.ActivePage
' Prompt the user for the X-coordinate in millimeters xCoord = InputBox("Enter X-coordinate for the guideline in millimeters:", "Create Guideline")
' Check if the user entered a valid number If IsNumeric(xCoord) Then ' Convert millimeters to inches (1 inch = 25.4 mm) xCoord = xCoord / 25.4
' Create a vertical guideline at the specified X-coordinate activePage.GuidesLayer.CreateGuide xCoord, 1, xCoord, -1 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
Thanks for your help.
Thank you for this very useful code. Can you make for horizontal option? Thx
This will work for the horizontal guidelines. It will run as a separate macro though.
Sub CreateGuidelineAtYCoord() Dim yCoord As Double Dim activePage As Page
' Prompt the user for the Y-coordinate in millimeters yCoord = InputBox("Enter Y-coordinate for the guideline in millimeters:", "Create Guideline")
' Check if the user entered a valid number If IsNumeric(yCoord) Then ' Convert millimeters to inches (1 inch = 25.4 mm) yCoord = yCoord / 25.4
' Create a horizontal guideline at the specified Y-coordinate activePage.GuidesLayer.CreateGuide 1, yCoord, -1, yCoord Else ' Display a message if the user entered an invalid Y-coordinate MsgBox "Invalid Y-coordinate entered. Please enter a numeric value.", vbExclamation, "Macro Error" End IfEnd Sub
Thank you, thank you so much FIM.
if you will predefine that the input will always be in millimeters
xCoord = ConvertUnits(xCoord, cdrUnit.cdrMillimeter, ActiveDocument.Unit)