PDA

View Full Version : Solved: Word automation struggle



newk
11-18-2004, 04:10 AM
Hi

I would just like to start by saying my advanced word skills suck, I haven?t used VB since I was at university (approx 3 years ago) so it?s unsurprising I have hit a brick wall.

I need to create a dialog box in Word that will ask for details such as addressee?s information and then automatically enter this into Word in the relevant place in the required font etc.

My main reason for posting is that on the form I need 3 radio buttons. Each button will have a region on it: ie. London, Manchester etc. These are the regions of our offices. I want staff to select a radio button, which in turn will print the correct address in the footer of the page. How can I do this?

Does anyone know of web links that may explain this? I would take a course but I have until Dec to create 250 templates.

Any help would be greatly appreciated! :mkay

Jacob Hilderbrand
11-18-2004, 04:27 AM
You can create a User Form or use an Input Box to get user input. Where does this data go? Are there bookmarks or fields etc.

Can you post an attachment of the document so we can get a better idea of what you want to do?

newk
11-18-2004, 05:31 AM
Hi again,

Sorry it took so long I had to rustle up something. So far I must have 20 failed attempts so choosing one to show you was a challenge.

This example shows the general layout. It doesn't do what I need it to.

I would like to make this a template that on opening, brings up a dialog box that prompts for:

Addressee's Full Name
Title
Company Name
Street Address etc.

0 Manchester (will input the Manchester branch adress into the footer)
0 London (will input the London branch into the footer)

I really can't get to grips with the bookmarks business. I thought that was how I should go about this but I am lost on how to implement it.

Please let me know if you need more info.

...and thanks for the help DRJ

Jacob Hilderbrand
11-18-2004, 06:04 AM
Try this out. I have some bookmarks setup for the name, address etc. spots on the document. A User Form will pop up when the document is opened and ask for the required inputs.

newk
11-18-2004, 09:13 AM
Thanks DRJ

Thats certainly the first bit. I will work through and figure out what you did.

But how would I store 3 addresses within Word so by selecting a radio button on that form you created it would automatically print the correct address at the bottom of the sheet instead of being prompted for each line of the address at the beginning?
Also can you use the same input to output twice on the screen. For example:
You get prompted once and Word prints the answer in two seperate areas within the document?

Jacob Hilderbrand
11-18-2004, 04:35 PM
You can output twice or more. Take a look at the code and see how I refer to the TextBox, just refer to it again to put the text somewhere else. You can also put the addresses into the VBA code and pull the text from there or save it as Auto Text.

newk
11-19-2004, 07:00 AM
Thanks for all your help yesterday DRJ.

I have tried a couple of times to get the radio buttons to do their thing, but they really aren't playing.

I know it's a lot to ask but would you mind taking a quick look at it?

Jacob Hilderbrand
11-19-2004, 08:25 AM
Try this code. It is only partially completed but it should show you how to proceed. Let me know if you need additional assistance.

Option Explicit

Private Sub CommandButton1_Click()

Selection.GoTo What:=wdGoToBookmark, Name:="AddresseesTitle"
Selection.Range.Text = Me.TextBox1.Text
Selection.GoTo What:=wdGoToBookmark, Name:="AddresseesFullName"
Selection.Range.Text = Me.TextBox2.Text
Selection.GoTo What:=wdGoToBookmark, Name:="Senders_Title"
Selection.Range.Text = Me.TextBox3.Text
Selection.GoTo What:=wdGoToBookmark, Name:="SendersFullName"
Selection.Range.Text = Me.TextBox4.Text
Selection.GoTo What:=wdGoToBookmark, Name:="CompanyName"
Selection.Range.Text = Me.TextBox5.Text
Selection.GoTo What:=wdGoToBookmark, Name:="FaxNumber"
Selection.Range.Text = Me.TextBox6.Text
Selection.GoTo What:=wdGoToBookmark, Name:="Subject"
Selection.Range.Text = Me.TextBox7.Text

Select Case True

Case Is = Me.OptionButton1
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.MoveDown Unit:=wdLine, Count:=9
Selection.Range.Text = "Sovereign House, Church Street, Brighton BN 1MA"
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
With Selection
.Font.Size = 8
.Paragraphs.Alignment = wdAlignParagraphLeft
End With
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Selection.Range.Text = "Tel: 01273 865452 Fax:01273 865995 Email: info@sony.co.uk"
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
With Selection
.Font.Size = 8
.Paragraphs.Alignment = wdAlignParagraphLeft
End With
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Case Is = Me.OptionButton2

Case Is = Me.OptionButton3

End Select

Unload Me

End Sub

newk
11-19-2004, 09:29 AM
Ok my last question on this topic I promise!

That code you gave me worked a treat, thank you.

I finished up, it all worked. I saved it as a document template and now the form doesn't pop up on opening. There was a fillin field, that came up (so I deleted it) But it still isn't automatically popping up. What can I do?

mdmackillop
11-19-2004, 03:33 PM
Hi Newk,
Have a look at the demo here.
MD
http://www.vbaexpress.com/kb/getarticle.php?kb_id=184

newk
11-30-2004, 09:12 AM
Thanks MD. That is very useful.

newk
12-01-2004, 07:28 AM
I keep finishing up documents sending them to friends and the form stops working. I assume this is because the coding is somehow being disenguaged or lost by word.

Could someone tell me
1)how to check code is saved with the file
2)how to make a document a stand alone template.

Bilby
12-06-2004, 02:50 PM
newk,
Can you post a sample document? There are basically two main causes for a document goin on strike.
a. The documents looking for its template (or some code has been saved in your local copy of Normal.dot) and can't find it.
b. Security settings on the recipients machine disable the macros (it can't tell the difference between your magic and a nasty macro virus).

As for point 1. VBA code is automatically saved with the document/template PROVIDED its actually in the doc. If you've recorded macros then they're in Normal.dot unless you have copied them over. Just shut down Word, open your doc/template in a new clean session and you should find your code/userforms in the VBE.

Point 2. Save your document as a template (extension .dot), or preferably create a new template specifically and then copy over your code.

mdmackillop
12-06-2004, 03:02 PM
Hi Newk,
Are you talking about my sample form? If so can you check the References you have running which is where I suspect the problem may lie.
MD

newk
12-15-2004, 09:10 AM
Thanks guys for all your help,

After playing around with it for ages I think I discovered the problem.

The script was being driven by a OPEN DOCUMENT command (Jacob gave me exactly what I asked for ref: post1). I changed this to an AUTONEW macro and it seems to have worked.

It's a very subtle difference, but don't fall into that trap it took me so long and I felt like a spack when I worked it out.

Once again thanks everyone for all your ideas and help, I'd never have got there without you. :kiss