Copying the content of specific word table to excel - excel

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

Related

Adapt a VBA code for Word and make it run from Excel

I have a vba code that runs in Word.
However when I control Word from excel and run the code from excel it doesn't work.
Any idea why?
Sub SetTextBoxStyle()
Dim objTextBox As Shape
Dim objDoc As Document
Set objDoc = WordApp.ActiveDocument
For Each objTextBox In objDoc.Shapes
If objTextBox.TextFrame.HasText Then
objTextBox.TextFrame.TextRange.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
objTextBox.TextFrame.TextRange.ParagraphFormat.SpaceAfter = 0
End If
Next objTextBox
End Sub
Since you have an ActiveDocument it can be safely assumed that microsoft word is running and your document is the active one. You just need to add a GetObject
Sub SetTextBoxStyle()
' You need to change word types to object as here you are working with
' late binding. If you work with early binding (i.e. by adding a reference
' to word, you can use Word.Shape and Word.Document etc
Dim objTextBox As Object ' Shape
Dim objDoc As Object ' Document
Set objDoc = GetObject(,"Word.Application").ActiveDocument
For Each objTextBox In objDoc.Shapes
If objTextBox.TextFrame.HasText Then
objTextBox.TextFrame.TextRange.ParagraphFormat.LineSpacingRule = 0
'wdLineSpaceSingle is not defined in excel vba by default
'either use its value (0), define it somewhere or add a reference to Word object model
objTextBox.TextFrame.TextRange.ParagraphFormat.SpaceAfter = 0
End If
Next objTextBox
End Sub
A lot of things can go wrong
Word might not be running and therefore, GetObject raises an error.
Your document might not be the active one.
There might be several instances of Word running and your document is not in that particular instance.
... possibly many more
To make your code robust, you need to read more about how to work with different office applications from one of them. The link provided in the comments by #braX is a good start.

Combine word documents in hyperlink list

I am new in the world of VBA. I would look for help for my problem.
I have list of file in range B3:40. In each cells, hyperlink to specific word document (.docx) file in the communal server.
Now, I would like to combine all documents in the hyperlink list in that range to become one new word document that contain all documents in the list using VBA excel.
Below is my code. I just can open it, but don't know how to combine it.
Thank you in advance for any help.
Dim xhyperlink as Hyperlink
Dim Openfile as Range
On Error Resume Next
Set Openfile = Thisworkbook.Sheets("name of sheet")
Set Openfile = Range ("B3:B40")
For Each xhyperlink In Openfile.Hyperlinks
xhyperlink.Follow
Next
To get a quicker response in the future ask a specific question about your code, "Is it possible?" is not a good question. If you are not sure how code something in Word/Excel try the Macro recorder first. Then post the specific issue you are having with the code. That being said Welcome to Stack Overflow. Here's a new account freebie answer...
Add the Microsoft Word 14.0 Object Library Reference through Tools --> References.
Sub MergeDocs()
Dim oLink As Hyperlink
Dim oLinkRange As Range
' Set Range of Hyperlinks
Set oLinkRange = Sheet1.Range("A1:A10")
'Setup "Merge" Document
Dim oWord As New Word.Application
Dim oMergeDoc As Word.Document
Set oMergeDoc = oWord.Documents.Add
'Insert Files into Master
For Each oLink In oLinkRange.Hyperlinks
oWord.Selection.InsertFile (oLink.Address)
Next
'Save Merged Document & Close
oMergeDoc.SaveAs2 ("C:\Temp\MergeTest\Merged.docx")
oMergeDoc.Close
oWord.Quit
End Sub
Note: You'll want to add some error checking in there.

Excel to Word - Mail Merge - Charts, Tables & name ranges covering multiple cells

I am trying to figure out the best way to send information from Excel to Word. I am currently using a data source created in excel (.csv) to send information to Word via mail merge. I have only figured out how to mail merge individual referenced cells and my data source has thousands of fields. I was wondering if it is possible to use mail merge or other technology to send either tables or a named ranges referencing multiple cells from Excel into Word. Ideally if I can send multiple cells at once I hopefully would not have to format each field. Any help or ideas would be much appreciated.
You could use DocVariables for this.
Sub PushToWord()
Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bkmk As Word.Bookmark
sWdFileName = Application.GetOpenFilename(, , , , False)
Set doc = objWord.Documents.Open(sWdFileName)
'On Error Resume Next
objWord.ActiveDocument.variables("BrokerFirstName").Value = Range("BrokerFirstName").Value
objWord.ActiveDocument.variables("BrokerLastName").Value = Range("BrokerLastName").Value
objWord.ActiveDocument.variables("Ryan").Value = Range("Ryan").Value
objWord.ActiveDocument.Fields.Update
'On Error Resume Next
objWord.Visible = True
End Sub
Run the code from Excel.
In the Excel VBA Editor: Tools->References->Microsoft Word x
Insert->Field->Category:DocumentAutomation->Field Names:DocVariable->Field Codes Button-> Then enter the name of the variable.

How to inport text from MS Word via vba-commandbutton

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.

Excel macro - keep source formatting

When I try to copy data between worksheets this is no problem, but when I try to copy the same data to a word document it loses its format. Is there a way to stop this?
' Copy all data from 1.xls to new.docx
Sheets("Design").Select
Range("A1:G50").Copy
appWD.Selection.Paste
Could it be something with PasteSpecial?
Thanks.
#Brown
Select Case Range("C19").Value
Case 1
Sheets("Info").Select
Range("B7").Copy Destination:=Sheets("Design").Range("A" & x)
x = x + 2
End Select
So this copies the data from cell C19(Sheet: Info) to cell B7(Sheet: Design)
' I open my word doc etc.
Sheets("Design").Select
Range("A1:E50").Copy
appWD.Selection.Paste
This selects sheet Design, copies everything and pastes this into a word doc. I lose my formatting, I'm also using XP, office 2007.
Here is my simple test program
Sub test()
Dim wrdApp As Word.Application
Set wrdApp = New Word.Application
Dim wrdDoc As Word.Document
wrdApp.Documents.Add
Set wrdDoc = wrdApp.ActiveDocument
Range("A1:B1").Copy
wrdApp.Selection.Paste
wrdDoc.SaveAs "D:\tmp\myworddoc.doc"
End Sub
This works with Office XP (I don't have Office 2007 at hand). Cells A1:B1 contain formatted numbers. This one works fine on my machine, the created word doc contains a table with formatted numbers, too. Can you try it on yours to see if it works?

Resources