I have a large program to extract data from 7 tables from a database and put all required data in a single workbook with 7 worksheets, each feeding a part of the ultimate word document. I am a volunteer trying to produce a Course Brochure for a not-for-profit educational organisation for older people. Because of the formatting requirements I wish to use Word documents to convert the data to the right format before generating a pdf file to go to the printers.
I am in the process of using Excel VBA code to generate the word documents. I have the following References activated: Visual Basic For Applications; Microsoft Excel 14.0 Object Library; OLE Automation; Microsoft Office 14.0 Object Library; Microsoft Forms 2.0 Object Library; Microsoft Word 14.0 Object Library.
In quite a number of cases when I try to run Word VBA statements within the Excel VBA code I get error messages which seem to indicate I am not doing something needed. In addition to the above references, I select or generate a word application:
(objWord is defined as Public Word.Application) then create each document with code such as
where oFrontCoverDocument is defined as a Public Word.Document.
I successfully select a worksheet in Excel and paste it as a one-column table into the new document. I have a blank first cell and select that cell and try to insert a logo from a file:
The cell is selected correctly but the AddPicture command results in "Run-time error '438': Object doesn't support this property or method".
If I make the equivalent macro in the word document (with appropriate definitions) it works correctly, inserting the logo.
There are lots of Word commands that work, but many, such as the above, that do not.
I feel there must be something that I have missed but I do not know what.
Can anyone help?
I am in the process of using Excel VBA code to generate the word documents. I have the following References activated: Visual Basic For Applications; Microsoft Excel 14.0 Object Library; OLE Automation; Microsoft Office 14.0 Object Library; Microsoft Forms 2.0 Object Library; Microsoft Word 14.0 Object Library.
In quite a number of cases when I try to run Word VBA statements within the Excel VBA code I get error messages which seem to indicate I am not doing something needed. In addition to the above references, I select or generate a word application:
Code:
Set objWord = GetObject(class:="Word Application")
Err.Clear
If objWord Is Nothing Then Set objWord = CreateObject(class:="Word.Application")
If Err.Number = 429 Then
MsgBox "Microsoft Word could not be found, terminating."
End If
objWord.Visible = True ' make the MS Word visible
objWord.Activate
Code:
Set oFrontCoverDocument = objWord.Documents.Add
I successfully select a worksheet in Excel and paste it as a one-column table into the new document. I have a blank first cell and select that cell and try to insert a logo from a file:
Code:
oFrontCoverDocument.Tables(1).Cell(1, 1).Select
Selection.InlineShapes.AddPicture Filename:=sOutputFilePath & sColourFileName, LinkToFile:=False, SaveWithDocument:=True
If I make the equivalent macro in the word document (with appropriate definitions) it works correctly, inserting the logo.
There are lots of Word commands that work, but many, such as the above, that do not.
I feel there must be something that I have missed but I do not know what.
Can anyone help?