Editing The ContentControl in Word from Excel VBA - excel

I want to edit the plain text content control in my Word application.
I searched but it seems The "plain text content control" in my target Word document does not respond to
objDoc.SelectContentControlsByTag("Name").Item(1).Range.Text
I tried with "ActiveDocument" options but I want it not to work with the active ones.
Code in VBA Excel:
Sub dataToWord()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("C:\Users\ASUS\Documents\Excell to Word - Copy\WordTesting1.docx")
objWord.Visible = True
objDoc.SelectContentControlsByTag("Name").Item(1).Range.Text = "I want to type some text here"
objDoc.Save
objDoc.Close
End Sub
I get:
Run-time error '445':
Object doesn't support this action
It refers to the line
objDoc.SelectContentControlsByTag("Name").Item(1).Range.Text = "I want to type some text here"

I have disabled the WPS app . and it worked fine.. it seems it is in conflict with the microsoft word application..

Related

Print specific pages from word file from excel vba

I'm trying to print specific pages from a word file using Excel VBA, but I couldn't do that, I was only able to print the whole file and not the pages I wanted (changes according the value of LastPage). This is the code I used. Please help with that, thanks.
Sub PrintFile()
Dim objWord As Object
Dim objDoc As Object
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("D:file.docx")
objWord.Visible = False
objDoc.PrintOut from:="1", To:="LastPage".value
objWord.Quit
End Sub
Assuming you are getting an error on the line starting objDoc.PrintOut then replace that line with
objDoc.PrintOut Range:=wdPrintFromTo, From:="1", To:=CStr(LastPage)

Find and Replace footer text in Word with an Excel VBA Macro

I'm trying to make a macro in Excel which opens a Word document, find a especify text, which is inside of footer in word doc, and replace it for a text.
At the moment, my macro opens the word doc but I couldn't figure out how to get into footer and find those texts.
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(ThisWorkbook.Path & "/NotaPromissoriaAutomatica.docx")
objWord.Visible = True
The footer have two texts which have to be replaced
1 - VAR_CIDADE > Which will be replaced the current city (which is in A1 of my excel table)
2 - VAR_DATA > Which will be replaced the current date (which is in A2 of my excel table)
I created a test document with a single page, header and footer, with the footer using the keyword "VAR_DATA". The example code below will search for all footers in the document and make the replacement. Notice that the code only searches in Section(1) though. If you have more sections, you may have to create an outer loop to search for each footer in every section.
Option Explicit
Public Sub FixMyFooter()
Dim myWord As Object
Dim myDoc As Word.Document
Set myWord = CreateObject("Word.Application")
Set myDoc = myWord.Documents.Open("C:\Temp\footertest.docx")
Dim footr As Word.HeaderFooter
For Each footr In myDoc.Sections(1).Footers
With footr.Range.Find
.Text = "VAR_DATA"
.Replacement.Text = Format(Now(), "dd-mmm-yyyy")
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindStop
End With
Next footr
myDoc.Save
myWord.Quit
End Sub
You'll need to expand the example to find your additional text with your own formatting.

Macro using Documents.Open doesn't return a Document, but a String.

I want to open and read paragraphs of a Word document from an Excel macro. Here's the sample code that confuses me:
Sub example()
Dim docPath
docPath = "C:\temp\test.docx"
Dim wordApp
Set wordApp = CreateObject("Word.Application")
Dim doc
doc = wordApp.Documents.Open(docPath)
MsgBox (TypeName(doc)) 'This line displays String. According to the documentation it should be a Document.
End Sub
According to the documentation the Documents.Open method should return a Document that you can operate on. But when I check the type it says it has returned a String. Obviously I can't loop through the paragraphs of a string:
https://msdn.microsoft.com/en-us/vba/word-vba/articles/documents-open-method-word
Is the documentation wrong? Am I doing something wrong? More importantly, how do I loop through the paragraphs of a Word document from an Excel macro?
I'm using Office 365 if that matters and I have created a small Word document at C:\temp\test.docx.
try:
Set doc = wordApp.Documents.Open(docPath)
The String you are getting is some property of the Object you have created; might be the Name of the Object.................insert MsgBox doc to see what it is.
Although basically what is missing is a 'Set Doc = ...' you are guaranteed to get into trouble with a code like that. To prevent that some suggestions and a starter code (your code really, a little bit modified):
Add Word object reference from Tools\References and "type" your objects. Typing them would make it much easier to write code with intellisense support.
Never ever let the word object hang out there. Get rid of it when you are done.
Sample:
Dim docPath
docPath = "C:\temp\test.docx"
Dim wordApp As Word.Application
Set wordApp = CreateObject("Word.Application")
Dim doc As Word.Document
Set doc = wordApp.Documents.Open(docPath)
For i = 1 To doc.Paragraphs.Count
Cells(i, 1) = doc.Paragraphs(i).Range.Words.Count
Cells(i, 2) = doc.Paragraphs(i)
Next
wordApp.Quit
Set doc = Nothing
Set wordApp = Nothing

