Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2021, 09:05 PM
Javi78503 Javi78503 is offline Generate a string only if textbox has been changed Windows 10 Generate a string only if textbox has been changed Office 2019
Novice
Generate a string only if textbox has been changed
 
Join Date: Jul 2021
Posts: 12
Javi78503 is on a distinguished road
Default 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
after that I have the code to change the other bookmarks if they exist, and i've got that part down

...Help
Reply With Quote
  #2  
Old 08-01-2021, 06:43 PM
Guessed's Avatar
Guessed Guessed is offline Generate a string only if textbox has been changed Windows 10 Generate a string only if textbox has been changed Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 08-01-2021, 08:37 PM
Javi78503 Javi78503 is offline Generate a string only if textbox has been changed Windows 10 Generate a string only if textbox has been changed Office 2019
Novice
Generate a string only if textbox has been changed
 
Join Date: Jul 2021
Posts: 12
Javi78503 is on a distinguished road
Default 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
Would that insert those fields at the bottom of the exitsting page? and if so I could just do that for each field I want to add

Last edited by Javi78503; 08-01-2021 at 11:22 PM.
Reply With Quote
  #4  
Old 08-01-2021, 09:32 PM
Guessed's Avatar
Guessed Guessed is offline Generate a string only if textbox has been changed Windows 10 Generate a string only if textbox has been changed Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 08-01-2021, 11:23 PM
Javi78503 Javi78503 is offline Generate a string only if textbox has been changed Windows 10 Generate a string only if textbox has been changed Office 2019
Novice
Generate a string only if textbox has been changed
 
Join Date: Jul 2021
Posts: 12
Javi78503 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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

Thanks! even less code I have to type up
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate a string only if textbox has been changed Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
Generate a string only if textbox has been changed 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
Generate a string only if textbox has been changed 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
Generate a string only if textbox has been changed Display result in textbox based on the input of another textbox scarymovie Word VBA 5 05-16-2012 07:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:59 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft