Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert vb to vba 3

Status
Not open for further replies.

Vbanewbie101

Instructor
Sep 5, 2007
2
US
The following code works in VB but not in VBA. A word document is opened the code looks for a string of text. If it finds it, it checks to see if the style is Heading 2. VBA gives me an overload error. Thanks

Dim mheading2
mheading2 = 0
'
Set myRange = ActiveDocument.Content
'
With Myrange.find
.clearformatting
.text="Computer Literacy requirement"
.Forward=True
.Execute
End With
'
If myrange.Find.Found Then
If myrange.Style = "Heading 2" Then
mHeading2 = 3
Else
mHeading2 = 0
End If
Else
mheading2=0
End If
 




Hi,

Error on which statment?

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 




I'm guessing it on this statement...
Code:
        If myrange.Style = "Heading 2" Then
since the Style property is an integer value, not TEXT.

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
If myrange.Style = "Heading 2" Then is where the error occurrs. The error message reads

"Overload resolution failed because no Public '=' can be called with these arguments:
'Public Shared Operator =(a As String, b As String) As Boolean':
Argument matching parameter 'a' cannot convert from '__ComObject' to 'String'."
 



Code:
If myrange.Style = ThisDocument.Styles("Heading 1") Then

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Code:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
   .Text = "Computer Literacy requirement"
   Do While .Execute(Forward:=True) = True
      If r.Style = "Heading 2" Then
         ' do whatever if style = "Heading 2"
      End If
   Loop
End With
Set r = Nothing

Sorry Skip, but, "since the Style property is an integer value, not TEXT. " is not quite correct.

ActiveDocument.Paragraphs(1).Style = wdStyleNormal
ActiveDocument.Paragraphs(1).Style = "Normal"
ActiveDocument.Paragraphs(1).Style = -1

are all equivalent. Using a text (string) is valid. I ran the code using myrange.Style = "Heading 2" and it runs fine.

From Help:
the local name of the style, an integer, a WdBuiltinStyle constant, or an object that represents the style.
So you could also use:
Code:
Dim myYaddaYadda As Style
Set myYaddaYadda = ThisDocument.Styles("Whatever")

If myrange.Style = myYaddaYadda
Vbanewbie101, I am not sure why you are getting that error. I have never seen it. As stated, adjusting for the variable myrange not being declared, the code runs with no error for me.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top