Add specific Document to specific Word.Application

May I assign the objDoc as a document of the ObjWord (after Set objDoc = oDoc.Object)?
My code looks like this:
'Declaration
Dim oDoc As OLEObject
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim WS as Worksheet
Dim strOdoc as String
'Initialization
Set WS = Whorksheets("Sheet1")
strOdoc = "WordDoc"
Set objWord = New Word.Application
'Set objWord = GetObject(, "Word.Application")
'I need using GetObject to be able to made the activated OLEDocument, invisible.
'And Need Set objWord = New Word.Application to be able
'to EndTask the WINWORD.EXE with objWord.Quit.
objWord.Visible = False
Set oDoc = WS.OLEObjects(strOdoc)
oDoc.Activate
Set objDoc = oDoc.Object
'I need here Add the objDoc to the objWord
I need objDoc to have been a document of the objWord object, which has been hidden with objWord.Visible = False (I can't use Dim objDoc As objWord.Document variable declaration).
I need the objWord to have been isolated because when using
objWord.Quit, it must not try to close other Word Documents. (Here I used Set objWord = New Word.Application)
I need using the GetObject statement to be able to made the activated OLEDocument invisible.
And Need Set objWord = New Word.Application to be able to EndTask the
WINWORD.EXE with objWord.Quit.
But how can integrate two above advantages: 1) Working Invisible with the OLEObjectDocument and 2) Perform EndTask the WINWORD.EXE if no other word documents are opened?
Please take a more logical, step-by-step approach. Bear in mind that you need the Microsoft Word Object Library to be referenced (VBE > Tools > References).
Now you can create an instance of the Word application. It is invisible by default. You can use it and quit it without affecting any other instances of the Word that might be running concurrently. That part of your code is logically correct but the recommended code is Set objWord = CreateObject("Word.Application"). This creates a new instance. I'm not sure if Set objWord = New Word.Application perhaps does the same thing but you shouldn't need both.
Now you can open a document. Use the Documents.Open method to do that. The document will be of Word.Document data type, not an OLEObject. This document will open in a window and it is that window which you can make invisible. Address it as objWord.Windows(1) or objWord.ActiveWindow. Perhaps there will be a flicker on the screen which you might combat with objWord.ScreenUpdating = False.
After these steps you would have a normal Word document, invisible in its own Window, open in an instance of Word which has no other document open in it. You can manipulate that document, close it and then quit objWord.

Excel to Word: User-defined type not defined, missing Word library in reference

I want to export some cells content in Excel to Word, I want to do like this
Sub CreateNewWordDoc()
' to test this code, paste it into an Excel module
' add a reference to the Word-library
' create a new folder named C:\Foldername or edit the filnames in the code
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
' or
'Set wrdDoc = wrdApp.Documents.Open("C:\Foldername\Filename.doc")
' sample word operations
With wrdDoc
For i = 1 To 100
.Content.InsertAfter "Here is a sample test line #" & i
.Content.InsertParagraphAfter
Next i
If Dir("C:\Foldername\MyNewWordDoc.doc") <> "" Then
Kill "C:\Foldername\MyNewWordDoc.doc"
End If
.SaveAs ("C:\Foldername\MyNewWordDoc.doc")
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
But when test the function Excel say "Compile error: User-defined type not defined"
I guess this i a libray missing, but I can't found any suitable in Tools->Reference (VBA),
like Microsoft Office World or Microsft Word... Where can I find them?
The refence depends wich version of MSOffice you are using.
For me it's "Microsoft Word 12.0 Object Library"
Hope that helps.

Resources