excel value to word bookmark - macro to keep formatting

excelor100

New Member
Joined
Aug 10, 2017
Messages
8
Hi all,


I lost access to my old email account so having to post for the first time again. Have always found everyone here amazingly helpful and i manage to struggle by with macros compared to most here.


I have macro which I'm using to copy a cell range 'name' in excel to a bookmark in word with the same 'name'. It works perfectly (and i can't contest to writing it all). However I'm looking for a way to keep the formatting, can anyone help?


The issue I think is that it's copying over the cell value, but this means it loses any formatting when it reaches the word bookmark.


Using the cell 'name' ranges from excel is extremely helpful, as it means I can edit the excel document, add rows etc and the range name stays the same meaning I do not need to alter the macro. Whereas If i use a copy and paste macro with a keep source formatting code (which would work i think) it would need a lot of manual editing.


So I'm hoping someone may have a solution to keep the formatting with the below macro!


Any ideas hugely appreciated.


Code:
Sub DataMergeToWord()
Dim pappWord As Object
Dim docWord As Object
Dim wb As Excel.Workbook
Dim xlName As Excel.Name
Dim TodayDate As String
Dim Path As String


  Set wb = ActiveWorkbook
  TodayDate = Format(Date, "mmmm d, yyyy")
  Path = wb.Path & "\Test1.docm"
  
  On Error GoTo ErrorHandler


'Create a new Word Session
  Set pappWord = CreateObject("Word.Application")
  
  On Error GoTo ErrorHandler


'Open document in word
  Set docWord = pappWord.Documents.Add(Path)


'Loop through names in the activeworkbook
  For Each xlName In wb.Names
    'if xlName's name is existing in document then put the value in place of the bookmark
    If docWord.Bookmarks.Exists(xlName.Name) Then
      docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
    End If
  Next xlName


'Activate word and display document
  With pappWord
      .Visible = True
      .ActiveWindow.WindowState = 0
      .Activate
  End With


'Release the Word object to save memory and exit macro
ErrorExit:
   Set pappWord = Nothing
   Exit Sub


'Error Handling routine
ErrorHandler:
   If Err Then
      MsgBox "Error No: " & Err.Number & "; There is a problem"
      If Not pappWord Is Nothing Then
        pappWord.Quit False
      End If
      Resume ErrorExit
   End If


End Sub


Thanks!


EDIT:
To make it clear, by formatting I mean decimal points etc or format of cell in % etc.

I know i could have for example a £ sign in word, but it wouldn't have the comma in a monetary figure for example, and the data coming across has a lot of numerical figures.
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,223,790
Messages
6,174,594
Members
452,574
Latest member
hang_and_bang

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top