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.
Related
I have an Excel worksheet with words in each cell. I wanted to write this row
onto a Word document.
The macro worked pefectly the first time.
When the macro is run, the docx file has already been created but not opened.
I rubbed out the text in the word document and closed it
Then I ran the Excel macro again and got a message saying that the document
was locked for editing by myself.
There was an option, then, to create a local copy. I did this and eventually
I got the text on to the Word document.
What is going on, please ?
I simply don't understand what this "locked for editing" is.
I think the relevant lines of code are :
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
Dim oRng As Word.Range
destfilename = "E:\......\Dummy_Script.docx"
Set objDoc = objWord.Documents.Open(destfilename)
'
' TextA is a string with the text of the row
'
Set oRng = ActiveDocument.Range(Start:=0, End:=0)
oRng.Text = TextA
"Code"
I have a situation where I need to pass a variable from Word to Excel via a macro. The reasoning is a little complicated, but basically I want to use this as a way to create a customized index. I point that out in case there might be an easier way to create the index. This particular index is created based on the paragraph a word or phrase is found in and not the page number. There doesn't appear to be a way to do this in Word.
So, I want to highlight the word or phrase, press my macro combo (or add a right click menu item to do it) which assigns the highlighted text to a variable and then sends it to one of three open worksheets (there will be 3 sections to the index) and adds it to the bottom row of the spreadsheet. From there I need to alphabetize the list and combine all the same phrases into one cell.
I know how to send items from Excel to Word, but when I try to research the idea of sending from Word to Excel all I can find are examples of Excel retrieving something from Word and returning it back to Excel. This is not applicable to what I'm doing. There will be thousands of index entries (before I combine them).
If it isn't possible to go directly from Word to Excel then I suppose I could send the selected text to another another document, but again I don't know how to do this.
Please help!
I did not understand why you need to work in Excel from Word, but I prepared a piece of code to show you how you can (easily) acces Excel object and work using its objects:
Sub testWorkingInExcel()
Dim objEx As Excel.Application, wb As Excel.Workbook, W As Excel.Workbook, sh As Excel.Worksheet
Dim lastEmptyRow As Long, boolFound As Boolean
On Error Resume Next
Set objEx = GetObject(, "Excel.Application") 'find Excel open session if any
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
Set objEx = CreateObject("Excel.Application")
Set wb = objEx.Workbooks.Open("Workbook full name")
Else
For Each W In objEx.Workbooks
If W.Name = "Needed workbook" Then
Set wb = W: boolFound = True: Exit Sub
End If
Next
If Not boolFound Then Set wb = objEx.Workbooks.Open("Workbook full name")
End If
On Error GoTo 0
Set sh = wb.Worksheets("NeededSheet")
lastEmptyRow = sh.Range("A" & sh.Rows.Count).End(xlUp).Row
'Now, you have the last empty row of the needed page and you can do
'whatever you could do directly in Excel...
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 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.
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.