Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Apr 2002
    Location
    New South Wales, Australia
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Security & Digital signatures (Word 2000)

    Hi.

    I

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Security & Digital signatures (Word 2000)

    Is it important that they be stored as .doc files, or could you transparently convert them to RTF as you save them into the database?

  3. #3
    New Lounger
    Join Date
    Apr 2002
    Location
    New South Wales, Australia
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Security & Digital signatures (Word 2000)

    Jefferson,

    As far as I know, that has the same effect as stripping off macros manually- that it still has to be opened in Word to be saved as rtf, and in doing so, any autoexec macros are executed before they are stopped.

    There is a switch (/a) when starting Word form a shortcut which can stop these. Is there a way to invoke this switch when starting Word from VB?

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Security & Digital signatures (Word 2000)

    Oh, the concern is prior to storage in the DB.

    While you cannot use /a from VB directly, you could Shell a new instance of Word using that option and manipulate it "at arms length." At least in theory. I've tried many things to set a programmable object to a task (e.g., Task.Application, Task.Application.Document) and I cannot get at it. Very frustrating.

    If you can tolerate a significant loss of formatting, you can Insert the target file into a blank document. The AutoOpen macro does not run when you do this, but of course, the margins, headers and footers are not brought over either.

    Wait a second... isn't it possible to strip all code modules externally using the Organizer?

    Application.OrganizerDelete "dangerous.doc", ModuleName, wdOrganizerObjectProjectItems

    But how to learn the module names? Could I install it as an add-in and use the VBE library?

    Doesn't work; documents cannot be installed as add-ins. Hmmm...

    (hours later)

    Documents can be added as references, and code in referenced documents can be deleted using the VBIDE library. I haven't tested whether this works with the IDE closed, and it does involve a kludge, but it's a first step:

    <pre>Sub StripCodeViaVBE()
    ' Add Reference to target document; requires reference to VBIDE library
    Dim prjHome As VBProject, ref As Reference, vbc As VBComponent
    Set prjHome = VBE.ActiveVBProject
    Set ref = prjHome.References.AddFromFile("FullPathToFile")
    ' Examine VBComponents in new reference and delete code
    For Each vbc In VBE.VBProjects(ref.Name).VBComponents
    If vbc.Type = vbext_ct_Document Then 'Clear ThisDocument
    vbc.CodeModule.DeleteLines 1, vbc.CodeModule.CountOfLines
    ElseIf (vbc.Type = vbext_ct_StdModule) Or _
    (vbc.Type = vbext_ct_ClassModule) Then 'Clear Module
    vbc.CodeModule.DeleteLines 1, vbc.CodeModule.CountOfLines
    End If
    Next
    ' Save changes: SaveAs fails in a "hosted" VBA project
    ' VBE.VBProjects(ref.Name).SaveAs ref.FullPath
    ' But removing the ref prompts to save, so we click Y with SendKeys
    SendKeys "y"
    prjHome.References.Remove ref
    Set ref = Nothing
    Set prjHome = Nothing
    End Sub</pre>

    This leaves the empty modules in the document and triggers a macro warning upon opening. <sigh> It is possible to gather the module names and use OrganizerDelete (where I started), but this also does not "clear" the macro signature from the document. Nevertheless, it might be preferable to using the rather indirect save reference method:
    <pre>Sub StripCodeViaOrganizer()
    ' Add Reference to target document; requires reference to VBIDE library
    Dim prjHome As VBProject, ref As Reference, vbc As VBComponent
    Set prjHome = VBE.ActiveVBProject
    Set ref = prjHome.References.AddFromFile("FullPathToFile")
    ' Examine VBComponents in new reference and store the module names
    Dim intCounter As Integer, strArray(25) As String
    For intCounter = 1 To VBE.VBProjects(ref.Name).VBComponents.Count
    Set vbc = VBE.VBProjects(ref.Name).VBComponents(intCounter)
    If vbc.Type = vbext_ct_Document Then 'Inspect ThisDocument
    If vbc.CodeModule.CountOfLines > 0 Then 'Record Name
    strArray(intCounter) = vbc.Name
    End If
    ElseIf (vbc.Type = vbext_ct_StdModule) Or _
    (vbc.Type = vbext_ct_ClassModule) Then 'Record name
    strArray(intCounter) = vbc.Name
    End If
    Next
    ' Disconnect the document (closes it)
    prjHome.References.Remove ref
    Set ref = Nothing
    Set prjHome = Nothing
    ' Delete code modules
    With Application
    For intCounter = 1 To UBound(strArray)
    If Len(strArray(intCounter)) > 1 Then
    .OrganizerDelete "FullPathToFile", strArray(intCounter), _
    wdOrganizerObjectProjectItems
    End If
    Next
    End With
    End Sub</pre>

    I realize this is all a bit sloppy, but it's such unfamiliar territory! Hope it leads you to an elegant solution somehow.

    Important P.S.: Virtually all documents will have the Project name Project. If any of your global templates or add-ins also have this project name, adding the reference will fail due to duplicate names. Thus, you should give them unique names.

  5. #5
    New Lounger
    Join Date
    Apr 2002
    Location
    New South Wales, Australia
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Security & Digital signatures (Word 2000)

    Jefferson,

    That looks promising.

    I'll be checking it out first thing tomorrow at work. When my daughter is not pestering me for Internet access <img src=/S/grin.gif border=0 alt=grin width=15 height=15>.

    Aloof

  6. #6
    New Lounger
    Join Date
    Apr 2002
    Location
    New South Wales, Australia
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Security & Digital signatures (Word 2000)

    Jefferson,

    I

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Security & Digital signatures (Word 2000)

    On the name conflicts, see my "Important P.S.".

    The Types I handled did turn out to be the same, and they probably can be consolidated. I am, however, skipping UserForms and anything else I don't know how to handle, so I wouldn't take that out completely unless, of course, you want to remove them, too.

  8. #8
    New Lounger
    Join Date
    Apr 2002
    Location
    New South Wales, Australia
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Security & Digital signatures (Word 2000)

    Jefferson,

    Sorry, I was working off my email notification, which did not have the "Important PS". I guess you must have added that.

    The next error I get is in the "For" statement- "Can't perform operation since the project is protected". I can get rid of that message by opening up the document I am trying to reference- but that sort of defeats the object.

    Aloof

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Security & Digital signatures (Word 2000)

    Yeah, I tend to edit posts after seeing them. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>

    When I add a template as a reference I also get that error. I only tested with a document last night. I'm not sure why they behave differently...or what the next level of workaround is if you need to strip modules from templates.

  10. #10
    New Lounger
    Join Date
    Apr 2002
    Location
    New South Wales, Australia
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Security & Digital signatures (Word 2000)

    Jefferson,

    Those paths were dead-ends. And they looked so promising.

    A document behaves differently from a template because a reference to a document loads the template into the VBE. A reference to a template seems to load procedures available in the background- they cannot be viewed. That's the difference between the way they behave in code.

    But that aside- it doesn't work. When I reopen a document "cleaned" of macros, I get a message about macro storage not available.

    But I followed an earlier lead about shelling.

    Yes, I can shell Word with a switch like "/a" which disbale all macros. But the database application has its own macros running which cannot be disabled. But, what I can do is to use a switch to starft a macro of my own choosing using a switch "/mMyMacro".

    {I haven't experimented too much yet, but using " /m /mMyMacro" seems to work better. It starfts the macro I select, (its function is to delete all macros in to newly opened document), without running an AutoiExec macros. The extra "/m" switch I have to experiment with, to know if it works that way.}

    "MyMacro" cleans out any macros, and saves the document as a cleaned-out Word doc, ready to be loaded into the DB.

    The next step is to save the cleaned doc as a temp doc, and to open it using normal methods. There's no macros showing or executing.

    I do get an error if I try to edit a macro using the "Alt +F8" and selecting- the "deleted" macros show up in the list. However, in this environment, ANY command which can possibly be used to run/create macros has been disabled already, so I'm not worried.

    Thanks so much for your help. It pointed me in the right direction, even if it was a different direction from where we started to go earlier.

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Security & Digital signatures (Word 2000)

    I think the /mMyMacro method is good; it gets inside the new instance of Word directly so that you don't have to manipulate it externally (which seems quite difficult). I can't understand why your macro names are still listed if you are deleting them, but as you are much further down this path than I have time to go...I'm sure you'll tell me once you figure that out.

  12. #12
    New Lounger
    Join Date
    Apr 2002
    Location
    New South Wales, Australia
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Security & Digital signatures (Word 2000)

    Actually, we now have found a much cleaner way to remove the macros.

    The initiating process (VB in our case) initiates Word- but with a blank document.- but it starts a macro (in Normal.dot) which does an "Insert, File" into the blank document. This creates a copy of the file with complete formatting- but without the macros.

    We first shell to the Word with parameters " /mCreateFile" (which starts Word running the macro we have written) and "/fFilename" (this is a switch not used by Word, so we hijack it for our purposes).

    The code in Word looks like this:

    <pre>Declare Function GetCommandLine Lib "Kernel32" _
    Alias "GetCommandLineA" () As Long
    Declare Function lstrcpy Lib "Kernel32" _
    Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long

    Public Function CommandLine() As String

    Dim strBuffer As String
    Dim lngLen As Long
    Dim lpstrAddress As Long

    lngLen = 255
    strBuffer = String(lngLen, " ")

    lpstrAddress = GetCommandLine()

    lpstrAddress = lstrcpy(strBuffer, lpstrAddress)

    CommandLine = Trim(strBuffer)

    End Function

    Public Sub CreateFile
    Dim strFileName As String
    Dim blnReadOnly As Boolean
    Dim strCommandLine() As String
    Dim lngIndex As Long

    ' Get the command line used to initiate the Word session; split it off
    ' into individual parameters, delimited by a slash.
    strCommandLine = Split(SPEARWeb.CommandLine, "/")

    ' The file name is in characters 2+ of the parameter starting with "f"
    For lngIndex = 1 To UBound(strCommandLine)
    If Mid$(strCommandLine(lngIndex), 1, 1) = "f" Then
    strFileName = Mid$(strCommandLine(lngIndex), 2)
    Exit For
    End If
    Next

    ' Add a new document, as non has been created at this stage
    Documents.Add

    ' Insert the Word document into the blank document
    Selection.InsertFile FileName:=strFileName, Range:="",
    ConfirmConversions:=False, _
    Link:=False, Attachment:=False

    ' Save the document, overwriting the original
    ActiveDocument.SaveAs FileName:=strFileName, _
    FileFormat:=wdFormatDocument, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False,
    EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False,
    SaveAsAOCELetter:=False

    ' Position cursor at start of document
    Selection.HomeKey Unit:=wdStory</pre>

    End Sub

Posting Permissions

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