Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-20-2021, 07:09 AM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default Macro doesnt work inside textbox


I use this macro to change specific text, but it doesnt work, when text is inside text boxes. If text is in table cell, then macro work fine. What I must change in code, that will affect text in text boxes too, not just in table cell?
This is code:
Code:
Sub Podpisi()
With ActiveDocument.Content.Find
 .Execute FindText:="blue", ReplaceWith:="Mr." & vbCr & "Adrian Tomc", _
 Format:=True, Replace:=wdReplaceAll
End With
End Sub
Reply With Quote
  #2  
Old 03-20-2021, 07:36 AM
gmaxey gmaxey is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You are in the wrong story range


Code:
Sub Podpisi()
Dim oRng As Range
 Set oRng = ActiveDocument.StoryRanges(wdTextFrameStory)
 With oRng.Find
   .Execute FindText:="help", ReplaceWith:="Mr." & vbCr & "Adrian Tomc", _
 Format:=True, Replace:=wdReplaceAll
End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 03-22-2021, 02:51 PM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default

Tnx for reply but it doesnt work.

I get Run-time error '5941' the requested member of the collection does not exist.
Any ideas why?
Reply With Quote
  #4  
Old 03-22-2021, 02:53 PM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default

Tnx for reply but it doesnt work.

I get Run-time error '5941' the requested member of the collection does not exist.
Any ideas why?
Reply With Quote
  #5  
Old 03-22-2021, 03:56 PM
Guessed's Avatar
Guessed Guessed is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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 error is most likely saying that there aren't any text frames in the document you are running the macro on.

Perhaps you would like to post a document with your 'text box' so we can make a macro that works with what you actually have.

Alternatively, the macro could be modified to work across every type of story range. The current code examples are specific to one range only.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 03-22-2021, 05:48 PM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default

Here is my file. Tnx for helping.
Attached Files
File Type: docm Doc2.docm (34.4 KB, 12 views)
Reply With Quote
  #7  
Old 03-22-2021, 06:34 PM
Guessed's Avatar
Guessed Guessed is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

Those empty boxes look like embedded OLE objects. Is this really what you are working with?

They don't appear to be editable to me. How do YOU put text inside them?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 03-22-2021, 06:58 PM
gmaxey gmaxey is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Those are ActiveX controls. Why are you using those?


Sub test()
With TextBox1
.MultiLine = True
.Text = Replace(TextBox1.Text, "blue", "Mr." & vbCr & "Adrian Tomc")
End With
With TextBox11
.MultiLine = True
.Text = Replace(TextBox1.Text, "blue", "Mr." & vbCr & "Adrian Tomc")
End With
With TextBox12
.MultiLine = True
.Text = Replace(TextBox1.Text, "blue", "Mr." & vbCr & "Adrian Tomc")
End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 03-22-2021, 09:25 PM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default

Thank you all for help.
Reply With Quote
  #10  
Old 03-29-2021, 10:20 PM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default

gmaxey, yours solution works perfect, but can you help me one more time?
How to replace text, no matter, if text is in TextBox1, 2 or 3? I will have multiple options of input text and replace every word with other text, so it is a little unpractical to assign all options to every text box. Macro shoud lookup for specific text in all textboxes and replace it. Is it possible?

Last edited by Charles Kenyon; 03-30-2021 at 08:39 AM.
Reply With Quote
  #11  
Old 03-29-2021, 11:19 PM
Guessed's Avatar
Guessed Guessed is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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

Can we convince you to use something other than ActiveX Controls?

You 'should' be using Content Controls for this task. Then the coding will be much simpler and extensible.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #12  
Old 03-30-2021, 02:36 AM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default

Ok. Let me explain, what I want to do.

I must have 3 text fields, because I will use this for server based signing word documents. I dont care, what kind of text field. I use ActiveX controls, because I can mask their text like password with ***. If is this possible with Content controls, fine by me. But I dont know, how to do that.

So...in macro I will have several "passwords" saved from several users. Now, 3 random users will type in textfields their passwords. I will not know, who and what. Macro must reveal their passwords and replace this "password" words with correct titles.
For revealing passwords I use this code with checkbox:
Code:
Private Sub CheckBox1_Click()
TextBox1.PasswordChar = IIf(CheckBox1, "", "*")
TextBox11.PasswordChar = IIf(CheckBox1, "", "*")
TextBox12.PasswordChar = IIf(CheckBox1, "", "*")
End Sub

