0

I have a Word 2013 document (.docx) with values I need replacing. I can find plenty of code but can't get it to work as the imports word.application doesn't work.

My form has a text box to specify the filename of the word document and a text box for the user to put a value into (tbCRMID). Once I execute I need the value in the word document ( to be replaced with tbCRMID.text).

I have a host of other replacements to do but once I get one working, I should be all set.

Here is my code:

Try 
    Dim NewOutputFile As String 
    Dim newApp As Object 
    Dim newDoc As Object 
    newApp = CreateObject("Word.Applicatoin") 
    newApp.visible = True 
Catch ex As Exception 
    MsgBox(ex.Message) 
End Try

This code generates the error "Cannot create Active X control".

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
George Vaisey
  • 139
  • 1
  • 5
  • 19
  • The following code produces an error about "Cannot create Active X control". Trying to open the program successfully, then I'll add the document to open and then work the the find/replace functions. Try Dim NewOutputFile As String Dim newApp As Object Dim newDoc As Object newApp = CreateObject("Word.Applicatoin") newApp.visible = True Catch ex As Exception MsgBox(ex.Message) End Try – George Vaisey Aug 22 '13 at 03:18
  • 1
    Put your code in the question above - edit it. Then you can format the code block. – OneFineDay Aug 22 '13 at 03:20
  • @GeorgeVaisey - How did you add the reference for the `Microsoft Word` library? – Karl Anderson Aug 22 '13 at 03:28
  • @KarlAnderson The OP wouldn't need to add a reference to Word; he's using `CreateObject`. – Zev Spitz Aug 22 '13 at 07:28
  • @ZevSpitz - what? the OP needs a reference to the Microsoft Word Object Library, just like you said in your answer! – Karl Anderson Aug 22 '13 at 13:39
  • @KarlAnderson [CreateObject](http://msdn.microsoft.com/EN-US/library/z4393zyz(v=VS.110,d=hv.2).aspx) is a VB.NET function for creating ActiveX objects. You don't need to know anything about the type of object you are creating (nor do you need a reference to its library), as `CreateObject` returns an `Object`, but you can still call members on the object because of late-bound object behavior in VB.NET. – Zev Spitz Aug 22 '13 at 14:18
  • @ZevSpitz - I understand, my apologies. – Karl Anderson Aug 22 '13 at 14:24

1 Answers1

0

You've misspelled Word.Application. Also, consider adding a reference to the Microsoft Word Object Library, and then you can use strong types and intellisense:

Dim newApp As New Word.Application
newApp.Visible=True
Dim newDoc = newApp.Documents.Add

See here and here for some good starting points.

I also refer you to my answer here (It's for python, but the principles are the same).

Community
  • 1
  • 1
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • Thanks for the help. the misspelled word and implementing the library is was did. I can now successfully open a document. Now on to my search and replace functions... :) – George Vaisey Aug 22 '13 at 11:03