i have a word document that has text in it, when the document opens the selection is in the very first line of the document so when I run the code below to add a new page the whole text moves to the new page, how do I move the selection to the end of the document so that when I add a new page the text does not move to the new page?
edit: tried below but does not work
"objSelection.EndKey Unit:=wdStory, Extend:=wdMove"
"objWord.Documents("letters.docx").Selected.EndKey Unit:=wdStory, Extend:=wdMove"
Sub exceltoword()
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\WORK\Desktop\letters.docx"
objWord.Activate
Set objSelection = objWord.Selection
'**MOVE TO END OF DOCUMENT BEFORE ADDING NEW PAGE**
'tried objSelection.EndKey Unit:=wdStory, Extend:=wdMove --- does not work
'tried objWord.Documents("letters.docx").Selected.EndKey Unit:=wdStory, Extend:=wdMove --- does not work
objSelection.InsertNewPage
objWord.Application.Quit
Set objWord = Nothing
End Sub
Your issue is a result of your use of late binding, declaring objWord As Object instead of setting a reference to the Word object library.
When using late binding you cannot use the enums or constants from the Word object library as Excel doesn't know what those represent. You either have to declare those constants yourself or use their underlying values.
Actually there is little value to using late binding in this instance. The only advantage to late binding is version independence, i.e. you can write code in a newer version and it will theoretically work in an older one. But that advantage is lost unless you also use late binding for the host application code as Office is usually installed as a package. It is better to use early binding when working with other Office applications and reserve late binding for other commonly used libraries, e.g. ADO or XML.
If you want to know more about late binding see https://rubberduckvba.wordpress.com/2019/04/28/late-binding/
I also noticed that you are not using Option Explicit at the top of the code module, as your code contains undeclared variables. To add this automatically to new modules open the VBE and go to Tools | Options. In the Options dialog ensure that Require Variable Declaration is checked.
You should also break the habit of using Selection. There is rarely any need to select anything when using VBA.
So your code should look similar to this:
Sub exceltoword()
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Dim wdDoc As Word.Document
Set wdDoc = wdApp.Documents.Open("C:\Users\WORK\Desktop\letters.docx")
wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
wdApp.Quit
Set wdApp = Nothing
End Sub
Related
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..
I'm attempting to populate a Word.dotm document that has content control text boxes using Excel.
In Excel, I create the word objects and open the file, but I can't figure out how to actually access the controls.
In the Word doc's VBA, I can access them via
.SelectContentControlsByTitle("control").Item(1).Range.Text = "..."
but that doesn't work using an Object in Excel..
Here's the code I've tried in Excel:
Dim objWord As Object
Dim objDoc As Object
Dim objSelection As Object
Dim ctrl As Word.ContentControl
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add(ThisWorkbook.Path & "\MyDoc.dotm")
objWord.Visible = True
Set objSelection = objWord.Selection
'Doesn't work
objDoc.SelectContentControlsByTitle("control").Item(1).Range.Text = "..."
'Doesn't work either, but tried nonetheless
For Each ctrl In objDoc.contentcontrols
If ctrl.Title = "control" Then
ctrl.Range.Text = "!"
Exit For
End If
Next ctrl
UPDATE:
I didn't have the MS Word 16.0 Object Library referenced... the early binding and the tip to copy the template file rather than altering it directly mentioned by Timothy Rylatt now works and John Korchok's For Each loop works as well. Their additions have been added to the code above. Thank you!
Although you have declared ctrl as a ContentControl it is not a Word content control, which is why the For Each loop won't work.
If you want to use early binding you need to specify the library that the object type belongs to in the variable declaration. Some libraries have objects with the same name but which are completely different.
Dim ctrl As Word.ContentControl
NB: You should also create a document from the Word template rather than edit the template directly:
Set objDoc = objWord.Documents.Add(ThisWorkbook.Path & "\MyDoc.dotm")
In any macro, but especially in cross-program macros, avoid using the Selection object and use Range object instead. Sometimes using the range object requires a slightly less direct route to accessing the control, such as having to poll all content controls to find which one has the correct title:
Sub SetCCText()
Dim CC As ContentControl
For Each CC In ActiveDocument.ContentControls
If CC.Title = "control" Then
CC.Range.Text = "Text"
End If
Next CC
End Sub
I've been using late binding code by using excel 2016, however when the earlier versions tried opening my created excel file, missing reference will happen and i have to remove it every time.
I thought late binding not suppose to be happened that way? My code as below :
Private Sub NTStep2a_Click()
Dim ws As Object
Set ws = ThisWorkbook.Sheets("Data Entry (A)")
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\" & Environ("username") & "\Desktop\xxx"
objWord.ActiveDocument.Unprotect Password:="xxx"
With objWord.ActiveDocument
objWord.ActiveDocument.Protect Password:="xxx", NoReset:=False, Type:=wdAllowOnlyFormFields
End With
Set objWord = Nothing
End Sub
Kindly advise.
Maybe you refer to some version of library which is not available.
For example, you use createObject("word.application.11") to create word, but word version 11 is not available.
You could try omit the version number, e.g. createObject("word.application").
In spite of heavy googling, I am not able to figure out, what is wrong with this. Do I still miss a reference or something? If you can see, where the error lies, I'll be forever grateful!
References:
Visual Basic For Applications
Microsoft Excel 16.0 Object Library
OLE Automation
Microsoft Office 16.0 Object Library
RefEdit Control
Microsoft Word 16.0 Object Library
Variables:
Public appWord As Word.Application
Public sapmWord As Word.Document
Dim asNimi As String 'in this current sub
Code:
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err <> 0 Then
Set appWord = CreateObject("Word.Application")
End If
On Error GoTo 0
appWord.Visible = True
Set sapmWord = appWord.documents.Open("C:\ThisIsWorking\andDocOpens.docx")
'sapmWord.Activate 'doesn't make a difference
With sapmWord
Selection.EndKey Unit = wdStory 'this line is first line to give an error. With or without a dot in the beginning of line.
Selection.TypeText Text:=asNimi 'this line too, if previous is commented
'...and so on!
End With
sapmWord.Close savechanges:=True
Set appWord = Nothing
Set sapmWord = Nothing
sapmWord is a word document. A word document doesn't have a selection method. The Word application object has it, so probably you mean (and yes, you need the '.')
With appWord
.Selection.EndKey Unit:= wdStory
.Selection.TypeText Text:=asNimi
'...and so on!
End With
You have to change sapmWord.Close savechanges:=True for appWord.quit savechanges:=True
To use With, you must reference members with a .:
With sapmWord
.Selection.EndKey Unit = wdStory
.Selection.TypeText Text:=asNimi
End With
At the end I had no choice but adding bookmarks to the Word document and filling them with VBA. I still have no clue why those original codes did not work in my code, although they work for others. Thank you all for help, maybe some one else is getting answers here anyway.
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.