Solved

Word VBA, add autotext with data from the userform only if checked

Posted on 2014-07-31
6
922 Views
Last Modified: 2014-08-01
Hi guys,

I'm still novice with VBA so still learning, im trying to get my head around a functionallity that should be able to insert a Specific autotext (a table with some placeholders, which are embedded in the document already). and insert the text which you have entered in the userform, but only if its checked.

So in short.
* Tick a checkbox in the userform to true
* It will insert an Autotext, which is a placeholder at a bookmark - the placeholder itself has other bookmarks which needs to retrieve data from the userform.

I'm sorry if the explanation is a bit confusing, but i have attached a document with what i got so far... you might get a better insight of what i'm trying to do :)

testdoc.dotm.jpg - Please rename the file to "testdoc.dotm", the *.dotm file extention was not allowed to be uploaded

Thank you in advance
0
Comment
Question by:Hakum
  • 4
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40231412
I think that you need something like this:
Private Sub CommandButton1_Click()
    If CheckBox1.Value = True Then
        My Template.BuildingBlockEntries("MyAutoText").Insert, _
             Where:=MyDocument.Bookmarks("MyBookMark1").Range, RichText:=True
        MyDocument.Bookmarks("MyBookMark2").Range.Text = TextBox1.Text
    End If
End Sub

Open in new window

I am having trouble with your file. My browser tries to display it.
That is because you embedded it into the question after uploading it.
0
 
LVL 1

Author Comment

by:Hakum
ID: 40231473
try to right click on it and save it as link and rename it to "testdoc.dotm"
0
 
LVL 1

Author Comment

by:Hakum
ID: 40231488
i tried the code but its not working :S

please try to view the document and you might get a better idea of what i'm thinking

thank you in advance for you time :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:Hakum
ID: 40231522
I guess this is a alternative

Capture.PNG
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40232022
You would need to change the code so that the names of the Autotext, Bookmarks, Userform controls and template fit your situation. I notice that you have put the Autotext in the template, so this code shows how to find the template for the active document.
Private Sub cmdOK_Click()
Dim tmpl As Template
If Me.CheckBox1.Value = True Then
    Set tmpl = ActiveDocument.AttachedTemplate
       tmpl.BuildingBlockEntries("Placeholder1").Insert _
       ActiveDocument.Bookmarks("Placeholder1").Range, True
        ActiveDocument.Bookmarks("Text1").Range.Text = Me.txttext1.Text
End If
MsgBox "Document pushed ok"

End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:Hakum
ID: 40233641
Works like a charm!!!! thank you soo much for your time :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now