Shred Dude
02-11-2011, 12:39 AM
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"
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
What am I missing??
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"
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
What am I missing??