Results 1 to 4 of 4

Thread: [RESOLVED] Specific VB Coding Explanation?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    2

    Resolved [RESOLVED] Specific VB Coding Explanation?

    Hi,

    I'm new here, so sorry if this is in the wrong place (I couldn't find anywhere else relevant). For a project I'm currently working on, I need to explain the coding I have used in Word. I'd found a code online, and tried to edit it to suit my requirements - I'm in no way an expert so it could be full of errors, but does what I wish. The macro I've been using splits a mail merged document into separate documents.

    I need the code explained line-by-line, so basically what each line does/means. If anybody could provide me with any help, I'd be incredibly grateful as I'm very new to VB.

    Thanks in advance,
    Marie.
    VB Code:
    1. Sub SplitLetters()
    2.  
    3. Dim mask As String
    4. Selection.EndKey Unit:=wdStory
    5. Letters = Selection.Information(wdActiveEndSectionNumber)
    6. mask = "<<Company>>"
    7.  
    8. Selection.HomeKey Unit:=wdStory
    9. Counter = 1
    10. While Counter < Letters
    11. DocName = "D:\Merged\" & Format(Date, mask) _
    12. & " " & LTrim$(Str$(Counter)) & ".doc"
    13. ActiveDocument.Sections.First.Range.Cut
    14. Documents.Add
    15. With Selection
    16.   .Paste
    17.   .EndKey Unit:=wdStory
    18.   .MoveLeft Unit:=wdCharacter, Count:=1
    19.   .Delete Unit:=wdCharacter, Count:=1
    20. End With
    21.  
    22. ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
    23. ActiveWindow.Close
    24. Counter = Counter + 1
    25. Wend
    26.  
    27. End Sub
    Last edited by Marie_; Jan 31st, 2006 at 06:38 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Specific VB Coding Explanation?

    Word VBA question moved to Office Development

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Specific VB Coding Explanation?

    It creates a new document for each section in the current document.
    VB Code:
    1. Dim mask As String '<- Declare a string (text) variable and calls it "mask"
    2. Selection.EndKey Unit:=wdStory '<- Move to the end of the document
    3. Letters = Selection.Information(wdActiveEndSectionNumber) 'Save the number of sections available in the document and store it an a variable named "Letters"
    4. mask = "<<Company>>" 'Store a string in the "mask" variable
    5.  
    6. Selection.HomeKey Unit:=wdStory '<- Move to the start of the document
    7. Counter = 1 '<- Set the variable "Counter" to 1
    8. While Counter < Letters '<- Repeat everything from here until "WEnd" while Counter is less than Letters (Counter is 1 the first time, Letters is the number of sections)
    9.     DocName = "D:\Merged\" & Format(Date, mask) _
    10.      & " " & LTrim$(Str$(Counter)) & ".doc"
    11.     'The above 2 lines are really one line. It creates a file name with the current date and the current section (Letter) number
    12.     ActiveDocument.Sections.First.Range.Cut '<- Cut the first section from this document
    13.     Documents.Add '<- Create a new empty document
    14.     With Selection '<- With the current selection (the start) of this new empty document do the following
    15.         .Paste '<- Paste the earlier cut text
    16.         .EndKey Unit:=wdStory '<- Jump to the end of this document
    17.         .MoveLeft Unit:=wdCharacter, Count:=1 '<- Move back one step
    18.         .Delete Unit:=wdCharacter, Count:=1 '<- Delete the last character
    19.     End With
    20.     'The below line saves the document using the file name created earlier
    21.     ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
    22.     ActiveWindow.Close '<- Close this document, which takes you back to the earlier document you cut text from
    23.     Counter = Counter + 1 '<- Increase the Counter variable with 1
    24. WEnd '<- Go back and check if Counter is still less then Letters, in which case the above code will run again

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    2

    Re: Specific VB Coding Explanation?

    Thank you so much for the help, it's just what I needed.

Posting Permissions

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



Click Here to Expand Forum to Full Width