none
Help needed: Copy a Line from One Word Document to Other! RRS feed

  • Question

  • I have some experience with Excel VBA (to clarify about my VBA background) and I do not know much about Word VBA (almost zero knowledge). Here's the complete scenario.

    1. There are many departments and they prepare their own weekly report. Each report contains bulleted points under Headings like "Pending Items", "Planned Items" etc. Number of bulleted points vary.

    2. So using these individual reports, Main report is created which is more or less compilation of all reports. This is being done manually as of now.

    So is it possible to create the main report using word VBA like:
    1. All departmental reports will be collected in a folder [manually].
    2. When the macro in Main Report is run then it will loop through selected folder & all word docs.
    3. Each department Doc will be opened and the bulleted points under each tab will be copied and pasted under Dept. Heading in Main Report.

    This will save a lot of manhours and headache.
    Kindly advice me, if it is a feasible idea? At this point, I am trying to search Word VBA book.

    I have posted this on a forum of which I am a regular member (Excel). The thread and attachments can be found here.

    http://www.vbaexpress.com/forum/showthread.php?t=38693

    Thank you for looking into it. I hope to get good advice.

    Saturday, August 20, 2011 3:19 PM

Answers

  • Hi shrivallabha

    <<Maybe if you could provide me the outline of the approach then one of IT guys in the office can then fix it up.>>

    I did provide the outline of an approach in the questions of my last reply. Given what I know about your requirements, that's how I'd consider going about it. But I cannot know how the users in the organisation function and what constraints you can enforce. So only you can decide whether the suggestion is workable.

    Note that, in your case, what Mark suggests is not feasible in detail. A content control with rich text cannot be mapped to XML in the document. And rich text is what would be required for bullet points. Also, given the learning curve and the speed with which you want to realize this, a pure XML approach is not going to be practical, due to the amount you'd need to learn. Not only the basics of XML, but also the Open XML file formats AND VB.NET or C#.

    Books on Word VBA: they are indeed few and far between. The best one I know of in any language is in German (and I may be biased on the point because I'm one of the four authors!), from MS Press and is in its third edition. In English, all are sadly out-dated. Strictly speaking, the O'Reilly "Writing Word Macros" is probably the best reference work. If it's still available, the Wrox "Word 2000 VBA" probably has the best over-all discussion, which is needed in order to understand how the object model functions.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by shrivallabha Sunday, August 21, 2011 8:47 AM
    Sunday, August 21, 2011 7:51 AM
    Moderator

All replies

  • Ah, you mean "line of text"; I was looking for something about a drawn line (graphic). OK, I'm with you, now :-)

    A couple of questions back to you:

    1. Version of Word involved?

    2. Do you want to keep that bullet formatting, or not?

    3. Do you already have a way to identify the target point (Word.Range) in the Main Report?

    4. By "tab" I assume you mean "Heading". Do you already have a way to identify these headings?


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 3:49 PM
    Moderator
  • Ah, you mean "line of text"; I was looking for something about a drawn line (graphic). OK, I'm with you, now :-)

    A couple of questions back to you:

    1. Version of Word involved?

    2. Do you want to keep that bullet formatting, or not?

    3. Do you already have a way to identify the target point (Word.Range) in the Main Report?

    4. By "tab" I assume you mean "Heading". Do you already have a way to identify these headings?


    Cindy Meister, VSTO/Word MVP

    Thank you for looking into it.

    1. Version of the main report will (always) be 2007 but for other departments' reports could be 2003 (sometimes since we are in a process of changing over from 2003 to 2007). Operating system is Windows XP Professional.

    2. If it is possible (fine). Final target will be: each point shall be distinguished from the other (and hence it is bulleted).

    3. I do not have any word VBA background. So at this point, I have not figured that out. I am sorry.

    4. Yes, its heading. Like 3, I haven't figured this out. Sorry again.

    Is it possible to upload samples? Maybe that can explain my situation better.


    Regards, Shrivallabha
    • Edited by shrivallabha Saturday, August 20, 2011 4:03 PM Point 4 was incorrect
    Saturday, August 20, 2011 4:02 PM
  • Hi Shrivallabha

    You can only upload samples to a website, somewhere, and post a link. It's not possible to upload as part of a forum message.

    For my part, I think I can picture it well enough. Your answers to my questions basically tell me where I have to start with my explanations :-) Thank you for the detail.

    The mixture of 2003 and 2007 makes this somewhat difficult. If it were only 2007/2010 then I'd advise putting each section of bullet points into a rich text content control. That would structure things a bit and make them easier to pick up. But 2003 doesn't support content controls...

    So the next question: Can the users basically do "anything" when creating the individual reports, or do you (the company) provide some rules and structuring? Or even (VBA) tools?

    Could you, for example, provide a Template (*.dot file) as the basis for all these reports (including the Main Report). Define the styles the users should apply. One of those should be for these bullet points (and only used for these bullet points). Does that sound feasible?

    If yes, then, at a high level, what your VBA will do is search for all instances of that style and copy to the Main Report. (Just FYI, but it may not mean a lot to you at this point, the copying would be done with the Range.FormattedText property in the Word object model.)


    Cindy Meister, VSTO/Word MVP
    Saturday, August 20, 2011 4:40 PM
    Moderator
  • I'm reading Charles J. Goldfarb's XML handbook.  I got it from the library. I really recommend it.  XML provides a structured way to store text data. 

    Word lets you save an XML document inside a Word file.  The XML document can be linked to Content Controls in the document, so that when you change a content control fields, you change the XML data node that is mapped to that content control field--and when you change the XML data node, you change all the content control fields that are mapped to that node.  You can control this process with VBA. 

    You can hide all this XML stuff from the users, so that when they submit the report to you, they are also submitting the XML (so long as they use the required form).  You can then merge the XML docs together into one big XML doc which will be the data source for your main report.  All this can be automated. 

    An advantage of this is that you now have an XML data document that you can use in any other Office Document or in any future report.  XML, with Word, can work like a really big highly structured, searchable, merge file.

    There's a learning curve, but XML is really worth learning. 

     

    Saturday, August 20, 2011 6:03 PM
  • Hi Cindy,

    Your replies have been very promising. Following are the links to the sample.

    http://uploading.com/files/37ac6e83/Sample_Main%2BReport.docx/

    http://uploading.com/files/36d3683f/Sample_Weekly%2BReport.doc/

    Word VBA (let alone VBA) is not part of my job. But I do it as it saves time. This is offhand request by a Project Manager who knows that I do this kind of stuff on Excel. So he imagined it'd be similar. If 2007 only makes it better approach then I can let the PM know. He will take it up and might be able to persuade all users to use 2007.

    Maybe if you could provide me the outline of the approach then one of IT guys in the office can then fix it up.

    ------------------------------------------------------------------------------------------------------------------------------

    Hi MarkvonW,

    Thank you. I will certainly take a look into this. My first (almost always) reaction was to search for a book in local store but the attempt came a cropper. There are a lot of books available on Excel VBA but the same can't be said about word VBA, locally in Mumbai (India). So the next attempt was my regular VBA forum, but the post remains unanswered. I'll accept that 3 days is not a long waiting period but you normally get a reply within first 2-3 hours of posting and that didn't happen. I'll try to fit XML in my studying plans but it could be too long.

    ------------------------------------------------------------------------------------------------------------------------------

    Thank you both, Cindy And MarkvonW


    Regards, Shrivallabha


    Sunday, August 21, 2011 6:21 AM
  • Hi shrivallabha

    <<Maybe if you could provide me the outline of the approach then one of IT guys in the office can then fix it up.>>

    I did provide the outline of an approach in the questions of my last reply. Given what I know about your requirements, that's how I'd consider going about it. But I cannot know how the users in the organisation function and what constraints you can enforce. So only you can decide whether the suggestion is workable.

    Note that, in your case, what Mark suggests is not feasible in detail. A content control with rich text cannot be mapped to XML in the document. And rich text is what would be required for bullet points. Also, given the learning curve and the speed with which you want to realize this, a pure XML approach is not going to be practical, due to the amount you'd need to learn. Not only the basics of XML, but also the Open XML file formats AND VB.NET or C#.

    Books on Word VBA: they are indeed few and far between. The best one I know of in any language is in German (and I may be biased on the point because I'm one of the four authors!), from MS Press and is in its third edition. In English, all are sadly out-dated. Strictly speaking, the O'Reilly "Writing Word Macros" is probably the best reference work. If it's still available, the Wrox "Word 2000 VBA" probably has the best over-all discussion, which is needed in order to understand how the object model functions.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by shrivallabha Sunday, August 21, 2011 8:47 AM
    Sunday, August 21, 2011 7:51 AM
    Moderator
  • Hi Cindy,

    Thanks for the guidance. This should be it then. I'll make a brief outline based on this conversation(the only problem that I see is my lack of knowledge in this area) and send it to Project Manager.

    It'll help if they have not figured out a way and looking for a guideline. Thank you for your time (even on weekends!)


    Regards, Shrivallabha
    Sunday, August 21, 2011 8:47 AM
  • Hi Shrivallabha

    OK, glad this gave you a starting point to work from. I understand about not "having a feel" for Word's object model; I often feel the same way about Excel and PowerPoint, when I have to do something "quick" with them.

    So if you have further questions (like what Word templates and styles are), do please ask. I know some of my answers are a bit terse at the moment - I'm in the middle of a looming book deadline and no where near as close to finished as I ought to be <sigh>


    Cindy Meister, VSTO/Word MVP
    Sunday, August 21, 2011 8:57 AM
    Moderator