Solved

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

Posted on 2014-07-31
6
1,008 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article will show you how to use shortcut menus in the Access run-time environment.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

838 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