generating word doc from excel

nardagus

Active Member
Joined
Apr 23, 2012
Messages
317
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello,

Heh. It is time for me to ask a question. :)

I have an excel file with a data I need to transfer to a new word document using macro.
I did almost everything I needed except one thing. But first things first.

Structure of a word document is so far as follows:

--------------------------
Some text (generated by excel)

col1col2col3
some datasome datasome data
some datasome datasome data

<tbody>
</tbody>
(table is also generated and filled by excel)

---------------------------

What I'm struggling with is how to insert a form field after a table (wdContentControlRichText) ?
I have no problem inserting it in a document but it is inserted in the most left upper cell of a table. Question is why in a table and not at the very beginning of a document?

Here is a part of a macro. It only inserts

Code:
sub addform()


Dim objWord
Dim objDoc As Object
Dim objSelection As Object


On Error Resume Next


'checking if word is opened


Set objWord = GetObject(, "Word.Application")


'if not we open it


If objWord Is Nothing Then
    Set objWord = CreateObject("Word.Application")
End If


'creating a new document


Set objDoc = objWord.Documents.Add


'making word invisible


objWord.Visible = False


'some selection variable, lazy ppl rule


Set objSelection = objWord.Selection

' inserting some text

objSelection.Font.Name = "Calibri"
objSelection.Font.Size = "14"
objSelection.Font.Color = RGB(146, 208, 80)
objSelection.TypeText "Client:"

'insert a table

objSelection.Tables.Add Range:=objSelection.Range, NumRows:=21, NumColumns:= _
        9, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed




'trying to insert


objSelection.Range.ContentControls.Add (wdContentControlRichText)


end sub

I tried to use this piece:
Code:
objSelection.EndKey Unit:=wdStory

but it didn't work. I read somewhere that it requires excel to be visible but since I need to generate around 30 documents from this excel I want to save a user a "pleasure" to watch it generated.

Is there a way to tell excel the place field should be insterted in or maybe I can jump to the end of the document (ctrl-END) without making excel visible?

I hope I explained my issue clearily.

Thank you for help.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,215,764
Messages
6,126,751
Members
449,335
Latest member
Tanne

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