Consulting

Results 1 to 4 of 4

Thread: Cannot be opened because there are problems

  1. #1

    Cannot be opened because there are problems

    I'm stumped. It's late. Maybe fresh eyes can see what's wrong.

    I made a nice template, tightened up a routine to gather data via userform launched in Excel, and then populate various elements of Template, save newly created Document. Works great...so I thought.

    Whenever I try to open the .docx file thereafter however, I get a Microsoft Office Word Message Box informing me:

    "The file <blah blah>.docx cannot be opened because there are problems with the contents."

    The Details >>> button reveals the very helpful: "No error detail available."

    I thought it must be something wrong in my template, so I made a new one, with just one Content control...same results. Made another one with just one Bookmark, thinking maybe Content Controls were the culprit...same result.

    If i use the same template, and manually create a new document from it, insert the text and save it, no problems...I can reopen the .docx file.

    Here's the little routine I used to test the scenario. It mimics my more in depth routine for the real document. I launch this from Excel 2007, creating Document in Word 2007.

    To use this create a .dotx file with one CC or one bookmark tagged/named "testdata"

    [VBA]Public Sub makeContract()

    Dim wrdApp As Word.Application
    Dim conTemp As Word.Document

    Dim SaveName As String
    Dim myPath As String
    Dim myTemplate As String

    myPath = "C:\Testing\"

    'myTemplate = "testContentControl.dotx"
    myTemplate = "testBookmark.dotx"

    SaveName = "Test"

    Set wrdApp = CreateObject("Word.application")
    wrdApp.Visible = True

    Set conTemp = wrdApp.Documents.Open(myPath & myTemplate)

    'conTemp.SelectContentControlsByTag("testdata").Item(1).Range.Text = "12345"
    conTemp.Bookmarks("testdata").Range.InsertAfter "12345"


    conTemp.SaveAs myPath & SaveName & ".docx"

    Set conTemp = Nothing
    Set wrdApp = Nothing

    End Sub
    [/VBA]

    What am I missing??

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Keith,

    Your sample code doesn't appear to be doing what you describe being done. You have a template "textContentConrol.dotx" that contains a content control "testdata"
    You want to create a new document based on this template from Excel and populate the CC with some data.
    Then save the new document.
    Right?

    [VBA]Public Sub makeContract()
    Dim wrdApp As Word.Application
    Dim conTemp As Word.Document
    Dim SaveName As String
    Dim myPath As String
    Dim myTemplate As String
    myPath = "C:\"
    myTemplate = "D:\My Documents\Word\Templates\testContentControl.dotx"
    SaveName = "Test"
    Set wrdApp = CreateObject("Word.application")
    wrdApp.Visible = True
    Set conTemp = wrdApp.Documents.Add(myTemplate)
    conTemp.SelectContentControlsByTag("testdata").Item(1).Range.Text = "12345"
    conTemp.SaveAs Filename:=myPath & SaveName, FileFormat:=wdFormatDocumentDefault
    Set conTemp = Nothing
    Set wrdApp = Nothing
    End Sub
    [/VBA]

    While I have not see the problem before and can't explain it off hand, it appears to be caused by your ".docx" in the SaveAs line.

    Try what I have posted.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Greg:

    Thanks, that file format was the issue. Having now read the help files more thoroughly, clearly my ".docx" was not of the type Enum "WdSaveFormat" as required by the .saveas call. Interesting that the vba code compiled though, and didn't crash on me. It was just afterwards when trying to open the file created that way that you knew you had a problem.

    In the meantime, I had gotten there, in the process of having thrown together a generic example. See the Attached Excel file. A little over board with a Custom Ribbon Tab and all, but was just trying to further digest some things I'm working on. Good exercise.

    Thanks for the fresh eyes on it!
    Attached Files Attached Files

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Keith,

    I haven't fully explored the issue but I don't think it was necessarilly your ".docx" not being of the type expected but more the absence of a type altogether. Without it I think Word saved in default format but then when you added .docx as part of the file name things got screwed up.

    Anyway glad you have it worked out and that you have learned something in the process.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •