Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-01-2022, 01:39 PM
zanodor zanodor 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
Novice
Error 4605 on Word Section Save VBA procedures all the time
 
Join Date: Jun 2022
Posts: 17
zanodor is on a distinguished road
Default Thanks



Quote:
Originally Posted by Guessed View Post
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
Sorry, Andrew, despite hitting refresh on the page I somehow missed your input. You are absolutely right. I encountered some changes in the formatting but thankfully nothing major that necessitates doing over what I had done in the meantime.

EDIT. I tried your code with a slight update and the error for Paste was gone. Imagine I ran Repair on Office 3 times already! Super, many thanks.

Cheers,

Z.
Reply With Quote
Reply

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 01:21 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