#1
|
|||
|
|||
Generate a string only if textbox has been changed
So I am trying to build a vba code to only generate a string of data concactenated from various textboxes if the initial box has been edited.
For context I'm trying to list schedules for an unknown # of people. I have a User form i'm piggybacking off of for fields that include: Person name, days attending, and hours. I have these boxes for up to 10 people currently and my end goal is to generate a line in my word document at a specific bookmark only if Person Name field textbox on the userform has been changed. My fields on the userform read as: PNBox DABox HABox 1PNBox 1DABox 1HABox ... all the way to 10 assuming i fill in the info for the first two people the following two lines would generate John Doe, Mon- Fri, 8:00am - 5:00pm Jane Doe, Mon- Thu, 8:00am - 5:00pm however, here's where i'm having the issue... I want to use the Bookmarks.Add method and only want it to generate the bookmarks for days and hours IF the name field has been changed. I hope i've explained that well enough. Cause otherwise I have a section in my document that is just , , , , where I have the bookmarks built in around the commas I'm a total noob at VBA but i've managed to complete all other things i'm trying to do, it's this bit of code I can't seem to pull together. This is all i've got, please don't laugh :P Code:
Dim Temp As Range If ActiveDocument.Bookmarks.Exists("bmPN") Then If Me.PNBox.Value <> “Persons Name” Then ActiveDocument.Bookmarks("bmPN").Range.InsertAfter _ ", " ActiveDocument.Bookmarks.Add Name:="bmDA ActiveDocument.Bookmarks("bmDA").Range.InsertAfter _ ", " ActiveDocument.Bookmarks.Add Name:="bmHA Set Temp = ActiveDocument.Bookmarks("bmPN").Range Temp.Text = Me.PNBox.Value End If End If ...Help |
#2
|
||||
|
||||
I find working with bookmarks as anchors to be quite annoying especially when working with those bookmarks to populate text in the document from the userform. I prefer to use content controls and populate them either indirectly from linked XML or directly in code.
Staying with bookmarks, if you want to add one, you need to provide a range for it to go and close the quotes on its name. For instance, this line won't work ActiveDocument.Bookmarks.Add Name:="bmDA but this should ActiveDocument.Bookmarks.Add Name:="bmDA", Range:=ActiveDocument.Paragraphs(1).Range The act of setting the text value content of a bookmark actually removes the bookmark [see Sentence #1 above] so these lines of code put text in the document but also remove the bookmark Set Temp = ActiveDocument.Bookmarks("bmDA").Range Temp.Text = Me.PNBox.Value
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Possible Solution?
So i've been going over this problem now for a day and some and I think i've been going about this the wrong way.
Would it be easier to do something like: Code:
If Me.PNBox.Value <> "Person Name" Then ActiveDocument.Range.InsertParagraphAfter ActiveDocument.Range.InsertAfter Me.PNBox.Value & ", " & Me.DABox.Value & ", " & Me.TABox.Value End If Last edited by Javi78503; 08-01-2021 at 11:22 PM. |
#4
|
||||
|
||||
That looks a lot simpler. If you are always adding the values as a new paragraph at the end of the document it could be even shorter
Code:
If Me.PNBox <> "Person Name" Then ActiveDocument.Range.InsertAfter vbCr & Me.PNBox & ", " & Me.DABox & ", " & Me.TABox End If
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Quote:
Thanks! even less code I have to type up |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Wildcard replace any string in context with a specified string | wardw | Word | 7 | 05-07-2018 09:13 AM |
How can I compare a string in a cell to another string? | Amitti | Word VBA | 2 | 04-10-2017 07:35 PM |
How to find all string within string. | PRA007 | Word VBA | 18 | 02-12-2016 08:11 PM |
Way to search for a string in text file, pull out everything until another string? | omahadivision | Excel Programming | 12 | 11-23-2013 12:10 PM |
Display result in textbox based on the input of another textbox | scarymovie | Word VBA | 5 | 05-16-2012 07:05 PM |