I have a word document with two embedded Excel files (insert -> object -> create from file) which I want to modify the word VBA to modify me at the point where I am able to open the embedded files for editing (see the code below), but I am unable to get a handle on the Excel workbook Which I am using I can do it and save the embedded file. Does anyone have a solution for this? thank you in advanced.
Sub TestMacro () Dim LNumShapes Long Dim LShapeCnt as long as dim xlApp object set wrdActDoc document set for wrdActDoc = lShapeCnt = 1 for ActiveDocument as 1 ' As wrdActDoc.InlineShapes.Count if wrdActDoc.InlineShapes (lShapeCnt). Type = wdIntecShapeEmbeddedOLEObject then if wrdActDoc.InlineShapes (lShapeCnt) OLEFormat.ProgID = "Excel.Sheet.8" then opens the embedded Excel workbook using this Excel wrdActDoc.InlineShapes (lShapeCnt). OLEFormat. If the end ends, then the next lShapeCnt termination sub
OK, what do you do Not suggesting in your comment, you may end up with several instances of Excel (check the Task Manager and see how many after executing your code).
First of all, select the Excel Object Library (Project-> Reference and Microsoft Excel Object Library) Now you can declare your object as an unobtrusive Excel type and in the form of "Object" Instead of declaring and using late binding, you can use the initial binding. It is not strictly necessary, but except for anything else, it means that when you edit your code, you get intensification.
You are doing the right thing. (I personally would use .OllFormat.Activate but since I have never tried to use .Edit I can not say that it does not make a difference). After doing
.Activate (or, maybe, .edit), you can reach OleFormat.Object member. Since the embedded object is an Excel chart, the "object", the Excel worksheet, you can do this:
Dim oOleFormat OleFormat set as Excel oOleFormat = ... oOllFormat.Activate slow oWorkbook In form of. Workbook set oWorkbook = oOleFormat.Object 'workbook oWorkbook.Charts (1) .ChartArea.Font.Bold = True
Note that you do not need to close Excel, and in fact you can not Make stuff together - the term "owner" used for the word an edit-in-place, and will decide when to close it is actually a problem, because there is no clear way to disable the embedded object. Not so, so to the above After executing the chart will remain open.
The way to turn off a hack-wi chart, though. If you add, ask the Word to activate it as something else, then it will activate it for the first time. Therefore, if you ask for it to be activated as a non-sensory, you will get the right result because it will disable it and then it will fail to reactivate it. Then, add the following line:
oOllFormat.ActivateAs "This.Class.Does.Not.Exist"
Note that this will raise an error so that you will need to temporarily Disable error by using error upon restart next. For this reason, I'm usually making an inactive method to avoid obstructing my handling of error in my main method. In the form of:
Private Sub DeactivateOleObject (byRef oOleFormat as OleFormat) Error on the next oOleFormat.ActivateAs "This.Class.Does.Not.Exist" start Sub again
Hope this helps. Gary
Comments
Post a Comment