Results 1 to 13 of 13
  1. #1
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Call a userform from another template

    Hello good coders,

    First post here, so treat me gentle please?
    Using this forum, I have been able to cobble together a routine that does version control on a document.
    I use a userform for the user to enter the new version number and description.

    I then save the document as a new document, with version number in the filename, and proceed to open the new file.
    Except that doesn't happen, because I have to close the old doc and the VBA script is closed with it.
    So I placed the userform and the code that controls it in the Normal.dot for now.

    But how do I call that DocControl userform?

    I have managed to reference the Normal.dot in the template I am using to create the docs.

    I have tried to use Load Normal.DocControl or Load Normal.Forms.DocControl.


    Code:
    Load Normal.DocControl
     
    With DocControl
            .Title.Text = Title
            .Subject.Text = Subject
            .Version.Text = VersionOrg
            .Show
            
        End With
    
    DocControl.Hide


    Nothing seems to work.


    A little more details
    I save the file as a docm, as I am capturing the Save action and ask the user if he wants to create a new version. So the macro's are running locally in the document itself. That is why it breaks when I close the document.
    The only way out, as I see it, is to place the controlling code in another template and let it run from there.

    Any help, any ideas?
    Thanks in advance.

    Lykle

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    667
    Thanks
    3
    Thanked 19 Times in 18 Posts
    Hi Lykle,

    I have a couple of questions (and I'll try to ask them as gently as possible ).

    When you display the form, which then alters the document, then saves the new version -- do you use Save As? If so, isn't the new document already on the screen? I guess this question boils down to why do you have to open the new file?

    If you were to post your code, we would probably be able to give you fairly specific and practical suggestions.

    I've passed control from one template to another. It's certainly possible, but you may not have to. Do you want the versioning code to only work on this specific type of document? Or would you like it to be available to all documents?

    FYI, if you want to experiment with passing control to code in another template, create a Public Sub routine in Normal that opens the userform. Then call that macro, don't try to call the form itself. You can do any other tasks (like closing one document and opening another) from your code in Normal.

    Best, Kim

  4. The Following User Says Thank You to kmurdock For This Useful Post:

    lykle (2012-07-14)

  5. #3
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello Kim,

    Thanks you for your reply.
    It was a smack on the forehead. I know you were gentle but I just didn't see it.
    Now I do and that makes it a lot easier.

    I don't know why I tried to close and open the doc, I did use SaveAs, so there is no need to open the new doc, it already is.

    As to the passing control, I was just thinking about doing it as you describe, so thank you for clearing that up. Although I don't need it anymore.

    There is still some logic problems, getting the dialog box at the wrong moment, but I will write out a flow and correct it.

    So at the moment I have all the code I need to create the version control in the same template that I use for the structure and layout of the document. This means I have to save it as a .docm to make it work, right? Because I was hoping I could save it as a .docx and run the code from the originating template. But because I catch the Save action, I need to run code locally, so I don't think it is possible.

    Post the code? When I get this to run the way I want it to run I think I will post the template, if anybody is interested.
    It is my first attempt at coding anything in VBA, so it will probably be cr*p, but whoever wants it, can have it.
    And hopefully will be gentle in fixing all my errors. :-)

    Once again, thank you Kim, for setting me straight.

    Lykle

  6. #4
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    667
    Thanks
    3
    Thanked 19 Times in 18 Posts
    ...This means I have to save it as a .docm to make it work, right? Because I was hoping I could save it as a .docx and run the code from the originating template. But because I catch the Save action, I need to run code locally, so I don't think it is possible.
    No, you shouldn't have to save the document as a docm. When you create a document from a template, the document inherits a "reference" to the template. This means any macros and userforms in the template are always available to the document. The document does not have to have any macros in it. There's a huge disadvantage to creating a bunch of documents with the same code in each -- what if you need to tweak the code? You'd have to do it once times every .docm you've created!

    The workflow (as I understand it) might go something like this:

    • Create the template and save in your templates folder.
    • Using File > New > My Templates, create a new document based on the template (this new document now has a reference to the template).
    • Save this original document as a .docx -- because of the reference, your macro code is still available to it.
    • You edit your document and now want to create a new version, so you run your macro and Save As.
    • Your new document also has a reference to the template, and can still be saved as a .docx.


    The document holds onto this reference through thick and thin. However, if you move the document to a computer that doesn't have a copy of your template, then the macros will not be available and the document will attach itself to Normal.dotm as a temporary reference. As soon as you bring it back to a computer with your template, the document finds it and the macros will once again work.

    So, if you need the macros to move around with the document, you need to save as a .docm. If you don't, then you'll be doing yourself a favor by simply creating .docx files.

    Unless there's something I don't understand about your intended use?

    Good luck with your code!
    Kim

  7. #5
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello Kim,

    Thanks, I knew that must be the way it should work.
    Anyway, I am still having trouble with the Save As part.

    Here is the Code:

    Code:
    If DocStatus = "NewDoc" Then
    
        Dim sFileName As String, fDialog As FileDialog, ret As Long
        Set fDialog = Application.FileDialog(msoFileDialogSaveAs)
        ret = fDialog.Show
        If ret <> 0 Then
            sFileName = fDialog.SelectedItems(1)
            ActiveDocument.SaveAs sFileName
            DocStatus = "CurrentDoc"
        End If
        Set fDialog = Nothing
      
        
    
    Else
        Dim myPath As String
        Dim myName As String
        Dim SaveAsFileName  As String
        
        myName = ActiveDocument.Name
    
        myPath = ActiveDocument.Path
        MyLen = Len(myName)
        MyPos = InStr(1, myName, "Version", 1) - 1
         
        If MyPos = -1 Then
            MyPos = InStr(1, myName, ".docx", 1)
        End If
            
        MyStr = Left(myName, MyLen - (MyLen - MyPos))
              
        SaveAsFileName = MyStr & "Version " & Version & ".docx"
        ActiveDocument.SaveAs myPath & "\" & SaveAsFileName
      
    End If
    What happens is this. The first time I run the code, the status is NewDoc so it runs the first option of the If then.
    That works, it files it as a new document with the file name I set using the dialog box.
    When I open that document again, and run the code again, it jumps to the Else bit and generates the new file name. It saves it as that file, but the original version remains open. So it saves version 2, but version 1 is still my active document.

    Now, that I don't understand. The command ActiveDocument.SaveAs is the same in both cases. So Why does it work in the If clause and not in the Else clause?

    I am really appreciating the help I am getting.

    Kind regards

    Lykle

  8. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,514
    Thanks
    3
    Thanked 143 Times in 136 Posts
    Add a line before the SaveAs to check the value of
    SaveAsFileName

    Debug.print
    SaveAsFileName

    Your code is not showing the value of Version, nor is it incrementing it as an integer. Are you sure the code is actually saving as a new file name rather than overwriting the existing file?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  9. #7
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hmm, I think I found the problem, but do not know how to solve it.

    This is the code I have in a Class, it captures the Save function.


    Code:
    Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
     On Error Resume Next
     Dim intResponse As Integer
     
     If DocStatus <> "procDoc" Then
     
     intResponse = MsgBox("Do you want to create a new Version?", vbYesNo, "Version Control")
     
     If intResponse = vbYes Then
            Call Properties("CurrentDoc")
        Else
            ActiveDocument.Save
        End If
    End If
    While in Debug mode, I step through the code and I see the new SaveAs document is active. But then when I exit the Subs and come back to the calling routine above, it flips back to the previous version.

    So in the subs, where I do a SaveAs to say Version2, I can see the filename as being version 2 and then when I step into the above sub, I see the file name is back to Version 1, as is the file.

    Hmm, maybe I should do the file save as in the above routine and not in the subs. Only use the subs to define the new file name. I will try.

    -----------------------------------------------------------------
    Well, no it didn't work.
    The moment I step out of the last End If and do the End Sub, it flips back to Version 1.

    I just can't seem to step out of this loop.
    Last edited by lykle; 2012-07-15 at 03:20. Reason: tried it, didn't work

  10. #8
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Andrew,

    Yes I have checked the filename and it is correct.
    The Version is a CustomDocProperty that I set before and is filled in correctly.

    It's just that the last call is from the Class that I use to capture the Save function. And that is still in the Version 1 doc.

    Is there a way to exit from all the Subs? Just stop processing any more lines?
    Not good programming I know, but it would solve my problem.

    Lykle

  11. #9
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    The template itself.

    Hello All,

    This is so frustrating, being so close but not there yet.

    Just to make it easier, here is the file that I am trying to get to work. Version document.docm
    It is a docm file but should be a dotm file, but I was not allowed to upload that.

    As I said before, this is the first time ever that I am programming in VBA, so the code probably looks pretty bad to your standards.

    Anyway, I really appreciate all the help I have been getting, so thank you.

    Kind regards

    Lykle

  12. #10
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    667
    Thanks
    3
    Thanked 19 Times in 18 Posts
    Lykle,

    In answer to your question about the document staying open, instead of this code (the second to the last line of the first code you posted):
    Code:
     ActiveDocument.SaveAs myPath & "\" & SaveAsFileName
    ...use:
    Code:
    ActiveDocument.SaveAs2 myPath & "\" & SaveAsFileName
    This replaces the document on screen with the new document and you shouldn't have to close the current document.

    Best, Kim

  13. #11
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello Kim,

    Thank you for your suggestion.
    I tried it but that causes Word to crash.

    I am sure it is because of the Class that catches the Save action.
    That is still running. It is there that Word crashes.


    Code:
    Public WithEvents AppWord As Word.Application (Declaration)
    
     
    Private Sub appWord_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
     On Error Resume Next
     Dim intResponse As Integer
     
     If DocStatus <> "procDoc" Then
     
        intResponse = MsgBox("Do you want to create a new Version?", vbYesNo, "Version Control")
        
        If intResponse = vbYes Then
            Call Properties("CurrentDoc")
        Else
            ActiveDocument.Save
                    
        End If
        
    End If
    In the mean time I am trying to clean up the code and simplify.

    I really like these challenges, but this one is getting to me a bit.

    Lykle

  14. #12
    New Lounger
    Join Date
    Jul 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello good people,

    Well, I got it to work, but not as nice.
    It is a bit of a sideways jump, because I can think of no other way.

    I still capture the Save action, but just ask if the want to make a new version and if the answer is Yes, would they please be so kind as to click on the "Save Version" button.

    And that works like a charm, but it is a bit of a work around.

    Now I will clean it up and post it here for anybody who wants it.
    Not that it is groundbreaking stuff, but who knows.

    Kind regards

    Lykle

  15. #13
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    667
    Thanks
    3
    Thanked 19 Times in 18 Posts
    Ah, I see what you're doing. I wasn't constrained by that code when I tested FileSaveAs2.

    I've attached an alternate way to control Save, and that's to name a macro in your template for the Save command. You'll see what I've done in the attached template (zipped). It's based on naming a macro for the FileSave command, so every time someone presses Ctrl+S or clicks File>Save, the macros kicks in -- and only for documents based on this template.

    To also capture the FileSaveAs command (F12 or File>SaveAs), you can create a sub named FileSaveAs and redirect to FileSave. Or you can assume someone wants to Save As a new version and start them down that path.

    Best, Kim
    Attached Files Attached Files

  16. The Following User Says Thank You to kmurdock For This Useful Post:

    lykle (2012-07-17)

Posting Permissions

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