+ Reply to Thread
Results 1 to 5 of 5

Preserve formatting in a word doc that I am importing excel data to via VBA/docvariables?

  1. #1
    Registered User
    Join Date
    08-11-2022
    Location
    US
    MS-Off Ver
    2013
    Posts
    7

    Preserve formatting in a word doc that I am importing excel data to via VBA/docvariables?

    I am attempting to import data into a very structured word document (i.e., a form with lots of tables, aligned fields, etc. packed together) from excel via docvariables. I've got the actual data transfer worked out, but the formatting is a nightmare. Basically if anything expands or shifts at all the whole document falls apart. I looked into using content controls in word or docvariables in publisher, but neither seem to be supported. Are there other methods to 'lock-in' the original structure of the word document and force the imported data to conform to a specified space?

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Preserve formatting in a word doc that I am importing excel data to via VBA/docvariabl

    Without seeing your code, or the Excel & Word files you're working with, it's impossible to know what is going on or how to fix it. You telling us "the formatting is a nightmare. Basically if anything expands or shifts at all the whole document falls apart" isn't particularly descriptive or helpful.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    08-11-2022
    Location
    US
    MS-Off Ver
    2013
    Posts
    7

    Re: Preserve formatting in a word doc that I am importing excel data to via VBA/docvariabl

    I've got proprietary content in my files but could mockup some analogues if necessary. However, my general questions would be:

    1. Is there a way to 'lock' all content in a word document in place, such that anything that is subsequently imported won't alter the original formatting?

    2. Is there a way to put constraints on docvariables such that, e.g., font size would dynamically adjust down to fit within specified parameters?


    I can put this into actual files if necessary, but the gist of it would be a word doc with fields like this (where the goal is to keep the 'Name:', 'Client ID:', and 'Date:' positioning the same no matter what the length of the imported doc variable content is):

    Name: {DOCVARIABLE LastName \* CHARFORMAT}, {DOCVARIABLE FirstName \* CHARFORMAT} Client ID: {DOCVARIABLE ClientID \* CHARFORMAT} Date: {DOCVARIABLE Date \* CHARFORMAT}


    The excel/VBA end of it would be along the lines of:

    Private Sub dataExport Click()

    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document

    Dim backgroundData(1 To 4) As String

    backgroundData(1) = "LastName"
    backgroundData(2) = "FirstName"
    backgroundData(3) = "ClientID"
    backgroundData(4) = "Date"

    Dim wbBook As Workbook
    Dim wsSheet As Worksheet

    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets("Data")

    wsSheet.Range(Cells(Selection.Row, 2), Cells(Selection.Row, 5)).Select

    bckgrndData = Selection

    Instantiate the Word Objects.
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\Report.Template.docx")

    For i = 1 To 4
    With wdDoc
    .Variables.Add Name:=(backgroundData(i)), Value:=bckgrndData(1, (i))
    .Fields.Update
    End With
    Next i

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Preserve formatting in a word doc that I am importing excel data to via VBA/docvariabl

    Importing content doesn't of itself have any effect on formatting. If what you're importing has an effect on the document's layout, perhaps you should consider importing that content into a Word table having sufficiently-large fixed dimensions to accommodate whatever data you're importing.

    Document variables are nothing more than plain text strings. As such they cannot have any formatting characteristics of their own.

    In any event, unless you're using the same variables in multiple locations in a document, there is no point in using them - and the corresponding fields; one might as well insert the data directly into the document at the relevant locations.

  5. #5
    Registered User
    Join Date
    08-11-2022
    Location
    US
    MS-Off Ver
    2013
    Posts
    7

    Re: Preserve formatting in a word doc that I am importing excel data to via VBA/docvariabl

    Thanks- yes, I may not have articulated this well but after doing additional reading it seems that using Word tables is the way to go for this issue.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Importing Data from Word to Excel
    By rickwillson in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-26-2013, 01:33 AM
  2. Replies: 1
    Last Post: 03-19-2013, 06:45 AM
  3. Importing data from invoices (in word) to excel log
    By jposey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-26-2012, 10:33 AM
  4. [SOLVED] Lost Functionality Importing Word Data File into Excel
    By alcharbonneau in forum Excel General
    Replies: 5
    Last Post: 04-18-2012, 12:14 PM
  5. help importing data from word file into excel
    By BBLANCOBRNX in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-11-2012, 04:25 PM
  6. Importing jpgs from excel to word data merge
    By comountain in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-07-2009, 03:37 AM
  7. importing data from excel into ms word
    By Jabbr in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 06-19-2007, 05:25 PM
  8. Excel importing of data from word
    By BarryD in forum Excel General
    Replies: 0
    Last Post: 12-24-2005, 06:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1