+ Reply to Thread
Results 1 to 8 of 8

Thread: How to copy VB code from Source File to Target File programmatically?

  1. #1
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Question How to copy VB code from Source File to Target File programmatically?

    This is for MSWORD 2003!!!!!

    1) I'm trying to automate updating of VB Projects from a Revised SOURCE document into multiple TARGET document(s); one document at a time.
    2) I used http://www.cpearson.com/excel/vbe.aspx (Copy A Module From One Project To Another) as starting code.
    3) Everything works OK, except for the "ThisDocument" module. For this module, you can't import the code directly into it, since it can't be deleted. Therefore, you have to insert code into a TEMP module, then delete all the lines from the target "ThisDocument" module, then either .InsertLines or .AddFromFile to copy code lines from the TEMP module into the now empty target "ThisDocument" module.
    4) Word crashes, though, when either of these two instructions are executed. From internet, got the idea that when code is added from the top down, that it sees "Sub ABC()" w/o the "End Sub" causing a compile error which causes the crash. So, I tried the idea of copying the lines (one line at a time, in a For ... Next loop) from the BOTTOM UP!
    5) But, it still crashes. If I step thru it one line at a time, I find that it crashes when it comes to a line that has the COTINUATION DASH (" _") at the end of the line. This causes a compile error and a crash.
    6) In the VB Editor, if I go to Tools / Options / Editor and UNCHECK the "Auto Syntax check" box, AND Tools / Options / General and UNCHECK the "Compile on Demand" box, then it DOESN'T crash.
    But, I'd rather avoid this special steps, if there is some other trick out there to make this work. ... Any ideas on better ways to do this????? ... Thanks, sauerj
    Last edited by sauerj; 02-14-2011 at 03:20 PM.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,648

    Re: How to copy code to ThisDocument module programmatically w/o crashing Word?

    Why not create a Template that contains the code instead?
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Re: How to copy code to ThisDocument module programmatically w/o crashing Word?

    I guess I don't know exactly how to use a template for this purpose.
    1) Suppose I have ~500 existing document on a business network. These are Word narrative documents (10-200 pages long) that describe in detail to a PLC programmer how to program the PLC for operational code. Each of these ~500 documents contain about 15 modules each with ~50-500 lines of code each. These macros help the narrative writer format the document as well as provide power editing & writing tools.
    2) Then, say someone adds a new macro or modifies an existing macro in one of these narratives that enhances its arsenal of narrative automation tools. It would be desired then, to copy the entire VB Project from this SOURCE document to the majority of the ~500 existing "target" documents.
    3) These existing documents are dynamic documents that engineers change for on-going programming changes. It is desired to keep the mass of these ~500 document up-to-date with the latest VB code tools. Also, engineers typically sponge off an existing similar narrative (similar in operational location, similar in content) when building a NEW narrative. They would not like to start with a blank template. So, it is desireable that each of the ~500 documents have the latest VB code so that as new documents are spawned, they get the latest code too.
    4) How does having a template with the latest code enable spreading that code out to a multitude of existing documents? That's where I'm confused. ... Regards, sauerj
    PS: I've got a new fix-it idea; going to work on this idea today. If it works, then I'll mark this thread as RESOLVED. Got my fingers crossed!

  4. #4
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Smile Re: How to copy code to ThisDocument module programmatically w/o crashing Word?

    I was able to solve this myself. Solution deletes all modules from target file & wipes the ThisDocument module clean. Then, imports all modules from source file into target file & copies the lines of code from the source ThisDocument to the target ThisDocument. Then, asks if you want to do the same to another target file.
    A lot faster than manually using the Organizer built-in tool, and then manually copy/paste over the ThisDocument code (which the Organizer doesn't do). I used it today to update ~20 documents (repeating the process 3 separate times), so gave it a good acid test. ... I like it!
    Current solution changes ONE Target document at a time. It wouldn't be too difficult to change this so to change MULTI-Selected Target documents (in a common folder) in one swoop, which would be even better!!! Possible weekend plans.

    When I try to e-mail the solution (which is a Doc file containing the VB code) from within the business environment to the outside, the business firewall blocks it thinking it is a virus. So, I won't post solution here, as may lead to bad things on this forum.
    If anyone wants solution and AFTER getting moderator's OK, then I will post. ... Regards, sauerj
    Last edited by sauerj; 02-10-2011 at 06:38 PM.

  5. #5
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Smile Re: How to copy VB code from Source File to Target File programmatically?

    FYI ...
    To whom may be interested. I was able to develop a Word doc that acts as a utility tool. It takes all modules from a selected Source File and copis these to selected Target File(s), replacing whatever was in the previous Target File VB Project with what is in the Source File. ... Let me know if interested in receiving. To post w/ this attachment will require forum moderator to approve posting file as virus scanners may see it as bad. ... sauerj

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: How to copy VB code from Source File to Target File programmatically?

    In your I would have use a Word -template containing all macros.
    E:\OF\example.dot

    For each user I would have made a document, based on this template and saved.
      with documents.add "E:\OF\example.dot"
        .saveas "E:\OF\" & username & ".doc"
        .close 0
      end with
    I would then have sent each user a macro to 'open' that document:

    sub start()
      documents.add "E:\OF\" & username & ".doc"
    End sub
    All macros in the template will be available for all users.
    If some macros change in the template all users will benefit. The only thing you have to do is adapt the template. (most efficiently done by adapting a copy of the tmeplate and when finished saving that under the templates name).



  7. #7
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Re: How to copy VB code from Source File to Target File programmatically?

    Pretty cool! ... Thanks for the tip. I think I now understand the benefit of using a template. Didn't realize that macros in a template would be globally available to any documents that sponge off of it. And, that changes to the template macros are dynamically available as updated macros to any document that starts from the template. ... I'll give it a try. Thanks again, sauer.

  8. #8
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Re: How to copy VB code from Source File to Target File programmatically?

    Thanks for the tip, and explaining how to set this up. With this explanation, I now understand how a template can be used for this "universal" use of VB code.

    However, got an issue. I took one of the narratives with the VB code imbedded in it and saved it as a *.dot file. Then opened the *.dot file as a NEW *.doc file. Saved that. Then, re-opened this *.doc file and the Document_Open FAULTED on the very first document manipulating command line "ActiveWindow.Split = False" which is a very simple, non-complicated command.
    When examining the VB code in the *.doc file, it IS clear that the VB code does actually reside in the *.dot file. And, if I change the code in the *.dot file, then open the *.doc file, the change DOES show up (like it should) in the *.doc file. That's a GOOD thing. BUT ...
    The only issue (and it is a BIG one) is that the code doesn't execute correctly, at least the code in the Document_Open procedure. Don't think I'm doing anything wrong; any ideas?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0