I am trying to make a configuration tool that is linked to several Excel-files.
They are all linked by an "option-number".
Are there any way to import a portion of a whole MS Word-document via the
"option-number" link to Excel?
Alright, since your question does not hold any code and doesn't really tell us much about the approach you're after, I'll just give some general pointers on how to access tables in a word document from an Excel macro.
First, you'll need to add a reference to the Microsoft Word #.# Object Library. To do this, open your VBE (alt+F11), click on tools, References... and check the checkbox for this library. The #.# above is the version number. For example word 2016 will be the Microsoft Word 16.0 Object Library.
Now you'll have access to Word objects in your Excel VBE.
You can do something like this:
Sub Test()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim wTable As Word.Table
Dim r As Word.Row
Dim c As Word.Cell
Set wApp = New Word.Application 'Get a word application object, so you can open and manipulate documents from your Excel macro.
Set wDoc = wApp.Documents.Open("C:\temp\temp.docx") 'Open your document.
Set wTable = wDoc.Tables(1) 'To access the first table in the document
For Each r In wTable.Rows 'Loop over Word table rows
For Each c In r.Cells 'Loop over Word table cells
'Do stuff with the table:
MsgBox c.Range.Text
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = c.Range.Text
Next c
Next r
'and clean up after yourself:
Set wTable = Nothing
wDoc.Close SaveChanges:=False
wApp.Quit
Set wDoc = Nothing
Set wApp = Nothing
End Sub
Good luck.
Related
I want to insert in a W10 Excel 2016 macro some code that will
open a new Word 2016 document and
copy the value of variable "s" (string) from the Excel macro to that new Word document
then returns to the Excel macro.
Maybe it helps that I know Word can be opened with this macro:
Sub startWord()
Application.ActivateMicrosoftApp xlMicrosoftWord
End Sub
TIA for your help.
If it’s a simple copy of a string into a new Word document, then the following code should work. You haven’t indicated where you get the string from, so I’ve used the contents of cell A1 on Sheet1. You can adjust this to suit.
For the following to work, you need to enable a reference within VBA. In the VBA Editor, select Tools/References & check the box ‘Microsoft Word 16.0 Object Library’. If it’s not already checked, you’ll find it listed alphabetically.
This works for me – let me know how you go with it.
Option Explicit
Dim WordApp As Word.Application, myDoc As Word.Document, s As String
Sub CopyToWord()
On Error Resume Next
Set WordApp = GetObject(class:="Word.Application")
On Error GoTo 0
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")
WordApp.Visible = True
WordApp.Activate
Set myDoc = WordApp.Documents.Add
s = Sheet1.Range("A1").Value
With myDoc.Content
.Text = s
End With
End Sub
I am trying to create a VBA code that take a specific cell in a word document and outputs it into excel.
I see lots of discussion on how to transfer all the tables from word to excel online, but is it possible to do it in a selective way?
For example I have a a word document called "example.docx" as such:
How can I transfer the content of the second cell in the second table into the A1 cell into excel?
Take into account that my aim is to loop through documents with the same structure at some point, thus I believe that indexing using the "Selection.Copy" method may not be the most ideal way for this case.
Thanks so much in advance for the help!
Should be something like this:
Sub test()
Dim wsSheet As Worksheet
Dim WordApp As Object, WordDoc As Object
Set wsSheet = ThisWorkbook.Worksheets("Sheet1")
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open("C:\Test\example.docx")
wsSheet.Range("A1").Value = WordDoc.Range.Tables(2).Range.Cells(2).Range.Text
WordApp.Quit
End Sub
I have a Word 2010 document embedded inside an Excel sheet. I want to create content control boxes inside the word doc which can be populated programatically. For this I need to set tags for the content control.
I read on the MSDN website and some other sources that it is simple enough - you just have to enable Design Mode and then right click the content control box and click Properties. However, the properties option is grayed out and disabled even though I'm in Design Mode.
When I do this on a standalone Word document (not an embedded one), it works just fine. So that's a workaround I'm using right now. However it's really inconvenient to have to create the boxes in the standalone Word doc and copy them over into the one embedded in Excel.
Is it possible to edit properties of content control box in a Word doc embedded inside an Excel sheet?
It can be done programmatically, something like the code snippet that follows.
An embedded Word document is an Excel OLEObject; such an object can be named, for example: ws.Objects(1).Name = "WordDoc" - this is saved in the workbook and will remain the same, even if other objects are added later. This does not name the document, only the object on the surface of the Worksheet.
If the embedded document has never been accessed during a session, it first needs to be activated. As doing so causes the screen to jump and the selection to change, IF conditions are included to test that and first activate the OLEObject, as well as re-selecting the cell that was active previously.
Working with the .Tag property is shown in the For Each...Next loop. You can see the result if you go into Design Mode.
Note that the macro will not work if you're in Design Mode or if the selection is in the embedded Word document.
Sub Test()
Dim ws As Excel.Worksheet
Dim currCel As Excel.Range
Dim oDoc As OLEObject
Set currCel = Application.Selection
Set ws = ActiveWorkbook.Worksheets("Sheet1")
'Debug.Print ws.OLEObjects.Count
Set oDoc = ws.OLEObjects("WordDoc")
' Debug.Print oDoc.OLEType 'Type 1 = Embedded
WorkWithWordDoc oDoc, currCel
End Sub
Sub WorkWithWordDoc(oDoc As OLEObject, selRange As Excel.Range)
Dim doc As Word.Document
Dim wasActivated As Boolean
Dim cc As Word.ContentControl
'On first opening the Workbook
'the OLE interface of the OLEObject
'isn't accessible, so activate it
wasActivated = True
On Error Resume Next
Set doc = oDoc.Object
If Err.Number = 1004 Then
Excel.Application.ScreenUpdating = False
oDoc.Activate
wasActivated = False
Set doc = oDoc.Object
Excel.Application.ScreenUpdating = True
End If
On Error GoTo 0
For Each cc In doc.ContentControls
cc.Tag = "CC in embedded Doc"
Next
'Clean up
If Not wasActivated Then
'Deactivate the document
selRange.Select
End If
Set doc = Nothing
End Sub
I am trying to create an excel macro that opens Word and creates a table. I made a macro in Word to create the table and copied it into the Excel macro. When I run it, I get this error:
run-time error '450'
wrong number of argument or invalid property assignment
at the point which excel is to execute the MS Word macro (which was recorded in Word):
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
EDIT: I have set references for the Word doc
' Declare the variables
Dim WordApp As Object
Dim WordDoc As Object
Dim wksSource As Worksheet
' Assign the active worksheet to an object variable
Set wksSource = ActiveSheet
' Create an instance of the Word application
Set WordApp = CreateObject("Word.Application")
' Make the Word application visible
WordApp.Visible = True
' Open the specified Word document and assign it to an object variable
Set WordDoc = WordApp.Documents.Open("C:\Users\Briet\Documents\PAJ\labels.dotm")
Make sure you've added a reference to the Word Object Lirary in your Excel VBA project.
This worked for me:
Sub Tester()
Dim wd As Word.Application
Set wd = GetObject(, "word.application") 'get reference to open Word document
wd.ActiveDocument.Tables.Add Range:=wd.Selection.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
End Sub
Note that because Selection is a native Excel object, you need to qualify that to indicate you're referring to the Selection in Word
wd.ActiveDocument.Tables.Add Range:=wd.Selection.Range
Hello I am looking to program a button in an excel 2010 document that will copy the entire worksheet and automatically open and paste into a word document.
I have been looking on the help pages, and there is some similar functions, but they are too specific and i can't get the code to work.
Here is the code i was using
The example i was going from was stating a range, and i can do it that way, or just copy the entire worksheet (called "outputCMCR") into a new word document.
When i run this code i get a Compile Error, User defined type is not defined.
Sub button2_click()
Dim objWord As New Word.Application
'Copy the range Which you want to paste in a New Word Document
Range("A1:B10").Copy
With objWord
.Documents.Add
.Selection.Paste
.Visible = True
End With
End Sub
Thanks any help is appreciated.
Ok I think there are 2 issues here
1- Have you referenced Microsoft Word 14.0 Object Library? To do this, click on Tools -> References (in your VBA editor). Scroll down the list until you see the Microsoft Word 12.0 Object Library or Microsoft Word 14.0 Object Library (depending on which version of Office is installed on your machine) and select it. Read this for early and late binding
2- You would need to create object like the code below
Private Sub CommandButton1_Click()
Dim objWord As Word.Application
Range("A1:B10").Copy
Set objWord = CreateObject("Word.Application.14")
With objWord
.Documents.Add
.Visible = True
.Selection.Paste
End With
End Sub