Create New Document and Copy Table from Existing Document 
Author Message
 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



Sun, 01 May 2005 09:12:57 GMT  
 Create New Document and Copy Table from Existing Document
Hi Dawn,

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

Try using objectvariables for the documents and ranges (see code below).
And there's one thing I don't understand, if you use a template file, why do you open it and not create a new document based upon it?
    > appWord.Documents.Open strLetter
In the following code, a document based on the template is opened

--------------------------------------------------------------------------------
Dim oOrgDoc as Word.Document
Dim oNewDoc as Word.Document
Dim oRange as Word.Range

    'Code that opens the LetterHeadTable document
    set oOrgDoc = appWord.Documents.Add (Template:=strLetter)
    'Code to copy here
    'Open new or previously saved document to paste
    Set oNewDoc = appWord.Documents.Open (Filename:="Path and filename.doc") 'open
    Set oNewDoc = appWord.Documents.Add(Template:="Path and filename.dot") 'create
    'Create a range to copy to
    Set oRange = oNewDoc.Content
    'or, if to a  bookmark
    Set oRange = oNewDoc.Bookmarks("NameBookmark").Range
    oRange.Paste

    'Close the original document
    oOrgDoc.Close SaveChanges:=wdDoNotSaveChanges

   'Cleanup
    Set oOrgDoc = Nothing
    Set oRange = Nothing
    Set oNewDoc = Nothing
--------------------------------------------------------------------------------    

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/



Mon, 02 May 2005 07:36:03 GMT  
 Create New Document and Copy Table from Existing Document
Thanks for the help.

I have gotten my documents to work the way I want them to!

Dawn


Hi Dawn,

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

Try using objectvariables for the documents and ranges (see code below).
And there's one thing I don't understand, if you use a template file, why do
you open it and not create a new document based upon it?
    > appWord.Documents.Open strLetter
In the following code, a document based on the template is opened

----------------------------------------------------------------------------
----
Dim oOrgDoc as Word.Document
Dim oNewDoc as Word.Document
Dim oRange as Word.Range

    'Code that opens the LetterHeadTable document
    set oOrgDoc = appWord.Documents.Add (Template:=strLetter)
    'Code to copy here
    'Open new or previously saved document to paste
    Set oNewDoc = appWord.Documents.Open (Filename:="Path and filename.doc")
'open
    Set oNewDoc = appWord.Documents.Add(Template:="Path and filename.dot")
'create
    'Create a range to copy to
    Set oRange = oNewDoc.Content
    'or, if to a  bookmark
    Set oRange = oNewDoc.Bookmarks("NameBookmark").Range
    oRange.Paste

    'Close the original document
    oOrgDoc.Close SaveChanges:=wdDoNotSaveChanges

   'Cleanup
    Set oOrgDoc = Nothing
    Set oRange = Nothing
    Set oNewDoc = Nothing
----------------------------------------------------------------------------
----

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to
the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/



Mon, 09 May 2005 02:46:25 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Best method to copy document contents to new document

2. Creating word Macro to copy text and create new files from existing one

3. Create new field in existing table exactly like field in second table

4. Create new Documents / new pages

5. Copy a document but without the document template

6. Copying table definition to create new table

7. Copy a style to a new document

8. Copying picture(s) to a new document

9. copying the content of a document to a new one

10. How to copy all macros to a new document

11. Copy all document content to new

12. Updating Documents Created From a Master Document Template

 

 
Powered by phpBB® Forum Software