Copying Word Content fails with associated template

S

stdavidclarke

I have been working on this far too long so hopefully someone has an
idea about this...

I have a visual basic (6) program that creates a new document, opens
several exiting documents and copies the contents of them into the new
document. It ends up being a report. The problem is this. When one
of the existing documents I open has an attached template containing a
form field (in this case a label control) the document does not seem
to work properly. My code fails the first time I access a property or
method. I have tried this with Word version 2003 and 2007 with the
same result. If I clear the "AttachedTemplate" field in the document
and save it it all works fine (this fix is not a solution as there are
thousands of documents that could potentially be selected for this
report. If I delete the "Label" field from the template it works as
well. The line that fails is: "doc.Content.Copy".

The document I am opening has regular text a a few pictures but has an
"AttachedTemplate" which contains the form field (label control).

Forgive the terrible variable names...

Any help would be GREATLY appreciated.

Dim DocPath As String
Dim qa As Object
Dim aa As Object
Dim doc As Object
Dim nD As Object
Dim Ran As Object

DocPath = "C:\Documents and Settings\clarke david\Desktop
\SomeDoc.doc"
Set qa = CreateObject("Word.Application")
Set aa = CreateObject("Word.Application")

qa.Visible = True
aa.Visible = True

Set doc = qa.Documents.Open(FileName:=DocPath, ReadOnly:=True,
AddToRecentFiles:=False) ' ReadOnly:=True,
Set nD = aa.Documents.Add

If doc.ProtectionType <> -1 Then
doc.UnProtect
End If

doc.Content.Copy

Set Ran = nD.Content
Ran.Collapse (0)
Ran.Paste
 
C

Cindy M.

If I clear the "AttachedTemplate" field in the document
and save it it all works fine (this fix is not a solution as there are
thousands of documents that could potentially be selected for this
report.
Why couldn't your code clear the attached template before you try
copying?

Doc.attachedTemplate = NormalTemplate

would reset it to the default template. You could then close the
document without saving changes so that it keeps its link to that
template.

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 :)
 
S

stdavidclarke

I had that same idea but it fails when trying to access the
"attachedTemplate" property. It is almost as if any property accessed
causes a failure. While in debug I can see values for these
properties but the program fails when accessing programmatically.

This made me think the document was projected but it is not.

Any other ideas? I have tried a lot of things and nothing has worked
for me.
 
S

stdavidclarke

I had the same thought but it failed with an error. It seems like any
property I access failes although while in debug I can see values for
all the properties. I don't understand why these form fields cause
this problem.

I thought the document was protected but it is not.

Any other ideas? I have tried many without success.
 
C

Cindy M.

I had that same idea but it fails when trying to access the
"attachedTemplate" property. It is almost as if any property accessed
causes a failure. While in debug I can see values for these
properties but the program fails when accessing programmatically.

This made me think the document was projected but it is not.
Mmm, you say "form field" but there is no label control in the Word form
fields. There is in the ActiveX controls (which were originally designed
for VBA user forms). If you open a document containing ActiveX controls
on an installation where macro security is set to anything but "Low", by
default it will go into Design Mode and lock out macros. My guess would
be this might be the problem.

You could try this at the beginning of your code:

Application.AutomationSecurity = msoAutomationSecurityLow

But before you go into production with it you need to be aware of the
possible security risk involved in allowing all macros to be enabled.
And if any "Auto" macros are in these documents or their templates they
could interfere with your application.

The other road to travel would be to sign these template projects (the
attached templates) with a digital signature. I'm talking about the VBA
project. Even though there may be no VBA code in a file, adding ActiveX
controls activates the code storage of a Word file. This is what the
macro security checks. Digitally signing with a certificate from a
trusted publisher will stop the macro security check. But it would still
pose a problem to your application if any "Auto" macros are present.

So you may need to figure out why the people who created these documents
felt they needed, of all things, a LABEL control? What does that do that
couldn't be achieved by other means?

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 :)
 

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