MS Publisher 365 and VBA...

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I really need some help with this one. I have tried coding in Excel and moving the code over to Publisher without any effective results.

I have a document that prints four (4) labels for a food product that my wife makes. Each label contains several textboxes, each containing different information. The last text box contains, "Best if eaten by: <current date + 80 days>" If this were in Excel, it would be easy to make the change using VBA, but in Publisher I must select the textbox, but I cannot find anything that shows me the textbox name (e.g., Textbox 440 or other number). So I've tried selecting the textboxes one at a time to change the name, but the code doesn't work.

I've tried:
VBA Code:
Sub Date_Change()

Selection.ShapeRange.TextFrame.Parent
Shape.Name = "Textbox 1"

End Sub

Can anyone help me with code that will rename the four textboxes (not all of them)? Or better yet, old versions of Publisher gave you the name of the shape you are working in in one corner of the program, but this feature has been deleted in this and recent versions.

Any help would be appreciated!

Thank you.

Charles
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
OK, I figured this one out. It took me months, but I ended up deciding to go a different route and search for existing text within the document, then select the text, delete it without deleting the textbox, and then adding the text and date that I want to appear. In case anyone is interested, here is the code:

VBA Code:
Sub UpdateTextboxes()
     Dim pubPage As Page
     Dim pubShape As Shape
     Dim searchText As String
     Dim newText As String
     Dim currentDate As Date

' Define the text to search for
     searchText = "Best if eaten by:"
'
 Calculate the new date (current date + 80 days)
     currentDate = Date
     newText = "Best if eaten by: " & Format(currentDate + 80, "mm/dd/yyyy")

' Loop through all pages in the document
     For Each pubPage In ActiveDocument.Pages

' Loop through all shapes on the page
     For Each pubShape In pubPage.Shapes

' Check if the shape contains text
     If pubShape.HasTextFrame Then

' Check if the text frame contains the search text
          If InStr(1, pubShape.TextFrame.TextRange.Text, searchText, vbTextCompare) > 0 Then

' Replace the existing text with the new text
          pubShape.TextFrame.TextRange.Text = _
Replace(pubShape.TextFrame.TextRange.Text, searchText, newText, , , vbTextCompare)

          End If
     End If
     Next pubShape
Next pubPage

End Sub

Regards,
Lidsavr
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top