Solved

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

Posted on 2014-07-31
6
982 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
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: …
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.

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