Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-29-2018, 08:55 PM
KjBox KjBox is offline Adding a Field to Word Document Windows 10 Adding a Field to Word Document Office 2016
Novice
Adding a Field to Word Document
 
Join Date: Mar 2018
Posts: 6
KjBox is on a distinguished road
Default Adding a Field to Word Document

I have the below codes.

There is 1 other procedure which updates the workbook then calls "GetWordFile" then calls "PopulteWord"
Code:
Sub GetWordFile()
    Dim oWd As Object, oDoc As Object, f As Boolean
    
    sfile = Application.GetOpenFilename( _
        FileFilter:="Word Files *.doc* (*.doc*),", _
        Title:="Browse to and open required word file.", _
        MultiSelect:=False)
    If sfile <> "False" Then
        On Error Resume Next
        Set oDoc = GetObject(sfile)
        On Error GoTo 0
        If oDoc Is Nothing Then
            Set oWd = GetObject(, "Word.Application")
            If oWd Is Nothing Then
                Set oWd = CreateObject("Word.Application")
                If oWd Is Nothing Then
                    MsgBox "Failed to start Word!", 16, "Word File Selection"
                    Exit Sub
                End If
                f = True
            End If
            Set oDoc = oWd.Documents.Open(sfile)
            If oDoc Is Nothing Then
                MsgBox "Failed to open selected document!", 16, "Word File Selection"
                If f Then
                    oWd.Quit
                End If
                Exit Sub
            End If
            oWd.Visible = True
        Else
            With oDoc.Parent
                .Visible = True
            End With
        End If
    Else
        Application.DisplayAlerts = 0
        MsgBox "No file selected.", 16, "Word File Selection"
        Application.DisplayAlerts = 1
    End If
End Sub

Sub PopulateWord()
    Dim x, y, i As Long, oDoc As Object, sKap As String, sFldText As String
    Set oDoc = GetObject(sfile)
    
    x = [tblMergefields]
    
    With oDoc
        .Bookmarks("Start").Range.Select
        .Activate
        With .Parent.Selection
            i = i + 1
            GetTextAndStyle CStr(x(i, 20))
            sFldText = "MERGEFIELD  " & x(i, 6) & " "
            .TypeText Text:=x(i, 20) & " " & sText
            .Style = oDoc.Styles(sStyle)
            .TypeParagraph
            .Fields.Add .Range, wdFieldEmpty, sFl, 1
            .Style = oDoc.Styles("Normal")
            .TypeParagraph
            i = i + 1
            GetTextAndStyle CStr(x(i, 20))
            .TypeText Text:=x(i, 20) & " " & sText
            .Style = .Parent.Styles(sStyle)
            .TypeParagraph
               ' further code that is a repeat of above to add further headings and fields
        End with
    End with

End Sub

Sub GetTextAndStyle(s As String)
    Dim x, i As Integer
    
    x = [tblKapitel]
    For i = 1 To UBound(x, 1)
        If x(i, 1) = s Then
            sText = x(i, 2)
            sStyle = UCase(x(i, 4))
            Exit For
        End If
    Next
    If i = UBound(x, 1) + 1 Then
        sText = x(i, 2)
        sStyle = "Normal"
    End If
    
End Sub
The word document that is selected will always be empty but with preset Styles and a single bookmark to set the starting point for the required data to be added.

All works fine until the line
Code:
.Fields.Add .Range, wdFieldEmpty, sFl, 1
Then I get Error 4608 "Value out of range"

Recording a macro directly in the opened document to add the field gives
Code:
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "MERGEFIELD  F111 ", PreserveFormatting:=True
The value in x(i, 6) is "F111" and the variable sFldText returns the correct Text.

The cursor in the word document moves to a new paragraph correctly and is in the correct position to add the Field.

I have tried countless ways to get around this error but can't figure it out, any help or suggestions greatly appreciated.

TIA.
Reply With Quote
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding the same field in the column field list of pivot table nanka Excel 4 03-19-2017 08:12 AM
Adding a link into a word document that when pressed, takes user to a page within the same document yan89 Word 1 04-29-2016 01:54 PM
Adding tables to Created word document whilst other word document open Help rpb925 Word VBA 18 03-30-2016 04:45 PM
Adding field in word in header in a bookmark in table with Excel vba Late Binding Hdr Excel Programming 6 02-11-2013 02:58 AM
Adding a Field to Word Document Excel vba adding field in word table/shape in a header Hdr Excel 1 02-04-2013 04:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:10 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