![]() |
|
#5
|
||||
|
||||
|
Using the Selection object is not recommended, especially when your code is opening/closing new documents. If your code does the right thing and defines objDoc then it should also start making use of that to ensure the code (and observers) doesn't confuse the new docs with the original doc. Also, you can get big changes in formatting if you are inserting content into a new document with different style definitions. The following changes to the code should deal with each of those problems.
Code:
Sub SaveEachSectionAsADoc()
Dim objDocAdded As Document, objDoc As Document
Dim nSectionNum As Integer, strFolder As String
Dim dlgFile As FileDialog
' Initialization
Set objDoc = ActiveDocument
Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)
' Pick a location to keep new files.
With dlgFile
If .Show = -1 Then
strFolder = .SelectedItems(1) & "\"
Else
MsgBox "Select a folder first!"
Exit Sub
End If
End With
' Step through each section in current document, copy and paste each to a new one.
For nSectionNum = 1 To objDoc.Sections.Count
Set objDocAdded = Documents.Add(Template:=objDoc.FullName)
objDocAdded.Range.FormattedText = objDoc.Sections(nSectionNum).Range.FormattedText
' Save and close new documents.
objDocAdded.SaveAs FileName:=strFolder & "Section " & nSectionNum & ".docx"
objDocAdded.Close
Next nSectionNum
End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
| Tags |
| macro, vba |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Error 4605 while trying to copy and paste with same document
|
Ilmari | Word VBA | 2 | 05-05-2020 12:08 AM |
run-time error 4605 command not available with Selection.paste
|
Marzio | Word VBA | 14 | 01-30-2019 04:46 AM |
| Word Error Message Run time Error 4605 | baes10 | Word VBA | 1 | 08-30-2018 02:37 PM |
Error 4605 when looping through files in folder and deleting comments
|
Peterson | Word VBA | 2 | 04-19-2018 08:45 AM |
| PasteAppendTable not available (Run-Time Error 4605) | q_scribe | Word VBA | 1 | 08-12-2013 09:56 AM |