Question about VBA object model

Gus01

New Member
Joined
Jul 27, 2011
Messages
5
Tried to set a signature but this option has yet to be made available to me?? So have added one below :)


Okay this is my code...

Code:
Sub makeAmemo()
Dim myWordDoc As Word.Application
Set myWordDoc = New Word.Application
Dim myString As String
Dim SaveAsName As String
'
    SaveAsName = ThisWorkbook.Path & "\Donkey.doc"
    For i = 1 To WorksheetFunction.CountA(ThisWorkbook.Sheets("sheet1").Range("A:A"))
        myString = ThisWorkbook.Sheets("sheet1").Cells(i, 1)
        
[B]
'THIS DOES NOT WORK
'        myWordDoc.Documents.Add.Selection.Font.Size = 14
'        myWordDoc.Documents.Add.Selection.TypeText Text:=" I am Here "
'        myWordDoc.Documents.Add.Selection.TypeText Text:=Format(Time, "hh:nn:ss  dd mmm yyyy")
'        myWordDoc.ActiveDocument.SaveAs Filename:=SaveAsName

'BUT THIS DOES[/B]
        With myWordDoc
            .Documents.Add
            With .Selection
                .Font.Size = 14
                .TypeText Text:=" I am Here Also "
                .TypeText Text:=Format(Time, "hh:nn:ss  dd mmm yyyy")
            End With
            .ActiveDocument.SaveAs Filename:=SaveAsName
        End With
    Next i
End Sub


My question is what is the difference as syntactically they seem to be equivalent functionally!! I have obviously got something wrong in my understanding of the model. Please help :confused:

Gus





@@@ BEWARE Newbie to Excel VBA @@@
Excel VBA 2003
Brace yourselves for lots of stupid questions !!!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
In the first instance you are tring to refer to the selection in a document, in the second you are referring to the selection in the application.
 
Upvote 0
Hello Gus,

".Documents.Add" is a property/function of "myWordDoc" (which is equivalent to using Word.Application in this case).

".Selection" is another property of "myWordDoc", NOT a property of ".Documents" or ".Documents.Add".
 
Upvote 0
Hello Gus,

".Documents.Add" is a property/function of "myWordDoc" (which is equivalent to using Word.Application in this case).

".Selection" is another property of "myWordDoc", NOT a property of ".Documents" or ".Documents.Add".



Okay I think the penny has dropped finally, thank you so much!

Code:
        With myWordDoc
            .Documents.Add
            With .Selection
                .Font.Size = 14
                .TypeText Text:=" I am Here Also "
                .TypeText Text:=Format(Time, "hh:nn:ss  dd mmm yyyy")
            End With
            .ActiveDocument.SaveAs Filename:=SaveAsName
        End With

I was loosely imitating code from an Excel book which unfortunately made a habit of leaving some fundamental explanations amiss. I was assuming that the second with-end-with was taking off from where the first left off and so I was ending up with code like this...

Code:
myWordDoc.Documents.Add.Selection.Font.Size = 14

So the With .Selection is actually referring to myWordDoc. You can see where one could easily go wrong as the these with-end-with statements seem nested.

Thanks
Gus
 
Upvote 0
Your variable naming is misleading; you might change myWordDoc to appWord or similar; it's referring to the application, not a document.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top