Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-06-2019, 12:27 PM
aee74 aee74 is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2013
Novice
Userform selectbox to insert autotext
 
Join Date: Feb 2019
Posts: 5
aee74 is on a distinguished road
Default Userform selectbox to insert autotext

Hello,

I made a userform in wich I have a selectionbox. If this box is selected (true) I need to insert some autotext in my document at a bookmark.



I’ve created a bookmark “bm1”
I’ve created an autotext with alt F3 and gave the name bm1 and saved it to my document template.

I followex this link https://answers.microsoft.com/en-us/msoffice/forum/all/vba-code-for-inserting-autotext/3c38f1a5-6f8b-4df6-ab72-b440004a304d
But it does not work. (I changed the bm names)

Any help would be welcome
Reply With Quote
  #2  
Old 02-07-2019, 03:54 AM
gmayor's Avatar
gmayor gmayor is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The code I posted in the linked thread does work, provided you identify the autotext location correctly and apply the correct names for the autotext entry and the bookmark.



Code:
Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
    On Error GoTo lbl_Exit
    With ActiveDocument
        Set oRng = .Bookmarks(strbmName).Range
        Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
                   (Where:=oRng, RichText:=True)
        .Bookmarks.Add Name:=strbmName, Range:=oRng
    End With
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-07-2019, 10:37 PM
aee74 aee74 is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2013
Novice
Userform selectbox to insert autotext
 
Join Date: Feb 2019
Posts: 5
aee74 is on a distinguished road
Default

[I will try this again tonight. But as I tried before nothing happens when I run the code. The code compiles without errors so I the code should be fine. The bookmarks are in the document and the autotext is saved in the template

QUOTE=gmayor;138239]The code I posted in the linked thread does work, provided you identify the autotext location correctly and apply the correct names for the autotext entry and the bookmark.



Code:
Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
'strBMName is the name of the bookmark to fill
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
Dim oRng As Range
    On Error GoTo lbl_Exit
    With ActiveDocument
        Set oRng = .Bookmarks(strbmName).Range
        Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
                   (Where:=oRng, RichText:=True)
        .Bookmarks.Add Name:=strbmName, Range:=oRng
    End With
lbl_Exit:
    Exit Sub
End Sub
[/QUOTE]
Reply With Quote
  #4  
Old 02-07-2019, 11:37 PM
gmayor's Avatar
gmayor gmayor is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If it is not working, comment out the On Error line and see where it fails.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 02-09-2019, 12:39 AM
aee74 aee74 is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2013
Novice
Userform selectbox to insert autotext
 
Join Date: Feb 2019
Posts: 5
aee74 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
If it is not working, comment out the On Error line and see where it fails.
Still not working and no errors.

I'm doing something wrong but can't find out what it is.

Code:
Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
If CheckBox1 = True Then
'strBMName is the name of the bookmark to fill
'strbmname should be "bm1"
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'oTemplate should be ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
'strAutotext should be "tekstbm1"
'How to declare these values?
Dim oRng As Range
'    On Error GoTo lbl_Exit
    With ActiveDocument
        Set oRng = .Bookmarks(bm1).Range
        Set oRng = oTemplate.AutoTextEntries(tekstbm1).Insert _
                   (Where:=oRng, RichText:=True)
        .Bookmarks.Add Name:=bm1, Range:=oRng
    End With
lbl_Exit:
    Exit Sub
    End If
        'no errors, but also no text has been placed
End Sub
Attached Files
File Type: dotm test.dotm (37.8 KB, 21 views)

Last edited by aee74; 02-09-2019 at 08:53 AM. Reason: I would like to use richtext option
Reply With Quote
  #6  
Old 02-09-2019, 11:38 AM
aee74 aee74 is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2013
Novice
Userform selectbox to insert autotext
 
Join Date: Feb 2019
Posts: 5
aee74 is on a distinguished road
Default

Quote:
Originally Posted by aee74 View Post
Still not working and no errors.

I'm doing something wrong but can't find out what it is.

