Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-16-2015, 08:34 AM
cc.caroline15 cc.caroline15 is offline help creating macro in word Windows 8 help creating macro in word Office 2013
Novice
help creating macro in word
 
Join Date: Jan 2015
Posts: 9
cc.caroline15 is on a distinguished road
Default help creating macro in word


Hello everyone I'm trying to create a macro(CreateTables) in word that when User enters Windows or MSWord or DB, each student enrolled in the respective class (Windows XP Level 1, MSWord Level 1, Database Programming – VBA) class is displayed in a separate table containing rows for ID, First, Last, Department name, Telephone and Classname. All values are placed in fields and records (tables) are sorted by last name ascending. Depicted here after DB is entered: like the one in the picture. This data will be extracted from a access file. but my code gives me the following error: Compile error: user defined type not defined. Can anyone please help me?? I have attach the word document and a picture of how the table should look in word. thank you
Attached Images
File Type: png table.PNG (51.3 KB, 42 views)
Attached Files
File Type: docm 243CCA04.docm (19.9 KB, 9 views)
Reply With Quote
  #2  
Old 02-16-2015, 04:57 PM
macropod's Avatar
macropod macropod is offline help creating macro in word Windows 7 64bit help creating macro in word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could do this quite easily with either a Letter or Directory mailmerge. No code required.

Simply create a document with a single table as per your layout, start the mailmerge wizard and connect to the data source, then insert the mergefields in the second column. Once you've done that, 'Finish' the merge. If needed, you can sort the list (before finishing) via Edit Recipient List > Sort.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-16-2015, 06:36 PM
Guessed's Avatar
Guessed Guessed is offline help creating macro in word Windows 7 32bit help creating macro in word Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

As macropod suggests, this would be a lot easier to do as a mail merge.

FWIW, your code has obvious errors which cause compilation errors but I can't test to iron out any other issues related to the things we can't see there.

DefaultTableBehaivor is not spelt correctly and needs to say DefaultTableBehavior
strPassword -InputBox("Password Please?", "Lock Forms Password") should have an equals sign instead of the minus

Add Dim strEmps As String, strPath As String to the start of the Sub Connect macro

Public connEmp As ADODB.Connection - this line fails to compile because you haven't added a reference to one of the Microsoft ActiveX Data Object x.x Library
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 02-17-2015, 06:38 PM
cc.caroline15 cc.caroline15 is offline help creating macro in word Windows 8 help creating macro in word Office 2013
Novice
help creating macro in word
 
Join Date: Jan 2015
Posts: 9
cc.caroline15 is on a distinguished road
Default

Hi macropod I'm required to do it the hard way with code
Guessed I made the changes you suggested the code doesn't give me that error any more but now I get the following error:
Run Time error 91 Do you have anymore advice?? I have attach the word document with the changes and the access database as well. Thank you
Reply With Quote
  #5  
Old 02-17-2015, 06:43 PM
Guessed's Avatar
Guessed Guessed is offline help creating macro in word Windows 7 32bit help creating macro in word Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I can't see the attachments
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 02-17-2015, 06:46 PM
cc.caroline15 cc.caroline15 is offline help creating macro in word Windows 8 help creating macro in word Office 2013
Novice
help creating macro in word
 
Join Date: Jan 2015
Posts: 9
cc.caroline15 is on a distinguished road
Default

here are the files
Attached Files
File Type: zip 243CCA04.zip (47.8 KB, 8 views)
Reply With Quote
  #7  
Old 02-17-2015, 07:42 PM
Guessed's Avatar
Guessed Guessed is offline help creating macro in word Windows 7 32bit help creating macro in word Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Caroline

I don't want to do your homework for you. I don't mind helping you past sticking points but you need to make a reasonable effort first.

First, do a 'Compile Project' and resolve all of those errors. For instance sngRecords is not a Connection and is not required in the Connect sub.

Second, build an example of the SQL query you want in your access database. Go to SQL view and look at what it looks like. Now compare it to the strEmps that your code creates - you will spot several errors that need resolving.

Once you have those resolved, we can move forward with the rest of what you need to do.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 02-22-2015, 01:45 PM
cc.caroline15 cc.caroline15 is offline help creating macro in word Windows 8 help creating macro in word Office 2013
Novice
help creating macro in word
 
Join Date: Jan 2015
Posts: 9
cc.caroline15 is on a distinguished road
Default

Hi Guessed,
I re-wrote the SQL statement I think it's right this time and I also notice I was running the wrong macro the one that I was suppouse to run was the ListEmps but when I run it I get the run time error: Not a valid file name. Could you help me please??
Thank you
Attached Files
File Type: docm 243CCA04.docm (24.8 KB, 7 views)
Reply With Quote
  #9  
Old 02-22-2015, 03:37 PM
Guessed's Avatar
Guessed Guessed is offline help creating macro in word Windows 7 32bit help creating macro in word Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If you are getting a 'not valid file name error' then it is likely that a file with that name doesn't exist.

There is a difference between cstrPath and strPath - are you sure you are using the right variable when you open the connEmp? It looks like you intended to build the full path by getting the current path and appending the constant to point inside a folder BUT your code is not quite doing that.

Add the following lines before the connEmp.Open line to check the value of cstrPath and strPath

Debug.Print cstrPath
Debug.Print strPath
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 02-23-2015, 01:26 PM
cc.caroline15 cc.caroline15 is offline help creating macro in word Windows 8 help creating macro in word Office 2013
Novice
help creating macro in word
 
Join Date: Jan 2015
Posts: 9
cc.caroline15 is on a distinguished road
Default

Hi Guessed
I was storing both files on my one drive cloud storage from outlook and as soon as I move them to my C drive on the computer that error stop! I don't understand why was this? now I'm getting the following error so I made some changes to the SQL statement again but still not working.. do you have any more advice?? I'm new to programming and SQL statements has been a hard subject for me
Thank you.
Attached Images
File Type: png errorvba.PNG (15.3 KB, 27 views)
Attached Files
File Type: docm 243CCA04.docm (25.3 KB, 7 views)
Reply With Quote
  #11  
Old 02-23-2015, 04:53 PM
Guessed's Avatar
Guessed Guessed is offline help creating macro in word Windows 7 32bit help creating macro in word Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Build the sql query in your access database before attempting to replicate it in your code. If you use the interface of the Access query wizard then you don't need to write this using sql directly. Once the query is built, you can look at it in SQL view and then copy that across to your VBA code.

At the very least you need to get the spaces right in your SQL string. The square brackets are useful when field names have spaces but are optional in what you appear to be dealing with however you MUST have spaces in the right places and can't truncate field names like you do.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
help creating macro in word help creating macro tingker Word VBA 4 01-08-2015 08:58 PM
help creating macro in word Need Help Creating Macro rsrasc Word VBA 1 10-22-2014 03:09 PM
help creating macro in word Need Help Creating Macro rsrasc Word VBA 4 03-28-2014 01:31 PM
Creating a MACRO Nikb3522 Word VBA 0 10-21-2010 05:55 PM
creating macro steveb Word VBA 0 08-14-2010 01:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:44 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft