[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:
Sub SplitLetters()
Dim mask As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
mask = "<<Company>>"
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
DocName = "D:\Merged\" & Format(Date, mask) _
& " " & LTrim$(Str$(Counter)) & ".doc"
ActiveDocument.Sections.First.Range.Cut
Documents.Add
With Selection
.Paste
.EndKey Unit:=wdStory
.MoveLeft Unit:=wdCharacter, Count:=1
.Delete Unit:=wdCharacter, Count:=1
End With
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
ActiveWindow.Close
Counter = Counter + 1
Wend
End Sub
Re: Specific VB Coding Explanation?
Word VBA question moved to Office Development
Re: Specific VB Coding Explanation?
It creates a new document for each section in the current document.
VB Code:
Dim mask As String '<- Declare a string (text) variable and calls it "mask"
Selection.EndKey Unit:=wdStory '<- Move to the end of the document
Letters = Selection.Information(wdActiveEndSectionNumber) 'Save the number of sections available in the document and store it an a variable named "Letters"
mask = "<<Company>>" 'Store a string in the "mask" variable
Selection.HomeKey Unit:=wdStory '<- Move to the start of the document
Counter = 1 '<- Set the variable "Counter" to 1
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)
DocName = "D:\Merged\" & Format(Date, mask) _
& " " & LTrim$(Str$(Counter)) & ".doc"
'The above 2 lines are really one line. It creates a file name with the current date and the current section (Letter) number
ActiveDocument.Sections.First.Range.Cut '<- Cut the first section from this document
Documents.Add '<- Create a new empty document
With Selection '<- With the current selection (the start) of this new empty document do the following
.Paste '<- Paste the earlier cut text
.EndKey Unit:=wdStory '<- Jump to the end of this document
.MoveLeft Unit:=wdCharacter, Count:=1 '<- Move back one step
.Delete Unit:=wdCharacter, Count:=1 '<- Delete the last character
End With
'The below line saves the document using the file name created earlier
ActiveDocument.SaveAs FileName:=DocName, FileFormat:=wdFormatDocument
ActiveWindow.Close '<- Close this document, which takes you back to the earlier document you cut text from
Counter = Counter + 1 '<- Increase the Counter variable with 1
WEnd '<- Go back and check if Counter is still less then Letters, in which case the above code will run again
Re: Specific VB Coding Explanation?
Thank you so much for the help, it's just what I needed.