Results 1 to 11 of 11
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting Lines of Code Error (VBA for Word 97/2000)

    I am using the following code to delete lines of code that I previously added:

    Public Sub RemoveShowMeButtonCode()
    Dim P As Integer
    Dim lngCodeLineCt As Integer
    Dim ButtonClickCodeLineCt As Integer
    lngCodeLineCt = 0
    GlobalTemplatePathFileName = "E:My DocumentsGoalsFormatDocuments.dot"
    Set GlobTemp = Templates(GlobalTemplatePathFileName)
    Set objCurProj = GlobTemp.VBProject
    Set FindListFormatModule = objCurProj.VBComponents("frmAnalyzeListFormat")
    With FindListFormatModule.CodeModule
    lngCodeLineCt = FindListFormatModule.CodeModule.CountOfLines
    ButtonClickCodeLineCt = lngCodeLineCt - 41
    If lngCodeLineCt > 41 Then
    FindListFormatModule.CodeModule.DeleteLines 1, Count:=ButtonClickCodeLineCt
    End If
    End With
    End Sub

    This code works perfectly if I run it manually (from the Visual Basic Editor in Word). HOWEVER, if I run it from the click event of the Cancel button on the form, it get this error:

    "Compile Error: Only comments may appear after End Sub, End Function, or End Property"

    This is the code that it is deleting. The green section is what is highlighted when the error box appears, not that it makes much sense:


    Public Sub MyCommandButton1_17_Click()
    P = 17
    FindListExample
    End Sub
    <font color=448800>Public Sub MyCommandButton1_16_Click</font color=448800>()
    P = 16
    FindListExample
    End Sub
    .
    .(See the progression from 17 to 1)
    .
    Public Sub MyCommandButton1_1_Click()
    P = 17
    FindListExample
    End Sub

    Are there better ways of doing this, like using Class Modules? Yes, but I haven't got time to learn that like I need to at this point. Everything about my code works beautifully except for this and I don't want (or have time) to reinvent what I have.

    Just occured to me that I should also put in my click event code:


    Private Sub cmbCancel_Click()
    If BulletsNumbersInTextAnalyzed = False Then
    RemoveShowMeButtonCode
    Erase ListFormatArray
    'Remove all labels and comboboxes that shouldn't be there.
    On Error Resume Next
    For M = 0 To frmAnalyzeListFormat.Controls.Count - 1
    On Error Resume Next
    frmAnalyzeListFormat.Controls.Remove (M)
    Next M
    End If
    frmAnalyzeListFormat.Hide
    End Sub

    Also, the following is the code I'm using to add the code:

    GlobalTemplatePathFileName = "E:My DocumentsGoalsFormatDocuments.dot" 'ActiveDocument.AttachedTemplate.FullName
    Set GlobTemp = Templates(GlobalTemplatePathFileName)
    Set objCurProj = GlobTemp.VBProject
    Set FindListFormatModule = objCurProj.VBComponents("frmAnalyzeListFormat")
    With FindListFormatModule.CodeModule
    .AddFromString _
    ("Public Sub MyCommandButton1_" & MyCommandButton1Number & "_Click" & vbCr _
    & "P = " & MyCommandButton1Number & vbCr _
    & "FindListExample" & vbCr _
    & "End Sub")
    End With


    Your help in getting me to overcome this particular error would be greatly appreciated. Thanks again!!
    Troy

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    Hi Troy,

    Re: using class modules - overall the class module solution is better than the adding code programatically method, but (1) not sure but I don't think you can use that particular kind of event procedure in Word 97, (2) you've already got this nearly finished so to be practical it's best to fix the one remaining glitch.

    If you have time later on, as a learning exercise it would be worth trying to figure out how you can do the same thing using a class module.

    A couple of quick questions about the current code:
    The syntax for the DeleteLines method is:

    Sub DeleteLines(StartLine As Long, [Count As Long = 1])

    - so the code you've posted currently is deleting code from the start of the module - is that correct? - i.e. was the programatically-added code added to the beginning of the module or the end?
    If the programatically-added code is being added to the end of the module, then you would need to set StartLine not to 1, but to 42.

    You're getting a compile error when the command button is clicked - the part that's hard to figure out is why the code would compile if run from the VBE, but not from the user interface (but you know that....).

    In the code snippet you posted, the following doesn't compile:

    .(See the progression from 17 to 1)

    -cause it doesn't have a comment mark in front of it - is this in your code like that, or is this something you typed in when posting the code?

    Gary

  3. #3
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    I'll answer your comments/questions in order:

    "Re: using class modules ..."

    I will learn when I have the time, but as you say this is my only glitch and I have to demo this on Monday.

    "so the code you've posted currently is deleting code from the start of the module "

    Yes. That is where it is naturally added using the code I am using, so I don't need the optional statement.

    "You're getting a compile error when the command button is clicked - the part that's hard to figure out is why the code would compile if run from the VBE, but not from the user interface (but you know that....).
    "


    Yep. That's the reason for the post.

    "In the code snippet you posted, the following doesn't compile:"

    Sorry I forgot to comment that out. I just put that in the post rather than put in all 17 instances of the code that was added in my test.

    Thanks for looking into this!!
    Troy

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    Hi again,

    It's hard to track down something like this, without having a sample of the whole deal to run, so the following is educated guessing (if you want to, e-mail me your template and I'll play around with it some more):

    Possibly the problem is that you're running a procedure that's in the frmAnalyzeListFormat code module, which calls another procedure which contains the instruction to delete the first x lines of code in the frmAnalyzeListFormat code module - that sounds very likely to throw some kind of runtime error.

    It's kind of like: when the cmdClick procedure starts, the compiler determines everything is OK, and makes note of which line of code in the form module, that it has to start running on. A couple of statements later, you're calling a procedure in a different module, which deletes the first x lines of code in the form module. When code execution then returns to the form's code module, the original "next" line of code is no longer in the position in the module that it was, before the x number of lines got deleted. At that point the compiler says "Out of here!"<g>)

    Going back to your first post, you say that the code runs perfectly when run from the VB Editor - do you mean you're running the RemoveShowMeButtonCode procedure by itself? - (which presumably is housed in a different code module i.e. not behind the userform). There's no reason for the code not to run OK from there, 'cause in that situation it's simply reaching out to code in another module and deleting it.

    So the tricky part is going to be: figuring out a way to delete the code associated with each commandbutton, while running a procedure that's in the same code module. Offhand though, I don't think there's any way to do that.

    Can you instead, just leave the code in there? And instead, have this code deleted at the start of your procedure which adds in new code, whenever you run that procedure? - that's what I had to do in my project:

    <pre>'Now add OnAction procedures for each control:
    Set modDynCode = objCurProj.VBComponents("modDynCode")
    With modDynCode.CodeModule
    'First, delete any pre-existing code in the code module:
    lngCodeLineCt = modDynCode.CodeModule.CountOfLines
    If lngCodeLineCt > 0 Then
    modDynCode.CodeModule.DeleteLines 1, Count:=lngCodeLineCt
    End If
    </pre>

    In the case of your project, you'd probably need to test for something like:

    If lngCodeLineCt > 41 Then

    Or even better, figure out how to leave some kind of unique flag in your programmatically-added code, that you can test for the presence of, whenever the add code procedure runs - if the flag is found, you know you've got to first run the delete existing code statements like the above.

    Hopefully this can be applied to your project - I think it's your best shot at this point.

    Gary

  5. #5
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    <P ID="edit" class=small>Edited by TroyWells on 16-Dec-01 14:28.</P>Gary, my friend, you are a genius!! Removing those lines (actually a reference to that procedure) before the code to add that code, and boom the procedure to remove the code works!! <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

    HOWEVER (don't you hate that word <img src=/S/devil.gif border=0 alt=devil width=15 height=15>), it also made it apparent that this was not the cause of my Compile error.

    I still get that same compile error when I click on the OK or Cancel button on the form. From what I can tell, it has nothing to do with the code in the click event for the buttons. In addition, the code for the click events does nothing when I click on the buttons.

    Does code that is programatically added have to be compiled or something?

    Do the Click event subs have to be "Private" like other event subs? I've tried adding code with that and I get an Automation error when trying to add the next for lines of code.

    Thanks again for the help!!
    Troy

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    Troy,

    Glad one more thing is solved, but sounds like you're still stuck. <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

    Click event code - oops, never noticed that in your code, but yes the command button click event code has to be a Private Sub (and has to be in the form's code module, which is where you have it).

    Not sure why you'd get an automation error when adding "Private Sub" as opposed to "Public Sub", unless you've got the form loaded at the time that you're adding the code - don't know whether you specified that in any previous posts - but it's not going to work if the form is loaded when you try to add the code.

    If the form isn't loaded when you add the code, and you're getting this error, then I'm stumped again....

    Gary

  7. #7
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    Nope the form is not loaded. I'm crying uncle!!

    I'd like to bite the bullet and learn about using class modules. I think I have a lot of the code written that would go into or be used by the class module, such as:

    - Code to create the command button.
    - Code to create the click event text for the command button.
    - Code to remove the click event code from the userform code module.

    I've even toyed with:

    CodeModule.CreateEventProc
    CodeModule.InsertLines
    (until I found an article that said that these were for working with class modules)

    So I've got a lot of the peices. I just need to know how to put them together, and what pieces need to go into the class module.

    Also keep in mind that I need to add the command button and click event code for "X" amount of times, where "X" is the upper bound of an array.

    Thanks again for your patience and help!!
    Troy

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    Oy!, Troy,

    You're not giving us the whole story - wasn't it all working, except for the delete code part? Has the cmdClick code not been working all along - that would have been relevant to know sooner.

    The offer still stands if you want to e-mail the project to me, and I'll try to have a look (click on my name to get my e-mail details) - it's hard to suss this out at arm's length.

    What's coming up in these threads are two issues, one is technical (how do I achieve this specific thing via code), the other relates to managing your project. The project management side of things doesn't get addressed much here, but it's probably more important for maintaining your professional sanity, than is technical skill alone.

    You've got a large project, using techniques you're never used before.
    First thing, make sure to give yourself lots of padding when estimating time to complete the project - in her book, Christine Solomon said if you think it will take you an hour, say you need a day; if you think it will take you a day, say you need a week, etc. If you don't even know how to accomplish the project when you take it on, you need even more lead time (or a contractor!).

    Second thing, if you come upon a new technique that may be the way to go for your project, don't just jump in and commit all your resources to going in that direction. First do a mini-project as proof of concept; if it succeeds then commit to going in that direction. Maybe the class module turns out to be the way to go (tho' probably won't work for '97), but you don't want to be finding that out, and trying to learn how to use class modules, the night before your project deadline!

    OK, lecture over. Too late to help this time, but maybe will help with your next project.
    If you want to e-mail the project to me, I'll have a look.

    Gary

  9. #9
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    Thanks for taking the time to look into this. In some ways I feel a little better that you couldn't get it to work either. This project was something I was doing on my own time as a personal objective for work. These objectives help determine raises, bonuses, etc., and should be something that improves myself and does something for the company. I had a little demo of it yesterday, which bombed on Word 97, but did well enough to impress the heck out of all present as we crowded around my laptop, which has Word 2000. We will eventually be moving to 2000 (both Office and Windows), but that move keeps getting postponed again, and again, and again.

    Thus, I wanted to give you a big THANKS!! for all the help that you especially, as well as others, provided.

    The work will continue, including looking into this class module thing, but without the time pressure I was under previously (thankfully!!). I will let you know what I find on the class module thing. I'm going to read up more on them, and will let you know if I get stuck. I think I'm just lacking a general high-level understanding of what class modules are (as opposed to standard modules), how they work differently, and what to include in them as opposed to
    standard modules in order to make this thing work.

    Thanks again for all your help, and if I don't post again before it, have a great Christmas!!
    Troy

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Deleting Lines of Code Error (VBA for Word 97/2000)

    Troy,

    Thanks for your nice note!

    Possibly you're not stuck yet - as noted in my e-mail, the problem is that you're adding controls (and the code behind them) to the userform, while the userform is loaded. It appears that you can add controls to a loaded userform, but you can't add private subs without making the application very unhappy.

    Have a look at this <A target="_blank" HREF=http://j-walk.com/ss/excel/tips/tip76.htm>page</A> (and download) from John Walkenbach's site - this is a demo project for creating forms, controls, and the code behind them, programatically.

    Based on a quick look, the key difference is that he's doing all of these things, before the form is loaded (makes sense!). And it definitely does work when the form is then loaded.

    I don't have a copy of your project here at the moment, so am going by memory, but this would probably involve:
    (1) show the userform, let the user choose what they want to see.
    (2) save their choices as variables.
    (3) unload the form, and run the code to programatically add the required controls and code behind them.
    (4) load the form again - the code behind the new controls should now run.

    It might look a little "blinky" to the user, but if the new buttons functioned, it would be worth it.

    Gary

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Button Class Demo (VBA for Word 2000)

    Hi Troy,

    Hope you're having a good Holiday.
    Attached is a simple demo for using a class module to create event procedures that will respond to clicks on buttons that are created at runtime.

    After getting the sample project you sent me to run once or twice, I can't get that to run anymore so am relying on memory as to what your userform looked like after the buttons are added a runtime.

    Anyway, whether that userform enables adding a variable or fixed number of new command buttons, this approach should work the same.

    A couple of key notes:

    (1) The code in the class module is not created dynamically i.e. at runtime; rather, you write all of it beforehand (aka 'design time') - as long as you know the maximum number of buttons that can be created at runtime, it's simple to create the necessary number of procedure stubs in the class module. The big benefit of doing it this way is that you can completely avoid the need for creating procedures at runtime which, as we've seen, is rife with problems (the J. Walkenbach demo does work, but I'm having trouble adapting it for your project - this may be because he creates the entire userform, all the controls and the code programatically at the same time, before the userform is actually loaded - whereas in your project you're attempting to add controls and code with the form already loaded). It may seem repetitive to have to add these beforehand, but it's very simple. For example, the code in the demo class module looks like this:

    <pre>Public WithEvents Button1 As CommandButton
    Public WithEvents Button2 As CommandButton
    Public WithEvents Button3 As CommandButton
    Public WithEvents Button4 As CommandButton
    Public WithEvents Button5 As CommandButton
    Private Sub Button1_Click()
    ShowInformation (Button1.Tag)
    End Sub
    Private Sub Button2_Click()
    ShowInformation (Button2.Tag)
    End Sub
    Private Sub Button3_Click()
    ShowInformation (Button3.Tag)
    End Sub
    Private Sub Button4_Click()
    ShowInformation (Button4.Tag)
    End Sub
    Private Sub Button5_Click()
    ShowInformation (Button5.Tag)
    End Sub
    </pre>

    (the demo permits you to add from 1-5 buttons at runtime).

    (2) The class module demo as posted by VBADude, is not usable for your project. That demo will only respond to the click event for a single command button which has been added at runtime. To respond to the click event for multiple command buttons added at runtime, you must have multiple WithEvents declarations and multiple event procedures in the class module. (VBADude's objection to the creation of multiple procedures to handle multiple buttons is spurious; the only real issues for debate in this case are whether the multiple procedures are put in a class module or in the form's code module, and whether the multiple procedures are created at design time or at runtime.)

    The attached demo illustrates what I think is the simplest way to do this (put procedures in a class module, created at design time).

    Hope this helps.

    Regards,
    Gary
    Attached Files Attached Files

Posting Permissions

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