SaveAs problem with Word 2007 docx format

G

Graham Mayor

My web site hosts an add-in created by fellow MVP Doug Robbins to split a
merge into individual files. This worked fine, but occasionally there was an
issue when the original document template on which the merge was based was
not available and the resulting merge, then based on the normal template,
produced some odd formatting.

To overcome that issue I added the following piece of code which saves the
document with a new name (so as to leave the original merge document
intact). The copy document is then stripped of content and saved as a
template and closed, The copied merge document is then reopened and the
template so created attached ready to merge..

Sub CreateMergeTemplate()
Dim sTempPath As String
Dim sQuery As String
Dim sRestore As String
Dim sATemp As String
Dim oSection As Section
Dim oStory As Range
Dim oMergeDoc As Document

If Documents.Count = 0 Then
MsgBox "No document present!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If

Set oMergeDoc = ActiveDocument
If InStr(1, oMergeDoc, ".dot") Then
MsgBox "Active document is a template!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If

If ActiveDocument.MailMerge.MainDocumentType <> wdFormLetters Then
MsgBox "Active document is not a letter merge document!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If


sTempPath = Options.DefaultFilePath(Path:=wdUserTemplatesPath) & Chr(92)

With oMergeDoc
If Application.Version = 12 Then
.SaveAs FileName:="SplitMerge.docx", _
FileFormat:=wdFormatDocument
Else
.SaveAs FileName:="SplitMerge.doc", _
FileFormat:=wdFormatDocument
End If

sRestore = .FullName

For Each oSection In .Sections
oSection.Range.Delete
Next oSection
For Each oStory In .StoryRanges
oStory.Delete
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Delete
Wend
End If
Next oStory
Set oStory = Nothing
With .PageSetup
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
End With

If Application.Version = 12 Then
.SaveAs FileName:=sTempPath & "SplitMerge.dotx", _
FileFormat:=wdFormatTemplate
Else
.SaveAs FileName:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate
End If
sATemp = .FullName
.Close SaveChanges:=wdDoNotSaveChanges
End With

Documents.Open sRestore
With ActiveDocument
.AttachedTemplate = sATemp
.Save
End With
End Sub

This produces an error message when run on Word 2007 docx format merge
document from the SaveAs function and is unable to attach the template, but
it works fine on Word 2003

As a temporary workaround I have changed the sections below to work in
compatibility mode thus:

If Application.Version = 12 Then
.SaveAs FileName:="SplitMerge.doc", _
FileFormat:=wdFormatDocument97

and

If Application.Version = 12 Then
.SaveAs FileName:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate97

which does work without error, but I would prefer to work with docx and dotx
formats in case the merge document contains features introduced by Word
2007. Does anyone have any insights into what this needs to force it to work
in Word 2007's native formats?

Thanks

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tony Jollans

Hi Graham,

I haven't tried to run it but I'm not sure I understand what you want to do.

.SaveAs FileName:="SplitMerge.docx", _
FileFormat:=wdFormatDocument

.. will save in Word97-2003 format - it will be called 'docx' but will be
in binary format. Is that what you really want?

To save in word 2007 format you should use, wdFormatDocumentDefault or
wdFormatXMLDocument, although for it to compile in earlier versions you'll
need to use the numeric values.
 
C

Cindy M.

Hi Graham,
This produces an error message when run on Word 2007 docx format merge
document from the SaveAs function and is unable to attach the template,
What error message, and which line of code is causing it, specifically?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
G

Graham Mayor

The code, as you might imagine, was adapted from a Word 2003 macro and I
wanted it to be able to run on Word 2007 also. I have tried it with
wdFormatXMLDocument and it made no difference to the core problem.

What I have is a simple mail merge test document in Word 2007 docx format,
associated with a data source. In Word 2003, you can 'SaveAs' and give the
document a new name - which is what the original macro does.

However, if I attempted the same thing with Word 2007 and used SaveAs to
save the merge document with a new docx filename, the document could not be
saved. Instead it produced a document in use warning. To get around this, I
used SaveAs to save in compatibility mode, which worked, both for me and my
correspondent who was having similar problems.

However this was the situation when I posted the message. There was a raft
of updates yesterday and today that part seems to work. I find I can use
SaveAs to save the docx file with a new docx filename. However, this only
moves the problem further down to where it is saved as a template.

Attempting to save the, currently now empty, document as a template invokes
the template in use warning and therefore the document cannot be saved (even
manually). Similarly if I get around that and try to attach the revised
template to the new merge document I have created, then that function too
crashes.

Using compatibilty mode throughout works, both for me and my correspondent
who raised the issue, but I can foresee occasions when it might not be
desirable to use compatibilty mode - particularly as Word 2007 (and later)
gain wider acceptance?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

See my reply to Tony. I get the problem even when using SaveAs manually.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Robbins - Word MVP

Hi Graham,

The following worked for me (I had the change the name of the document and
template to SplitMerge2 because after a crash and even restarting the
computer, I was getting an error message that I could not save with the
original SplitMerge name because it was in use by another process?) The
changes that I made to your original code were setting the formats to
wdFormatXMLDocument for the document and wdFormatXMLTemplate for the
template:

Dim sTempPath As String
Dim sQuery As String
Dim sRestore As String
Dim sATemp As String
Dim oSection As Section
Dim oStory As Range
Dim oMergeDoc As Document

If Documents.Count = 0 Then
MsgBox "No document present!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If

Set oMergeDoc = ActiveDocument
If InStr(1, oMergeDoc, ".dot") Then
MsgBox "Active document is a template!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If

If ActiveDocument.MailMerge.MainDocumentType <> wdFormLetters Then
MsgBox "Active document is not a letter merge document!" & vbCr & _
"Open the merge document and run this macro again", _
vbCritical, "Merge Template Creator"
Exit Sub
End If


sTempPath = Options.DefaultFilePath(Path:=wdUserTemplatesPath) & Chr(92)

With oMergeDoc
If Application.Version = 12 Then
.SaveAs filename:="SplitMerge2.docx", _
FileFormat:=wdFormatXMLDocument
Else
.SaveAs filename:="SplitMerge.doc", _
FileFormat:=wdFormatDocument
End If

sRestore = .FullName

For Each oSection In .Sections
oSection.Range.Delete
Next oSection
For Each oStory In .StoryRanges
oStory.Delete
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Delete
Wend
End If
Next oStory
Set oStory = Nothing
With .PageSetup
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
End With

If Application.Version = 12 Then
.SaveAs filename:=sTempPath & "SplitMerge2.dotx", _
FileFormat:=wdFormatXMLTemplate
Else
.SaveAs filename:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate
End If
sATemp = .FullName
.Close SaveChanges:=wdDoNotSaveChanges
End With

Documents.Open sRestore
With ActiveDocument
.AttachedTemplate = sATemp
.Save
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

Now I am completely bemused as that works for me too. If I use the name
SplitMerge - even though that file doesn't exist on the hard drive and even
though there are no lock files associated with such a file, it crashes with
the 'in use' message. In use by what? It seems bizarre?

If you change the file types to the compatibilty option types, Splitmerge as
a name works without the error

If Application.Version = 12 Then
.SaveAs FileName:="SplitMerge.doc", _
FileFormat:=wdFormatDocument97

If Application.Version = 12 Then
.SaveAs FileName:=sTempPath & "SplitMerge.dot", _
FileFormat:=wdFormatTemplate97

I can live with the change of name, but am curious why it should be
necessary.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top