When passwords was revealed, I run macro, that will change this words to proper titles.
Answer from gmaxey working fine, problem is, that I dont know who and in which textbox will write his password. So...Macro must be able to check this too.
Before I use this code:
Code:
Sub Podpisi()
With ActiveDocument.Content.Find
 .Execute FindText:="winki", ReplaceWith:="V. d. strokovnega direktorja" & vbCr & "Valentina Winkler Skaza, dr. med., spec. psih.", _
 Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find
 .Execute FindText:="mario123", ReplaceWith:="Pomočnik direktorja za področje zdravstvene nege " & vbCr & "Mario Dremšak, mag. manag., dipl. zn.", _
 Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find
 .Execute FindText:="onsightclimb", ReplaceWith:="Bojan Strauss, ZT", _
 Format:=True, Replace:=wdReplaceAll
End With
End Sub
and work fine but did not work with textboxes.
Now, I have solution from gmaxey, it working fine, but I have to assign all possible options to every box, right?


Now, if anybody understand from my poor english, what I try to accomplish, can you help me? I dont care, if I use something else and not Activex?


TnX
Reply With Quote
  #13  
Old 03-31-2021, 03:51 PM
Guessed's Avatar
Guessed Guessed is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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 don't understand why there needs to be multiple steps so my solution may not be what you wanted. I would approach this by making use of checkbox content controls and having a macro check the user logon name to determine how to expand the user details.

This approach then just expects the user to just check a box and then move their cursor in order to see their details replace the checkbox immediately. To me this is much simpler in terms of usability.

I've attached a demo document showing how this is set up. The code that is being used in the file is
Code:
Private Sub Document_ContentControlOnExit(ByVal aCC As ContentControl, Cancel As Boolean)
  Dim sUser As String, sID As String
  sID = Environ$("UserName")
  If aCC.Type = wdContentControlCheckBox Then
    If aCC.Checked Then
      sUser = ExpandUser(sID)
      If sUser <> "" Then
        aCC.Type = wdContentControlText
        aCC.MultiLine = True
        aCC.Range.Text = sUser
      End If
    End If
  End If
End Sub

Function ExpandUser(sID As String) As String
  Select Case sID
    Case "winki", "winkidelegate"
      ExpandUser = "V. d. strokovnega direktorja" & vbCr & "Valentina Winkler Skaza, dr. med., spec. psih."
    Case "mario123"
      ExpandUser = "Pomocnik direktorja za podrocje zdravstvene nege " & vbCr & "Mario Dremšak, mag. manag., dipl. zn."
    Case Else
      Application.StatusBar = "Not a recognised user: " & sID
  End Select
End Function

Sub ResetCheckBoxes()
  Dim aCC As ContentControl
  For Each aCC In ActiveDocument.SelectContentControlsByTag("Signed")
    aCC.Type = wdContentControlCheckBox
    aCC.Checked = False
  Next aCC
End Sub
Attached Files
File Type: docm Demo.docm (32.2 KB, 12 views)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #14  
Old 04-02-2021, 09:55 AM
onsight onsight is offline Macro doesnt work inside textbox Windows 10 Macro doesnt work inside textbox Office 2016
Novice
Macro doesnt work inside textbox
 
Join Date: Mar 2021
Posts: 9
onsight is on a distinguished road
Default

Thank you for that but this is not what I need. Thank you anyway.
I use this for signing documents. There are about 15 people, that sign documents. I place docunemt on server, send link and 3 workers, assign for sign document can acces to file. Nobody can not know for password of other. So...they just read document, if they agree with content, they must write their password in one of those 3 textboxes. Becouse of that, passwords must be masked. I can do that only with textboxes.
I am documents admin. When I see all 3 password filled, I then with macro running, convert those 3 passwords to original titles. Macro will be locked and only I can run macro.
My two codes in original post working perfect...problem is, that they dont work inside textboxes. And my solution have multiple steps. Now I search for solution just with one step and that will work with textboxes too.
Is it possible or not?
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro doesnt work inside textbox DocumentBeforePrint doesnt work Gerrit Word VBA 6 02-20-2021 08:53 AM
Macro doesnt work inside textbox Code doesnt work properly ksigcajun Word VBA 6 04-06-2015 07:41 AM
Safe Senders Doesnt work Anil Kaul Outlook 0 11-16-2012 01:28 PM
Macro doesnt work inside textbox Command Button doesnt work on network rmw85 Word VBA 1 04-25-2012 01:02 PM
Macro doesnt work inside textbox My Product code doesnt work!!!! PLEASE HELP dukquaknoobhack Office 1 01-05-2012 03:43 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:49 PM.


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