Passing parameters to a word macro from an excel macro - excel

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.

Related

Writing from an Excel row onto a Word docx

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"

Copying the content of specific word table to 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

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.

VBA Interop: Is there a way to Explicitly use Excel data types in a word macro and vise versa?

I'm reading data from an excel spreadsheet in a word macro so I can use the spreadsheet to instantiate a document from a template and set properties in the instantiated document from the excel table. I'm trying to be as explicit with types as I can, but it turns out that the object that comes back from an excel selection has rows typed differently than a row in a word document. Not actually that surprising now that I think about it. Is there a common base class more specific than Object between an excel spreadsheet row type and a word table row type? Maybe a way to specify that excel row type? Or is my best bet to use the Object type and not worry about the details. Dynamic typing is amazing here, most of the class methods are the same.
I am also interested in running word based VBA subroutines (located in normal) from excel VBA. I tried to look it up via Google but did not have any luck.
The guts are here:
Set testList = LoadExcel(strFile)
testList.Activate
Dim allSuites As Object
' LoadExcel also selects the rows that contains the data we want in excel
Set allSuites = testList.sheets("FullSuiteList").Application.Selection
Dim myRow As row
Dim Columns() As String
Dim i As Integer
' Previously I used a table in word, but there are too many columns
' to be manageable
' LoadPropertyNames testList.Tables(1).Rows(1), Columns
' For i = 2 To testList.Tables(1).Rows.Count
' Now we get data directly from our Excel sheet
LoadPropertyNames allSuites.Rows(1), Columns
For i = 2 To allSuites.Rows.Count
CreateOne allSuites.Rows(i), Columns
Next I
' row type yields type mismatch at runtime. myrow as Object works though.
Function LoadPropertyNames(myRow As row, Columns() As String)
...
End Function
' row type yields type mismatch at runtime. myrow as Object works though.
Function CreateOne(myRow As row, Columns() As String)
...
End Function
In the VBA editor, in the Tools menu choose References then add "Microsoft Excel ##.# Object Library" to your Word VBA project. This will give you access to all of Excel's objects. Note that some object names, e.g. "Range" represent different objects in Word vs Excel, so declare variables as Word.Range or Excel.Range as appropriate.
To answer your question in the comment about running one of the Normal template's subroutines. First in Excel add a reference to the the Word object model --- in the Tools menu choose References then add "Microsoft Word ##.# Object Library" to your Excel project. Then here's an example of how to call a Normal template sub:
Sub RunNormalTemplateSub()
Dim wdapp As Word.Application
Dim wddoc As Word.Document
Set wdapp = New Word.Application
wdapp.Visible = True
' This is the line that runs the sub
' This runs a sub named "ThisIsATest"
' in the module "Sandbox"
' in the Project/Template "Normal"
wdapp.Run "Normal.Sandbox.ThisIsATest"
' If the sub takes arguments then you would call
' wdapp.Run "Normal.Sandbox.ThisIsATest", Arg1, Arg2
Set wdapp = Nothing
End Sub
Note that Word is running the subroutine. I don't know of a way to have Excel run the Word subroutine (maybe it can be done, but I don't know how).
Hope that helps

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