Consulting

Results 1 to 4 of 4

Thread: Userform appearing on Documents saved from template

  1. #1

    Userform appearing on Documents saved from template

    Good Morning,
    I suspect this may be quite simple but I'm trying to get my head round it through forums etc.. I have a Template that has userform for input on it, I hide the application so the users only see the Userform on launch of the template. Its basically a simple tool to print labels, They enter 7 fields worth of data and hit print, in the background I have the code save a copy of the label as a docx file.
    Unfortunately the docx file that is being created is still linking to the template, when the user open the saved documents (docx) it is still opening to the Userform and not the plain document with the label as I hoped.

    This is an extract of the code I use to Save the file: I have tried various FileFormats as well.
    strPath = "C:\Temp\"
    strFileName = "Pallet - " & ActiveDocument.Bookmarks("bkPallet").Range.Text & "_" & ActiveDocument.Bookmarks("bkShortDesc").Range.Text & ".docx"
    ActiveDocument.SaveAs2 FileName:=strPath & strFileName, FileFormat:=wdFormatDocumentDefault, SaveFormsData:=False
    I have looked at the ActiveDocument.Attachedtemplate = "\...\Normal.dot" and tried to change it to Normal.dot but the code keeps failing with 4198.

    I have found some other code that someone had kindly released but I cant get it to work, it doesn't seem to find any VB Project to remove?
    This was a bit over my head though, I'm just getting bits of code as I search through forums.

    Sub RemoveAllMacros(objDocument As Object)
        
    ' deletes all VBProject components from objDocument
    ' removes the code from built-in components that can't be deleted
    ' use like this: RemoveAllMacros ActiveWorkbook ' in Excel
    ' or like this: RemoveAllMacros ActiveDocument ' in Word
    ' requires a reference to the
    ' Microsoft Visual Basic for Applications Extensibility library
    Dim i As Long, l As Long
        If objDocument Is Nothing Then Exit Sub
        i = 0
        On Error Resume Next
        i = objDocument.VBProject.VBComponents.Count
        On Error GoTo 0
        If i < 1 Then ' no VBComponents or protected VBProject
            MsgBox "The VBProject in " & objDocument.Name & _
                " is protected or has no components!", _
                vbInformation, "Remove All Macros"
            Exit Sub
        End If
        With objDocument.VBProject
            For i = .VBComponents.Count To 1 Step -1
                On Error Resume Next
                .VBComponents.Remove .VBComponents(i)
                ' delete the component
                On Error GoTo 0
            Next i
        End With
        With objDocument.VBProject
            For i = .VBComponents.Count To 1 Step -1
                l = 1
                On Error Resume Next
                l = .VBComponents(i).CodeModule.CountOfLines
                .VBComponents(i).CodeModule.DeleteLines 1, l
                ' clear lines
                On Error GoTo 0
            Next i
        End With
    End Sub
    Any help would be very much appreciated.
    Mark

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    What is the name of your procedure that displays the userform? If it Document_Open or AutoOpen then you might want to replace Open with New.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Thank you Greg, Thats all it needed! I had userform launch in the Document_Open. Working perfectly now.
    Thanks again.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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