Copying cells from Excel to Word bookmarks

christosf360

New Member
Joined
Apr 19, 2015
Messages
8
Hello everyone, I have been trying to do this for the past 2 days!!!! I have spent a lot of time here looking at various posts and I have tried to pick up bits and pieces to make this macro work, unfortunately I am not successful.

The task is this: I need to past various cells (ones that are not blank) from column E in XL into different bookmarks in Word.

I have this code below (copied from a post from this site) that will open the Word document but then anything I try with locating the bookmarks fails, any advice would be much appreciated:


Code:
Dim TestRange As Range




'Declare obj variables for the word application and document.
    Dim WdApp As Object, wdDoc As Object
    
'Declare a String variable for the example document's name and folder path.
    Dim strDocName As String
    
'On Error statement if Word is not already open.
On Error Resume Next


'Activate Word if it is already open.
Set WdApp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Err.Clear
    
'Create a Word application if Word is not already open.
Set WdApp = CreateObject("Word.Application")
End If
    
'Make sure the Word application is visible.
WdApp.Visible = True
    
'Define the strDocName String variable.
strDocName = "C:\Test.docx"
   
'Check the directory for the presence of the document name in the folder path.
'If it is not recognized, inform the user and exit the macro.
If Dir(strDocName) = "" Then
    MsgBox "The file UC372" & vbCrLf & _
    was not found in the folder path" & vbCrLf & strDocName, _
    vbExclamation, _
    "Sorry, that document name does not exist."
    Exit Sub
End If




'Create the Word document from the template.
    Set wdDoc = WdApp.documents.Add(strDocName)
    Run "OP"
  
'Release system memory that was reserved for the two Object variables.
  Set wdDoc = Nothing: Set WdApp = Nothing
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Your code is from this thread: https://www.mrexcel.com/forum/gener...cel-userform-word-template-via-bookmarks.html
Posts 4 & 7 in the same thread are examples of how to populate bookmarks from a userform. To populate bookmarks from an Excel range you might use something like
Code:
    With wdDoc
      .Bookmarks("Bookmark1").Range.Text = ActiveSheet.Range("A1").Value
      .Bookmarks("Bookmark2").Range.Text = ActiveSheet.Range("B1").Value
    End With
PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
 
Last edited:
Upvote 0
Paul many thanks for the reply and for the tip about posting code properly. Fairly new to this level or programming I would say and I have managed to learn all I know by picking up bits and pieces on the way. This method becomes more challenging as the task in hand gets more demanding. Are there any online VBA tutorials that you could recommend. I feel I am lacking some basic knowledge and it may be time to tackle this properly...

Many thanks again for your help!
 
Upvote 0

Forum statistics

Threads
1,223,789
Messages
6,174,576
Members
452,573
Latest member
Cpiet

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