PDA

View Full Version : [SOLVED:] Template code not working in new documents



JBSMichael
11-07-2013, 04:53 AM
Hi all, new member here.

I have an issue with a Word 2010 .dotm template that contains code to import text from a .doc file held elsewhere in the same Sharepoint environment. The idea is simple enough: complete a table in the .doc file, then at a later date, create a .docx based on the .dotx that holds the code, click on a button and import some (populated) table text from the .doc to overwrite an (empty) table in the .docx. Both source and destination files look similar, with the relevant areas of table/text both being encapsulated in a bookmark called Part1Content.

The code in the template is held in the ThisDocument object. When the template is used to create a new document, what is created is a .docx (rather than a .docm), but the events and modules still run. The only issue seems to be that when I run this 'import' code by clicking the relevant button in the new document, the problem code appears to run (for example the desired text goes onto the clipboard) but nothing is pasted into the destination document. The real headache is that this has worked in the past but has stopped doing so and I don't know why.

Here is the code from the template:



Private Sub CommandButton2_Click()
Dim docMOD As Document
Dim doc As Document
Dim bkmTF As Bookmark
Dim bkmSGF As Bookmark
Dim rngTF As Range
Dim rngSGF As Range
Dim strLink As String

On Error Resume Next
ActiveDocument.Shapes("txtInstruction").Delete 'Get rid of instruction textbox
On Error GoTo 0

strLink = InputBox("Please paste a link to the Tasking Request file in the box below, then click OK. In the box that follows, click OK again.", "Source File", vbOKCancel)
If strLink = vbNullString Then Exit Sub
ThisDocument.FollowHyperlink (strLink)
Set docMOD = ActiveDocument
Set bkmTF = docMOD.Bookmarks("Part1Content")
Set rngTF = bkmTF.Range
Set bkmSGF = ThisDocument.Bookmarks("Part1Content")
Set rngSGF = bkmSGF.Range

rngTF.Copy
rngSGF.Select
Selection.Paste

docMOD.Close (False)

End Sub



I have copied this code into a normal module and run it - it works fine then.

Any ideas? I'm exhausted.

Thanks

Michael

Jay Freedman
11-07-2013, 07:31 PM
You've run into the common confusion between "ThisDocument" and "ActiveDocument". In Office VBA, ThisDocument refers to the object (template or document) that contains the running code -- in this case, your .dotm file. (That's not to be confused with the ThisDocument module, which appears in every template and document...) The object ActiveDocument refers to whatever document has the focus at the time the statement runs.

Therefore, your statement

Set bkmSGF = ThisDocument.Bookmarks("Part1Content")
refers to the bookmark in the template, not in the document based on the template.

Complicating matters, you have two documents open, so ActiveDocument.Shapes early in the code refers to the document based on the template; but after you follow the hyperlink to open the .doc file, ActiveDocument refers to the Tasking Request document.

What you need to do is

At the beginning of the macro, assign ActiveDocument to the variable doc (which you declared but haven't used for anything).
Replace both occurrences of ThisDocument (or at least the second one) with doc.


That should work.

As an additional suggestion, you can avoid using the clipboard (copy/paste). If the user has anything on the clipboard that they wanted to use later, running copy/paste in the macro would wipe that out. Instead, replace this block of code



Set bkmTF = docMOD.Bookmarks("Part1Content")
Set rngTF = bkmTF.Range
Set bkmSGF = ThisDocument.Bookmarks("Part1Content")
Set rngSGF = bkmSGF.Range

rngTF.Copy
rngSGF.Select
Selection.Paste

with this:


doc.Bookmarks("Part1Content").Range.Text = docMOD.Bookmarks("Part1Content").Range.Text

or, if the formatting should be copied along with the plain text,


doc.Bookmarks("Part1Content").Range.FormattedText = docMOD.Bookmarks("Part1Content").Range.FormattedText

JBSMichael
11-08-2013, 03:41 AM
Thank you Jay, you are a legend. And thanks for responding so quickly.