
Create New Document and Copy Table from Existing Document
I am about halfway there - but I still need some help.
What I want to do is create a new document from a Template and copy a table
from an existing document into the new document.
Here is what I have:
********** Start of Code ********************
Sub AutoNew()
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim objdocs As Object
Dim strLetter As String
Dim strTestFile As String
Dim strDocsPath As String
Dim strTemplatePath As String
Dim strFileName As String
Set appWord = GetObject(, "Word.Application")
strFileName = "LetterHeadTable.doc"
strDocsPath = DocsDir
strTemplatePath = TemplateDir
strLetter = strTemplatePath & strFileName
'Check for existance of template in Template Folder, and exit
'if not found.
strTestFile = IsNull(strLetter)
Debug.Print "Test file: "; strTestFile
If strTestFile = "" Then
MsgBox strLetter & " template can not be found. Can't create the
letter." & vbCrLf _
& "Contact the Help Desk with the template name."
Exit Sub
End If
Set objdocs = appWord.Documents
appWord.Documents.Open strLetter
Selection.WholeStory
Selection.Copy
-----Below breaks unless I manually change to the proper
document.---Selection.GoTo What:=wdGoToBookmark, Name:="letterhead"
Selection.Paste
---- Remainder works as expected ------------
On Error GoTo EH
'Make letter visible
appWord.Visible = True
appWord.Activate
EH_Exit:
Exit Sub
EH:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
ElseIf Err = 5151 Then
MsgBox "The template you requested is no longer available. If you
believe this" _
& vbCrLf & " to be an error, contact the Help Desk", , "Document
Name or Path No Longer" _
& " Available."
Resume EH_Exit
Else
MsgBox Err.Number & ": " & Err.Description
Resume EH_Exit
End If
End Sub
Public Function TemplateDir() As String
Dim appWord As Word.Application
On Error GoTo EH
Set appWord = GetObject(, "Word.Application")
TemplateDir = appWord.Options.DefaultFilePath(wdWorkgroupTemplatesPath) &
"\"
EH_Exit:
Exit Function
EH:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description
Resume EH_Exit
End If
End Function
Public Function DocsDir() As String
Dim appWord As Word.Application
On Error GoTo EH
Set appWord = GetObject(, "Word.Application")
DocsDir = appWord.System.PrivateProfileString("", _
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders", _
"Personal") & "\"
EH_Exit:
Exit Function
EH:
If Err = 429 Then
'Word is not running; open Word with CreateObject
Set appWord = CreateObject("Word.Application")
Resume Next
Else
MsgBox Err.Number & ": " & Err.Description
Resume EH
End If
End Function
*************** End of Code ***********************
I am missing two key pieces of information to make the above work.
1. How do I move to the new document to perform the paste?
2. After I copy the table, I want to close the LetterHeadTable.doc and then
continue with more coding
Any help you can provide is GREATLY APPRECIATED!
Thanks.
Dawn