Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 45

Thread: Solved: Wrote a Macro in Word, now What???

  1. #1
    VBAX Regular
    Joined
    Aug 2004
    Posts
    13
    Location

    Smile Solved: Wrote a Macro in Word, now What???

    I have Word 97. I went into Tools...Macro...Visual Basic Editor and wrote the code and created an "online" order form for our sales reps. I don't know what to do with it now...When I want to view the order form, I have to open Word...Tools...Macro...Visual Basic Editor then Run it. Obviously, this is not acceptable to give to our reps. I need them to be able to simply open the form, enter their info and print it. Unfortunately, the person that helped me write the code is not familiar with Word's Visual Basic and can not help me. I did not know what a Macro or Code was before Monday so I am dead in the water. Help please!!! Thanks!

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi n2mopars,

    Welcome to VBAX!

    The easiest way to run a macro as a one-off is via Tools > Macro > Macros (shortcut, Alt+F8), select the macro from the list and press Run. For regular use a button, either on a toolbar or in the document, is better, or maybe just having it auto run when the document is opened.

    I can't be sure based on what you've posted so far which would be best in your circumstances. Can you post a bit more, and perhaps a sample document?

    Thanks
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Regular
    Joined
    Aug 2004
    Location
    Mesquite, Texas
    Posts
    13
    Location
    I just looked in Word help and there are references to an AutoExec and AutoRun macro, tha will cause the macro or form to open automatically when the document was open.
    I couldn't get help to give me details, but I know from experience that if you tinker around in help long enought you can stumble over some gems.

    Maybe you will have more luck looking at this than I did.

  4. #4
    VBAX Regular
    Joined
    Aug 2004
    Posts
    13
    Location

    Wrote a Macro in Word, now What???

    Tony and sgrant,

    Thank you for the quick responses. I have attached a copy of the document for Tony. Hopefully you can help me with this...Thanks again!

    Julie

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by n2mopars
    ... I have attached a copy of the document for Tony. Hopefully you can help me with this...
    Where is it at? I don't see it. Did you zip it? Did you browse for it, then press the Upload button?

  6. #6
    VBAX Regular
    Joined
    Aug 2004
    Posts
    13
    Location

    Wrote a Macro in Word, now What???

    Yes, I zipped, browsed and uploaded. When I tried it again it said there was an upload error. I can go to the zip file on my computer and open it and look at it ok. Any ideas on why it won't attach?

  7. #7
    VBAX Regular
    Joined
    Aug 2004
    Posts
    13
    Location

    Wrote a Macro in Word, now What???

    Hey Guys!

    For whatever reason, probably a Word thing, it appears to have attached this time...Let me know! Thanks~!!

    Julie

  8. #8
    VBAX Regular JOrzech's Avatar
    Joined
    Jun 2004
    Location
    Upstate New York
    Posts
    83
    Location

    AutoNew

    I think the easiest to way to make your code run is to write the code in an AutoNew macro. That way, when the user clicks on a new template to create a new document, your macro will run.

    So add a module called AutoNew and paste this code into it:

    [VBA]
    Sub AutoNew()
    Load UserForm1
    UserForm1.Show
    End Sub
    [/VBA]
    I usuallly set all textboxes, etc. to contain nothing ... but let's see if this works for you. Let me know if you need more info.

    P.S. I know they say to use Document_New but it doesn't work as well for me....
    Joanne

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi n2mopars,

    Several things I note about your document.

    Firstly it is a document (.doc) and not a template (.dot) - Joanne's suggestion of an AutoNew macro will work in a template - in a document you will want AutoOpen instead.

    Either way I guess you need to know where to put the code. Open up Module1 (double click on it in the explorer at the left of the VBE screen) and paste the code in the window which opens on the right. If you do it in your document as is, you'll need to change the line Sub AutoNew() to read Sub AutoOpen()

    Your document has several unused objects - Userform2, Userform3, Userform4, Userform5, Module2, Class1 - these can all be deleted. Module1 is also unused but you're going to use it for the new code so don't delete that one.

    I don't see what will happen after the form has been displayed - there isn't any obvious mechanism for doing anything with the data entered in the form - I haven't studied it closely so I may have missed something - how do you intend to use it? What is it that you want to print? And do you want to save the result as well?

    Finally, AFAIK there is no benefit in explicitly Loading the Userform as Joanne has done and I think this is sufficient code:

    [VBA]Sub AutoNew()
    UserForm1.Show
    End Sub[/VBA]

    or
    [VBA]Sub AutoOpen()
    UserForm1.Show
    End Sub[/VBA]


    If you need more help then please post back.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Regular JOrzech's Avatar
    Joined
    Jun 2004
    Location
    Upstate New York
    Posts
    83
    Location
    Thanks for correcting me Tony... I must have skimmed over the "doc" part.

    I've corrected the document.
    Joanne

  11. #11
    VBAX Regular JOrzech's Avatar
    Joined
    Jun 2004
    Location
    Upstate New York
    Posts
    83
    Location
    Tony -

    I think I'm still pretty clumsy about Word VBA. Why would he/she need Module1? I would add a command button to UserForm1 and run the code from there. Please educate me.

    And Tony is correct - you will need to populate the Word document with the responses from UserForm1. I use DOCVARIABLES to do that, but I understand most people don't like them. I use Insert, Field, DocVariable - give it a name in the Word document, then I populate it by code like this:

    [VBA]
    ActiveDocument.Variables.Add Name:=("BlahBlah"), Value:=UserForm1.TextBox17.Text
    [/VBA]

    Then you would have to update your fields:

    [vba]
    Sub UnlinkFields()
    Selection.WholeStory
    Selection.Fields.Update
    'this is optional - it turns the fields to text
    Selection.Fields.Unlink
    Selection.HomeKey
    End Sub
    [/vba]

    And unload/hide your UserForm
    Joanne

  12. #12
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Joanne,

    I suggested he put the AutoOpen in Module1 which was why he needed to keep it - of course it can, and probably should, go in ThisDocument. Sorry to confuse.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I think Joanne is refering to myself, with my usual loud mouth opinion. DOCVARIABLES has their purpose. I use them, but also I sometime make an Active X control and resize them to a single character, and make them essentially invisible. You can store a LOT of data in there, it is easily retrievable, and unlike DOCVARs have their own firing events.

  14. #14
    VBAX Regular
    Joined
    Aug 2004
    Posts
    13
    Location
    Tony,

    I was able to get it working placing the code in ThisDocument. I still have a question, though. It opens the user form but behind it is the page with my letterhead on it. If you view the sample that is on Joanne's post that she corrected, it opens the same way. Do you (or anyone) have a suggestion to make the flolaborderformtemplate.doc page not appear? Thanks a million everyone, we're almost there! :0)

    Julie

  15. #15
    VBAX Regular JOrzech's Avatar
    Joined
    Jun 2004
    Location
    Upstate New York
    Posts
    83
    Location
    n2mopars:

    First of all, I'd make it a template.

    Secondly, the document will normally show up in the background and then once populated by the VB code, it will assemble on the same screen. I have a suggestion to make it not appear "behind the scenes." Try adding this to your code:

    Documents.Add Template:=Options.DefaultFilePath(wdUserTemplatesPath) & "\flolaborderformtemplate.DOT", newtemplate:=False
    Joanne

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Revised Coding

    Hi Julie,
    I read that you're new to this, and looking at the form code, I think it's a bit daunting, even for an "expert". I've taken the liberty to mess around with it, to make it a bit more user friendly, if figures etc. need changing.
    The checkboxes haxe been renamed to Option 2 - Option11, and the Textboxes on the right renamed from Cash1 - Cash15, Indexes correspond to make things more transparent. Cost are declared as Constants within the code, so if you need to change a figure, only one change is required, similarly Shipping Costs, for which I have entered some sample figures. The Calculate button should calculate the totals, rather than the coded totals from the "if" functions. I've deleted the colouring etc., and anything else which seemed unnecessary, but it can always re reinstated if required. Please test thoroughly before use.
    Good luck!
    MD

  17. #17
    VBAX Regular
    Joined
    Aug 2004
    Posts
    13
    Location
    MD,

    I opened your file and am getting a "Run-time error '400':", "Form already displayed; can't show modally"...I like the calculate button..The colors were there to show where you were if you tab through the form, instead of clicking on which boxes you want with the mouse (easy enough for me to put back).

    I still can't figure out how to make just the macro open, without the document in the background. I assume I need to make the macro an executable? If so, does anyone know how to do this from Word? I tried compile but that doesn't seem to do anything (at least not anything noticeable or that I can find). I did make the document a template and tried Joanne's code. I don't know if I am putting the code in the wrong place or what I did wrong but I could not get it to work.

    Thank you again to everyone for your replies and suggestions. I really appreciate all the help you have offered :0)

    Julie

  18. #18
    VBAX Regular JOrzech's Avatar
    Joined
    Jun 2004
    Location
    Upstate New York
    Posts
    83
    Location
    Can you upload the file so we can look at it? I'm sure it can function as you request.
    Last edited by JOrzech; 08-31-2004 at 02:41 PM.
    Joanne

  19. #19
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Julie,
    I based my code on Joanne's posting and had assumed the "background" issues were resolved. I'd rather leave them to those assisting already .
    Re your error message, what version of Word are you running, and can you pin down the line giving you the error? If you need help to do this, please ask and I'll try to assist.
    MD

  20. #20
    VBAX Regular
    Joined
    Aug 2004
    Posts
    13
    Location
    Joanne,

    Thanks for everything! The .dot template is attached.

    Julie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •