Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-09-2024, 10:52 AM
mordyas mordyas is offline Automatic form update between paragraphs Windows 10 Automatic form update between paragraphs Office 2016
Novice
Automatic form update between paragraphs
 
Join Date: May 2023
Posts: 5
mordyas is on a distinguished road
Default Automatic form update between paragraphs

(Translated by Google.)
I built a form called userform1.
In the form, a text box called SPB.
When I run the form, I want to receive in the text box the data of the spacing before the current paragraph.
Just kidding.


The matter gets complicated, when I request that every time the focus moves to a new paragraph, the size in the text box be updated automatically.
After 5 days with Ghat GPT, I realized that I would not get any substantive help from it...
Who is willing to help?
(Office 365.)
Reply With Quote
  #2  
Old 12-09-2024, 01:04 PM
Italophile Italophile is offline Automatic form update between paragraphs Windows 11 Automatic form update between paragraphs Office 2021
Expert
 
Join Date: Mar 2022
Posts: 554
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

That information is already available on the Layout tab. Why re-invent the wheel?
Screenshot 2024-12-09 195856.png
Reply With Quote
  #3  
Old 12-09-2024, 03:34 PM
mordyas mordyas is offline Automatic form update between paragraphs Windows 10 Automatic form update between paragraphs Office 2016
Novice
Automatic form update between paragraphs
 
Join Date: May 2023
Posts: 5
mordyas is on a distinguished road
Default

Thanks for your answer, you're right, there's no need to reinvent the wheel...
I'm building a complex form, for unique editing of a document, with some macros, and appropriate code. It's going pretty well. But I'm "stuck" with the "simple" task I described, how to keep the information in the form relevant.
Reply With Quote
  #4  
Old 12-09-2024, 06:02 PM
macropod's Avatar
macropod macropod is offline Automatic form update between paragraphs Windows 10 Automatic form update between paragraphs Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You will at least have to provide the code you're using to determine the current paragraph.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-10-2024, 02:45 PM
mordyas mordyas is offline Automatic form update between paragraphs Windows 10 Automatic form update between paragraphs Office 2016
Novice
Automatic form update between paragraphs
 
Join Date: May 2023
Posts: 5
mordyas is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
You will at least have to provide the code you're using to determine the current paragraph.
Fair enough...
Here's the code.
Form named: UserForm3
Text box named: SPB_box
Update button named: SPB_updt

=*=*=*=*=*=*=*=*=*=*=
This is the code associated with the document itself (or all documents)
=*=*=*=*=*=*=*=*=*=*=
Private Sub Document_SelectionChange(ByVal Sel As Selection)
' If the form is open, update the text box
If UserForm3.Visible Then
MsgBox "Selection changed - updating spacing"
UserForm3.UpdateSpacingBefore
End If
End Sub

Sub ShowUserForm()
' Open the form modelessly (you will be able to work with the document)
UserForm3.Show vbModeless
' Run the update even if you haven't changed any paragraphs
UserForm3.UpdateSpacingBefore
End Sub

=*=*=*=*=*=*=*=*=*=
This is the code associated with the form
=*=*=*=*=*=*=*=*=*=*=
Private Sub UserForm_Initialize()
' Update the text box when the form is opened
UpdateSpacingBefore
End Sub

Public Sub UpdateSpacingBefore()
Dim spaceBefore As Single
On Error Resume Next ' If the paragraph cannot be accessed
spaceBefore = Selection.ParagraphFormat.spaceBefore ' Get the "space before" value
On Error GoTo 0 ' Return to normal error mode

' Update the text box with the value of "space before"
SPB_box.value = spaceBefore
End Sub

Private Sub SPB_updt_Click()
' Manually update the space before
UpdateSpacingBefore
End Sub

(Disclosure: I used AI to create this code, and it works for me)
I'm looking to improve this code, so that when I move focus From paragraph to paragraph - the information in the text box will be updated automatically.
Reply With Quote
  #6  
Old 12-11-2024, 10:33 AM
gmaxey gmaxey is offline Automatic form update between paragraphs Windows 10 Automatic form update between paragraphs Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Mordyas,


1.There is no such thing as a Document_SelectionChange (at least not that I'm aware of).


2. Create a new class module. Call it clsApp. In it put this code:


Code:
Option Explicit
Private WithEvents mWordApp As Word.Application
Private Sub Class_Initialize()
  Set mWordApp = Word.Application
End Sub
Private Sub mWordApp_WindowSelectionChange(ByVal oSel As Selection)
  If UserForm3.Visible Then UserForm3.UpdateSpacingBefore
End Sub

3. Create a new standard module. Call it modMain. In it, put this code:


Code:
Option Explicit
Private oCls As clsApp
Sub ShowUserForm()
  If oCls Is Nothing Then Set oCls = New clsApp
  UserForm3.Show vbModeless
 End Sub

4. In your Userform3, you can use:

Code:
Option Explicit
Private Sub UserForm_Initialize()
  UpdateSpacingBefore
End Sub
Public Sub UpdateSpacingBefore()
Dim spaceBefore As Single
  On Error Resume Next
  spaceBefore = Selection.ParagraphFormat.spaceBefore
  On Error GoTo 0
  SPB_box.Value = spaceBefore
End Sub
Private Sub SPB_updt_Click()
  UpdateSpacingBefore
End Sub

5. Turn off ChatGPT. Just my opinion.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 12-11-2024, 01:19 PM
macropod's Avatar
macropod macropod is offline Automatic form update between paragraphs Windows 10 Automatic form update between paragraphs Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,465
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by gmaxey View Post
1.There is no such thing as a Document_SelectionChange
Agreed. Probably nonsense code generated by Chat GPT.

For Event-driven procedures in Word, see: https://wordmvp.com/FAQs/MacrosVBA/AppClassEvents.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 12-12-2024, 06:37 AM
mordyas mordyas is offline Automatic form update between paragraphs Windows 10 Automatic form update between paragraphs Office 2016
Novice
Automatic form update between paragraphs
 
Join Date: May 2023
Posts: 5
mordyas is on a distinguished road
Default

It didn't work for me.
Thanks anyway.
I'll try to move on from here, and look at nearby options.
Reply With Quote
Reply

Tags
auto update, paragraphs, space before



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Update for Excel Spreadsheet Frenchy305 Excel 3 07-14-2017 12:17 PM
Automatic form update between paragraphs Stop Automatic Date-Update janetb Word 10 11-10-2016 12:05 AM
Automatic form update between paragraphs automatic insert hypens until the end of the row and in empty rows between paragraphs Ivica Word 3 12-28-2015 01:31 PM
Automatic Form update rlmoss44 Word VBA 6 12-03-2015 09:27 AM
help needed for automatic update of footer sanju71821 Word 7 07-01-2015 08:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:52 AM.


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