Code:
Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
If CheckBox1 = True Then
'strBMName is the name of the bookmark to fill
'strbmname should be "bm1"
'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate
'oTemplate should be ActiveDocument.AttachedTemplate
'strAutotext is the name of the autotext entry
'strAutotext should be "tekstbm1"
'How to declare these values?
Dim oRng As Range
'    On Error GoTo lbl_Exit
    With ActiveDocument
        Set oRng = .Bookmarks(bm1).Range
        Set oRng = oTemplate.AutoTextEntries(tekstbm1).Insert _
                   (Where:=oRng, RichText:=True)
        .Bookmarks.Add Name:=bm1, Range:=oRng
    End With
lbl_Exit:
    Exit Sub
    End If
        'no errors, but also no text has been placed
End Sub

Is it also possible to insert rich text?

I have an autotext with with a header in bold and text on normal lettertype.

Als i have some bullets wich I need to insert.

Hope you can help
Reply With Quote
  #7  
Old 02-09-2019, 08:04 PM
Guessed's Avatar
Guessed Guessed is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

You have posted a sub with input variables which means this macro can't be run directly, it has to be passed some extra information in order to run). The sub also needs modification in order to use the variables that it expects to be passed in. To run it, you need a separate subroutine which passes in the variables and also to fix the function. Try
Code:
Sub Test_AutoTextToBM()
  AutoTextToBM strbmName:="bm1", oTemplate:=ActiveDocument.AttachedTemplate, strAutotext:="tekstbm1"
End Sub

Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
  'If CheckBox1 = True Then
  Dim oRng As Range
  On Error GoTo lbl_Exit    'will avoid errors if bookmark doesn't exist or AT doesn't exist in attached template
  Set oRng = ActiveDocument.Bookmarks(strbmName).Range
  Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert(Where:=oRng, RichText:=True)
  ActiveDocument.Bookmarks.Add Name:=strbmName, Range:=oRng
lbl_Exit:
  Exit Sub
  'End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 02-09-2019, 09:48 PM
gmayor's Avatar
gmayor gmayor is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Andrew has pointed out the problem, corrected in the attached.
The process will insert whatever the autotext entry contains.
Attached Files
File Type: dotm test.dotm (41.6 KB, 25 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 02-10-2019, 03:39 AM
aee74 aee74 is offline Userform selectbox to insert autotext Windows 10 Userform selectbox to insert autotext Office 2013
Novice
Userform selectbox to insert autotext
 
Join Date: Feb 2019
Posts: 5
aee74 is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
You have posted a sub with input variables which means this macro can't be run directly, it has to be passed some extra information in order to run). The sub also needs modification in order to use the variables that it expects to be passed in. To run it, you need a separate subroutine which passes in the variables and also to fix the function. Try
Code:
Sub Test_AutoTextToBM()
  AutoTextToBM strbmName:="bm1", oTemplate:=ActiveDocument.AttachedTemplate, strAutotext:="tekstbm1"
End Sub

Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
  'If CheckBox1 = True Then
  Dim oRng As Range
  On Error GoTo lbl_Exit    'will avoid errors if bookmark doesn't exist or AT doesn't exist in attached template
  Set oRng = ActiveDocument.Bookmarks(strbmName).Range
  Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert(Where:=oRng, RichText:=True)
  ActiveDocument.Bookmarks.Add Name:=strbmName, Range:=oRng
lbl_Exit:
  Exit Sub
  'End If
End Sub

It works. Great. Thanks
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
insert Autotext via button only from specified category lodi123 Word VBA 0 07-26-2017 02:57 AM
Userform selectbox to insert autotext How do I programmatically insert and append the text from AutoText 0002 to the end of the existing t erik816 Word VBA 1 03-09-2016 12:48 AM
UserForm of Autotext Entries ptmuldoon Word VBA 11 02-13-2016 09:36 AM
Userform selectbox to insert autotext How do i insert autotext using a contol toolbox checkbox NP85 Word VBA 12 02-09-2014 03:06 PM
Userform selectbox to insert autotext Insert identifiers in AutoText snippet. Microsoft word deepgreen Word 13 01-21-2014 05:16 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:12 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