Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Batch-create macro-enabled docs and insert VBA module

    Hi all,

    I have a lot of VBA modules that I have curated and need to test and adapt.

    I was wondering if any one knew of a method where I can create a .docm file for each module.

    Example

    FormatParagraph.bas ----- > Create Format Paragraphs.Docm and Insert the Module FormatParagraph.bas

    I did record a macro to see the process - but nowhere did it show the import of the module.

    I do appreciate this may be a very complex task - I have not found anything anywhere.


    I also don't mind importing them all into 1 .docm and then splitting them into individual files - again seems very complex.

    I thought I would throw it out there in case any one has heard of anything similar.

    Thank you

    pb

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    P.B.,

    Am I reading your post correctly that you have a series of .bas files each containing a VBA module?

    If so I don't understand why you want to create a .docm file for each when they can be directly imported into any .docm file where you want to use them.

    Perhaps I am missing the point? Would you care to elaborate.

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Sure, one could use a macro to loop through all .bas files in a folder, create a document with the same filename and import the .bas module into the document's VBE, but why? Surely you'd do better to import the modules into documents as & when you need them??? Do note that doing what you suggest would also require giving trusted access to the VBE, which is generally not recommended due to the security risks it exposes you to.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #4
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    Hello RG & Paul,

    I have been doing the standard importing them as and when I need.

    The problem is - I then end up making a number of versions of the same VBA Macro - when I adapt it.

    To make life easier and sane for my self - I thought I may just have - 1 docm file per vba macro - then I can duplicate the file - rename it to the version I am working on - and throw away the versions I don't need. The ones that were duds - so to speak

    So

    1 docm file -----> Contains only 1 .bas vba module

    example

    Format Paragraphs.docm ONLY contains formatparagraphs.bas

    Table Resize.docm ONLY contains tableresize.bas

    I am doing a lot of testing and adapting and going a bit potty misplacing modules - and versions

    Paul you have tempted me with the idea - so please do point me in the direction

    Thank you so much

    pb

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub CreateModuleDocuments()
    Application.ScreenUpdating = False
    ' Note: Requires a reference to Microsoft Visual Basic For Applications Extensibility
    ' and the user running the code must grant access to VBA object model beforehand
    '(see http://support.microsoft.com/kb/282830).
    Dim strFolder As String, strFile As String, wdDoc As Document
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    Set wdDocTmp = Documents.Add
    strFile = Dir(strFolder & "\*.bas", vbNormal)
    While strFile <> ""
      i = 0
        Set wdDoc = Documents.Add
        With wdDoc
        On Error Resume Next ' ignores any errors if the project is protected
          .VBProject.VBComponents.Import strFolder & "\" & strFile ' inserts component from file
          On Error GoTo 0
          .SaveAs2 FileName:=strFolder & "\" & Split(strFile, ".bas")(0) & ".docm", _
            FileFormat:=wdFormatXMLDocumentMacroEnabled, AddToRecentFiles:=False
          .Close SaveChanges:=True
        End With
      End If
      strFile = Dir()
    Wend
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    End Sub
     
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. The Following 2 Users Say Thank You to macropod For This Useful Post:

    Charles Kenyon (2016-02-04),pb89 (2016-02-01)

  7. #6
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Smile

    Paul,

    this is outstanding - thank you so much.

    For us newbie learners - a good workflow is important, and managing all the modules is very difficult. We have to go through 10 versions of the same macro to get it working - aah growing pains.

    I used to also store macros within modules but then I would forget they were there and have name conflicts - I am particular with my naming convention - hence they all sound the same

    May I say this is an extremely useful module - an essential.

    All I have to do is run this and all my files are ready for testing

    I searched all over - couldn't find a thing

    Thanks Paul -Top Man

    Really made my Monday

    PB

    Solved Thank you

Posting Permissions

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