Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-07-31
6
Medium Priority
?
1,234 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 1

Author Comment

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

Capture.PNG
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

773 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