Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #5  
Old 06-30-2022, 07:48 PM
Guessed's Avatar
Guessed Guessed is offline Error 4605 on Word Section Save VBA procedures all the time Windows 10 Error 4605 on Word Section Save VBA procedures all the time Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
 

Tags
macro, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error 4605 on Word Section Save VBA procedures all the time Error 4605 while trying to copy and paste with same document Ilmari Word VBA 2 05-05-2020 12:08 AM
Error 4605 on Word Section Save VBA procedures all the time 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 on Word Section Save VBA procedures all the time 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:25 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft