Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 02-06-2013, 03:11 PM
macropod's Avatar
macropod macropod is offline Adding field in word in header in a bookmark in table with Excel vba Late Binding Windows 7 64bit Adding field in word in header in a bookmark in table with Excel vba Late Binding 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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding field in word in header in a bookmark in table with Excel vba Late Binding Open header to insert text into bookmark Amapola188 Word VBA 3 07-12-2012 05:16 PM
Excel Pivot Table Calculated Field BertLady Excel 0 05-21-2012 10:51 AM
Late Binding on SHDocVw tinfanide Excel Programming 1 05-19-2012 10:00 PM
Adding field in word in header in a bookmark in table with Excel vba Late Binding Putting a bookmark on a REF field in the selection b0x4it Word VBA 4 05-26-2011 01:14 AM
Adding field in word in header in a bookmark in table with Excel vba Late Binding Highlight and put bookmark on the closest field to the cursor b0x4it Word VBA 11 05-19-2011 11:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft