Page 1 of 3 123 LastLast
Results 1 to 15 of 41

Thread: VBA (Word 2000)

  1. #1
    kstaker
    Guest

    VBA (Word 2000)

    I posted a question in the Word Forum, but now I think I need more VBA assistance.
    I have a form I created and basically I have a code that will allow me to have an invoice number that increments by one everytime someone opens the form. I used the following script:

    Sub AutoNew ()

    OrderNew = System.PrivateProfileString ("C:Settingsnew.txt", MacroSettings", "OrderNew")

    If OrderNew = "" Then
    OrderNew = 70000
    Else
    OrderNew = OrderNew + 1
    End If

    System.PrivateProfileString ("C:Settingsnew.txt", "MacroSettings", "OrderNew") = OrderNew

    ActiveDocument.Bookmarks ("OrderNew"). Range.InsertBefore Format (OrderNew, "00#")
    ActiveDocument.SaveAs FileName:="path" & Format (OrderNew, "00#")

    End Sub

    My bookmark was OrderNew


    I used this and it worked. My invoice started at 70,000 and went up 1 everytime the form was opened. The problem that I am having is that I cannot protect my form. What should I do. I have attached one of the forms and there are two I am building, but if I have the code I can modify it to the other.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA (Word 2000)

    Your template doesn't need to be protected. Instead, protect the document created from it at the end of your code:

    ...
    ActiveDocument.Bookmarks ("OrderNew"). Range.InsertBefore Format (OrderNew, "00#")
    ActiveDocument.SaveAs FileName:="path" & Format (OrderNew, "00#")
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

    You can probably omit the NoReset:=True; it comes in handy if you set the value of some form fields beforehand. Without the NoReset:=True they would be reset when you protect the document.

  3. #3
    kstaker
    Guest

    Re: VBA (Word 2000)

    That worked PERFECT!

    I have one more Dilemma, I need a code that will allow me to make a mirror copy of a form on the same page. My bookmark is called StartCopy and I have attached the document for ideas. Thanks KIM

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA (Word 2000)

    Kim, try

    Selection.WholeStory
    Selection.Copy
    Selection.Collapse wdCollapseEnd
    Selection.TypeParagraph
    Selection.Paste

    Note: to add more space between the original and the copy, you can increase the SpaceBefore of the paragraph created in Selection.TypeParagraph, or by adding more Selection.TypeParagraph instructions.

    Regards,
    Hans

  5. #5
    kstaker
    Guest

    Re: VBA (Word 2000)

    Unless I did it wrong it did not work. Have any other ideas or more instructions. [img]/forums/images/smilies/smile.gif[/img]

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA (Word 2000)

    I have attached the document with the modified StartCopy macro. What happens if you run it?
    Attached Files Attached Files

  7. #7
    kstaker
    Guest

    Re: VBA (Word 2000)

    Nothing happens, the number comes in which it should but I don't get the mirrored copy of the check on the bottom half of the page like I need.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA (Word 2000)

    I'm sorry, I just adapted the StartCopy macro and had assumed that you would run it manually to test it.

    I have built the code into AutoNew now. If that doesn't work, it might be due to a difference between Word 97 (which I am using) and Word 2000. So I won't be able to help you there.
    Attached Files Attached Files

  9. #9
    kstaker
    Guest

    Re: VBA (Word 2000)

    Okay, That worked great. Thank you for all your help.

  10. #10
    kstaker
    Guest

    Re: VBA (Word 2000)

    You have been such a big help and we released the form out into the office and of course now I get stuff back.

    The one thing I am trying to figure out is yes I got it to copy, but it copies before the information is entered into the fields. I need it to copy when I print it or after or as I enter the information in the top I need a carbon copy on the bottom. I tried putting in in as a macro upon exit but that only works when you exit. Let me know if you have any more ideas. Thanks Kim

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA (Word 2000)

    Kim,

    I can think of two approaches:

    (1) Create a custom toolbar button or menu item that runs a macro to copy the form. The user must use the button or menu item after filling in the form.

    (2) Set up the template so that the copy is filled in as the user enters information. This is an awful lot of work, but you have to do it only once. It has the advantage that if the user changes something in the original, the change is reflected in the copy.

    Copy and paste the existing form in the template manually.

    First, we're going to fix the text form fields
    1. <LI>Double click the first text form field in the original form.
      <LI>Enter a name in the Bookmark box. Remember this name.
      <LI>Check the "Calculate on exit" check box.
      <LI>Click OK.
      <LI>Go to the corresponding location in the copy of the form.
      <LI>Delete the text form field there.
      <LI>Select Insert/Cross-reference...
      <LI>In the first dropdown list, select Bookmark; in the second, select Bookmark Text.
      <LI>In the list of bookmarks, select the name you just assigned to the form field.
      <LI>Uncheck the "Insert as hyperlink" check box.
      <LI>Click Insert.
      <LI>Repeat the above for all text form fields in the original form.
    Next, we're going to fix the check box form fields. We're going to need macros for this, so enter the Visual Basic Editor (Alt+F11) and make sure that you have a standard module open (if you have no standard module, create one by selecting Insert/Module. Keep this module open and switch back to Word.

    1. <LI>Double click the first check box form field in the original form.
      <LI>Enter a name in the Bookmark box, for instance Check1. Remember this name.
      <LI>Click OK.
      <LI>Go to the corresponding check box form field in the copy of the form.
      <LI>Double click it.
      <LI>Enter a name in the Bookmark box, for instance CheckCopy1. Remember this name too.
      <LI>Switch to the Visual Basic editor and create a macro. The names must be those of the bookmarks in step 2 and 6.

      Sub CopyCheck1
      ActiveDocument.FormFields("CheckCopy1").CheckBox.V alue = _
      ActiveDocument.FormFields("Check1").CheckBox.Value
      End Sub

      <LI>Switch back to Word
      <LI>Go back to the first check box on the original form.
      <LI>Double click it.
      <LI>In the Exit Field dropdown list, select the name of the macro you just created (CopyCheck1 in the example).
      <LI>Repeat the above for all check box form fields in the original form, substituting the appropriate names of course.

    Don't forget to remove the instructions from the AutoNew macro that copy the form. They are not needed any more.

    HTH,
    Hans

  12. #12
    kstaker
    Guest

    Re: VBA (Word 2000)

    So I am glad I did not decide to be a VB programer by trade cause I would go crazy. I opted to go with the longer second half of the solution and let me tell you the text fields work great. But those darn little check boxes are giving me fits. I used the macro that you stated and when you check on the other one won't check then if you check another one of the ones on the bottom checks. I have attached the form. And if you then decide to go an uncheck a box it won't do the same at the bottom. Let me know what you think. Thanks So Much FOR ALL YOUR HELP!!!

  13. #13
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: VBA (Word 2000)

    Kim

    If you have rolled this template out to the users in your office have you noticed anything funny in the numbering when forms come back in. I am expecting that each user will give you an incrementing number on each form they submit but you will end up with a #70,001 from every user.

    Is that what you wanted to happen? Or is each form meant to come back with a unique number?

    You will need to store the incrementing number somewhere other than a private registry if you need unique numbers.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA (Word 2000)

    Kim,

    In the first place, the CopyCheck1 etc. macros as you wrote them just turn both check boxes ON. I have posted the correct code below.
    In the second place, you haven't set the On Exit macro for Check2, Check3 and Check4, only for Check1. You must do it for each check box individually, else the macros won't be executed.

    Sub CopyCheck1()
    ActiveDocument.FormFields("CheckCopy1").CheckBox.V alue = _
    ActiveDocument.FormFields("Check1").CheckBox.Value
    End Sub

    Sub CopyCheck2()
    ActiveDocument.FormFields("CheckCopy2").CheckBox.V alue = _
    ActiveDocument.FormFields("Check2").CheckBox.Value
    End Sub

    Sub CopyCheck3()
    ActiveDocument.FormFields("CheckCopy3").CheckBox.V alue = _
    ActiveDocument.FormFields("Check3").CheckBox.Value
    End Sub

    Sub CopyCheck4()
    ActiveDocument.FormFields("CheckCopy4").CheckBox.V alue = _
    ActiveDocument.FormFields("Check4").CheckBox.Value
    End Sub

    HTH,
    Hans

  15. #15
    Star Lounger
    Join Date
    Aug 2002
    Location
    Orlando, Florida, USA
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA (Word 2000)

    What you said would happen did and it is not what i wanted. I need it to have a unique number. How can I fix this?

Page 1 of 3 123 LastLast

Posting Permissions

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