Macro run from Excel to transfer data in new Word doc won't output into Word

cpu97

Board Regular
Joined
Jan 8, 2010
Messages
69
Hello,

I usually only work in Excel, this is the first time I am trying to output to Word. As part of a larger macro run from Excel which will ultimately find and paste data into a new Word doc, I found a number of relevant threads here and was able to cobble together some code which mostly works. The part that isn't working is below. It's giving me a 438 run time error on the TypeText line. I get the same error when I run the code below from a macro run from Word. Microsoft Word 15.0 Object Library has been checked in References. I've tried other methods to specify the text string, such as omitting the "Text:=" and putting the string in parentheses, to no avail. I suspect the problem is a simple one having to do with the word document object, but hopefully someone more knowledgeable can tell me the fix. Thanks for your help.

VBA Code:
Dim WdApp As Word.Application
Set WdApp = New Word.Application
WdApp.Visible = True

Dim wdDoc As Word.Document
Set wdDoc = WdApp.Documents.Add

wdDoc.Content.Select
wdDoc.Selection.TypeText Text:="Test"
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I see the docs for Selection.TypeText specifies this warning:
This method has changed. Using VBA Selection commands like Selection.BoldRun on user selection with Comments no longer applies bold formatting on user-selected text or Selection.TypeText command or on user selection with Comments no longer inserts text.

I am not too familiar with the Word object model, so I'm not 100% sure what that leaves you or what your other options are for what you are trying to do. If you could clarify what you want to achieve there might be a different way?
 
Upvote 0
Thank you. The "Test" output in the code above is obviously just a placeholder. I am ultimately trying to output an array in vba, element by element. But if I can't get it to output a test string, the rest of it doesn't matter. After seeing a reference to range on another thread, I poked around and found (Range).InsertAfter, and that works! Amended code below:

VBA Code:
Dim WdApp As Word.Application
Set WdApp = New Word.Application
WdApp.Visible = True

Dim wdDoc As Word.Document
Set wdDoc = WdApp.Documents.Add

Dim startRng As Range
Set startRng = wdDoc.Range(Start:=0, End:=1)
startRng.InsertAfter ("Test")
 
Upvote 0
Thank you. The "Test" output in the code above is obviously just a placeholder. I am ultimately trying to output an array in vba, element by element. But if I can't get it to output a test string, the rest of it doesn't matter. After seeing a reference to range on another thread, I poked around and found (Range).InsertAfter, and that works! Amended code below:

VBA Code:
Dim WdApp As Word.Application
Set WdApp = New Word.Application
WdApp.Visible = True

Dim wdDoc As Word.Document
Set wdDoc = WdApp.Documents.Add

Dim startRng As Range
Set startRng = wdDoc.Range(Start:=0, End:=1)
startRng.InsertAfter ("Test")
Well, it worked when I ran it from a macro stored in a Word doc and called from a Word doc, but "Set startRng = wdDoc.Range(Start:=0, End:=1)" results in a run-time error code 13 (type mismatch) when I run it from Excel. Perhaps I have to specify a different Range object type when referencing a Word document from a macro stored in Excel...
 
Last edited:
Upvote 0
Yes - you need to use Dim startRng as Word.Range
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,714
Members
452,995
Latest member
isldboy

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