View Single Post
 
Old 02-06-2013, 03:11 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try something based on the following:
Code:
Sub CreateWordDocFromExcel_BM()
     'LATE BINDING METHOD - Reference to Word not required.
    Dim objWord As Object 'Note Object in lieu of Word.Application
    Dim objDoc As Object 'Note Object in lieu of Word.Document
    Dim wdRng As Object 'Note Object in lieu of Word.Range
    Dim fileToOpen As Variant
    Const StrBkMkNm As String = "MySpots"
 
     'Late binding requires values or constants to be declared with values
    Const wdFieldEmpty = -1
    Const wdWithInTable = 12
 
    fileToOpen = Application.GetOpenFilename("WordDocuments (*.doc*), *.doc*")
    If fileToOpen = "" Or fileToOpen = False Then
        MsgBox "No file has been opened"
        Exit Sub
    Else
        MsgBox "Now opening:" & vbCr & fileToOpen
    End If
 
     'Try GetObject first in case Word Application is already open.
    Set objWord = Nothing
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    On Error GoTo 0
    If objWord Is Nothing Then 'Word not open so create object
        Set objWord = CreateObject("Word.Application")
    End If
    With objWord
        .Visible = True 'Can be false
         'Open the document
        Set objDoc = .Documents.Open(fileToOpen, AddToRecentFiles:=False)
        With objDoc
            Set wdRng = .Bookmarks(StrBkMkNm).Range
            With wdRng
                If .Information(wdWithInTable) = True Then
                    If .End = .Cells(1).End Then .End = .End - 1
                End If
                .Text = vbNullString
            End With
            .Fields.Add Range:=.Bookmarks(StrBkMkNm).Range, Type:=wdFieldEmpty, Text:="REF MyReference \*Charformat", PreserveFormatting:=False
            .Bookmarks.Add Range:=wdRng, Name:=StrBkMkNm
        End With
    End With
     'Clean up
    Set wdRng = Nothing: Set objDoc = Nothing: Set objWord = Nothing
End Sub
Note that I've stripped out the constants etc that are not required, and that no seeking/selecting is used.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote