Writing vba variables from Excel cells directly to Word bookmarks

Cudd

New Member
Joined
Jun 20, 2008
Messages
2
My apologies in advance if this is topic has been covered (perhaps multiple times) on this board. I am not quite sure how to phrase the question in succinct search terms.

I have a set of user forms in Excel that prompt for various bits of data, store them in variables, and then writes them to subsequent cells in worksheet.

I am then trying to craft automation code that will write that data to a Word doc. Current I am accomplishing this by selecting a cell, copying, and "paste special" into various bookmarks in Word, but I want to find a more direct approach.

I would like to be able to directly write the value of a variable that I have defined in VBA code from my Excel workbook to a subsequent bookmark in Word. (No cells as a intermediary)

I am trying to avoid using an Excel cell as a go between.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,

a simple example. You must still adapt:


  • path and file name
  • the names of the text marks
  • the names of the variables
Code:
Sub Test()
    Dim wdApp As Object
    Dim strTMP1 As String
    Dim strTMP2 As String
    Dim strTMP3 As String
    strTMP1 = "Text mark 1"
    strTMP2 = "Text mark 2"
    strTMP3 = "Text mark 3"
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If wdApp Is Nothing Then Set wdApp = CreateObject("Word.Application")
    On Error GoTo 0
    With wdApp
        .Visible = True
        .Documents.Open "C:\Temp\Test.doc" 'adapt
        .ActiveDocument.Bookmarks("Textmark1").Range = strTMP1
        .ActiveDocument.Bookmarks("Textmark2").Range = strTMP2
        .ActiveDocument.Bookmarks("Textmark3").Range = strTMP3
    End With
    Set wdApp = Nothing
End Sub
Case_Germany
 
Upvote 0
Hi,

a simple example. You must still adapt:


  • path and file name
  • the names of the text marks
  • the names of the variables
Code:
Sub Test()
    Dim wdApp As Object
    Dim strTMP1 As String
    Dim strTMP2 As String
    Dim strTMP3 As String
    strTMP1 = "Text mark 1"
    strTMP2 = "Text mark 2"
    strTMP3 = "Text mark 3"
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If wdApp Is Nothing Then Set wdApp = CreateObject("Word.Application")
    On Error GoTo 0
    With wdApp
        .Visible = True
        .Documents.Open "C:\Temp\Test.doc" 'adapt
        .ActiveDocument.Bookmarks("Textmark1").Range = strTMP1
        .ActiveDocument.Bookmarks("Textmark2").Range = strTMP2
        .ActiveDocument.Bookmarks("Textmark3").Range = strTMP3
    End With
    Set wdApp = Nothing
End Sub
Case_Germany

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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