Writing from an Excel row onto a Word docx - excel

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"

Related

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.

Populate Excel File from Word Document Tables - VBA

I'm fairly new to VBA and trying to populate a preexisting excel document based on Word Documents.
The Word Documents will have three tables, and certain cells will become the Excel columns. The idea is, every day new product information sheets come in and the Excel sheet will need to be appended. I've started by looking over this previously asked question. Do I create a macro-enabled excel sheet and run it from within Excel? Could I get the macro to look inside a directory for the word documents, and perform an iterative macro?
How about this?
Sub ImportFromWord()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(ThisWorkbook.Path & "\My document.doc")
'Use below line if document is already open.
'Set wrdDoc = Documents("My document.doc")
With wrdDoc
N_Of_tbles = .Tables.Count
If N_Of_tbles = 0 Then
MsgBox "There are no tables in word document"
End If
Set wrdTbl = .Tables(1)
ColCount = wrdTbl.Columns.Count
RowCount = wrdTbl.Rows.Count
' Loop through each row of the table
For i = 1 To RowCount
'Loop through each column of that row
For j = 1 To ColCount
'This gives you the cell contents
Worksheets("sheet1").Cells(i, j) = wrdTbl.Cell(i, j).Range.Text
Next j
Next i
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
MsgBox "completed"
End Sub
That's the simplest solution. In Excel set a reference to Word in the VB Editor using Tools, References - you can then write code to manipulate Word from within Excel. You can use the keyword DIR to look for files in a folder, then declare a Word object, open the word document, iterate over the tables in the document and copy the values across to the right cells in Excel. Just watch for the ^p character that Word sticks in the cells - I tend to out the word cell's contents into a string variable and then take Left(s,len(s)-1) into excel to drop the last char.

Copy Word content into Excel spreadsheet using Excel VBA

I have a set of word documents which contains evaluation forms. I can manually copy and paste these along with their formatting into an excel spreadsheet, but I am interested in automating this using VBA since I have about 400 of these.
How can I open each of these and copy and paste the data into excel while retaining all of the formatting?
I would get the text from clipboard with:
Dim DataObj As New MSForms.DataObject
DataObj.GetFromClipboard
myString = DataObj.GetText
and then parse that text. You can check out this link https://excelmacromastery.com/vba-string-functions/#Extracting_Part_of_a_String
The first stage is to set a reference to Microsoft Word in the vb editor in Excel. You can then open a word document like this
Dim wd as new Word.application
dim doc as word.document
set doc = wd.documents.open("path and mame of word document")
'working with a table is like this 'Assume target is a pointer to an excel cell
Dim t As Word.Table
Set t = doc.Tables(1)
t.Cell(3, 2).Range.Copy 'this copies the cell at row 3, column 2
target.PasteSpecial xlPasteValues
That should get you started

Passing parameters to a word macro from an excel macro

I have an issue a variable to a word app, via an excel macro:
dim wrdApp as Word.Application
dim wrdDoc as Word.Document
dim PRODUCT_NAME as string
PRODUCT_NAME="This thing"
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
wrdApp.Run "MAIN_TOC", PRODUCT_NAME
This generates Run-time error '450':
Wrong number of arguments or invalid property assignment.
What am I doing wrong?
As a background, I was processing multiple tables in Excel and passing the tables to Word, via the clipboard.
I needed someway to let Word know what table I was passing. Using 'WrdApp.Run "MAIN_TOC", PRODUCT_NAME', I thought,
would allow Word to process each clipboard with the correct name.
What in fact was happening, was that PRODUCT_NAME was passed but the contents of the clipboard was erased.
My eventual solution, was to place the name of the table, in the table itself.
The word macro would get the clipboard and then look at the first cell of the table to get the table name.
When done, Excel then passed the next table and the process repeated.
All is working now.

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