0
votes

I am trying to use VBA in an open .docm file to open a 2nd read only .docx file and then insert -> object -> text from file (a 3rd read only .docx stored within the same folder).

The below code correctly opens and merges the two files but when it comes to saving the output it returns a Run-Time 13 “mismatch” error. My limited understanding leads me to believe that at the point where I am saving, the active document reference is still the original .docm and it is the .docx designation that then causes the conflict.

I am really struggling to manage the active document reference to avoid this. Presumably I am missing something very simple, all assistance is very gratefully received.

Documents.Open ActiveDocument.Path & "\DocA.docx", Visible:=True
        
Selection.InsertFile FileName:=ActiveDocument.Path & "\DocB.docx", Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
                
ActiveDocument.SaveAs2 "C:\Users\" & Environ("UserName") & "\DocC" & ".docx", FileFormat:= _
wdFormatXMLDocument

ActiveWindow.Close
1
In your scenario, ActiveDocument will just confuse you. Instead use Documents(1) and Documents(2), or use a Set statement to create separate named references.John Korchok

1 Answers

0
votes

Putting flesh on John Korchok's comment:

Sub deleteme3()
    Dim oldDoc As Document
    Set oldDoc = Documents.Open(ActiveDocument.Path & "\DocA.docx", Visible:=True)
    oldDoc.Activate
    selection.Collapse Direction:=wdCollapseEnd 'to insert at end of document
    selection.Range.InsertBreak Type:=wdPageBreak
    Selection.EndKey Unit:=wdStory
    Selection.InsertFile FileName:=ActiveDocument.Path & "\DocB.docx", range:="", _
    ConfirmConversions:=False, Link:=False, Attachment:=False
    oldDoc.SaveAs2 "C:\Users\" & Environ("UserName") & "\DocC" & ".docx", FileFormat:= _
        wdFormatXMLDocument
    oldDoc.Close
    Set oldDoc = Nothing
End Sub

Note this puts the inserted document at the end of the original document. You may want to use a next-page section break instead if there is header/footer differentiation. If you need that, please comment and I will include it.

There are a number of break types. Here is the enumeration of all of them if you are interested. The following types create a page break of one sort or another:

  • wdPageBreak (the default)
  • wdSectionBreakNextPage
  • wdSectionBreakOddPage (starts section on next odd-numbered page - good for chapters)
  • wdSectionBreakEvenPage (starts section on next even-numbered page - rarely used)

If wanting to preserve headers and footers additional code would be needed. (Every section in a Word document has three headers and three footers, even if they are not displayed or used.)

' Break Link to Previous in newly added section for all of the headers and footers
Dim oHeaderFooter As HeaderFooter
Dim iCounter As Long
Let iCounter = ActiveDocument.Sections.Count
' break link in headers
For Each oHeaderFooter In ActiveDocument.Sections(iCounter).Headers
    Let oHeaderFooter.LinkToPrevious = False
Next oHeaderFooter
' repeat for footers
For Each oHeaderFooter In ActiveDocument.Sections(iCounter).Footers
    Let oHeaderFooter.LinkToPrevious = False
Next oHeaderFooter