Word 2000 macro -> VB.Net conversion question

M

Mason

I'm having some problems converting VBA for Word 2000 to code that
VB.Net understands. I recorded a macro in Word to add numbering (a.
b. c.) to my paragraphs. I managed to translate quite a bit of it,
but I'm having trouble with two pieces:

ListGalleries - I can't find any way to make VB.Net understand this.
When I do a mouseover, the error message it gives is, "Interface
'Word.ListGalleries' cannot be indexed because it has no default
property."

PointToInches - Not sure VB.Net even has a way to translate it.

Here is the Macro I recorded, followed by my transalted VB.Net code. I
removed some of the code I thought would be unnecessary to the
translation, but I could be wrong.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 9/14/2004 by H2O
'
With ListGalleries(wdNumberGallery).ListTemplates(6).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(0.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.5)
.TabPosition = InchesToPoints(0.5)
.ResetOnHigher = 0
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.StrikeThrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
ListGalleries(wdNumberGallery).ListTemplates(6).Name = ""
Selection.Range.ListFormat.ApplyListTemplate
ListTemplate:=ListGalleries( _
wdNumberGallery).ListTemplates(6),
ContinuePreviousList:=False, ApplyTo:= _
wdListApplyToWholeList,
DefaultListBehavior:=wdWord9ListBehavior
Selection.TypeText Text:="Blah blah blah"
Selection.TypeParagraph
Selection.TypeText Text:="Gah gah gah"
Selection.TypeParagraph
Selection.TypeText Text:="Wee wee wee"
End Sub

------- Begin VB.Net Code -------

Private Sub testing(ByVal owordnew As Word.Application)

With owordnew
With .ListGalleries(Word.WdListGalleryType.wdNumberGallery).ListTemplates(6).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter =
Word.WdTrailingCharacter.wdTrailingTab
.NumberStyle =
Word.WdListNumberStyle.wdListNumberStyleLowercaseLetter
.NumberPosition = InchesToPoints(0.25)
.Alignment =
Word.WdListLevelAlignment.wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.5)
.TabPosition = InchesToPoints(0.5)
.ResetOnHigher = 0
.StartAt = 1
.LinkedStyle = ""
End With
.ListGalleries(Word.WdListGalleryType.wdNumberGallery).ListTemplates(6).Name
= ""
.Selection.Range.ListFormat.ApplyListTemplate(ListTemplate:=ListGalleries(
_
Word.WdListGalleryType.wdNumberGallery).ListTemplates(6),
ContinuePreviousList:=False, ApplyTo:= _
Word.WdListApplyTo.wdListApplyToWholeList,
DefaultListBehavior:=Word.WdDefaultListBehavior.wdWord9ListBehavior)
.Selection.TypeText(Text:="Blah blah blah")
.Selection.TypeParagraph()
.Selection.TypeText(Text:="Gah gah gah")
.Selection.TypeParagraph()
.Selection.TypeText(Text:="Wee wee wee")
End With
End Sub


Thanks for any assistance,
MW
 
S

Siv

I notice that you pass the Word application object byval, I would have
thought by reference would expose more stuff?

Does the Word object have to be passed at all, could you create it in the
same function as it is used?

Siv
 
M

Mason Wood

Yes, you're right, I should use byref. I had forgotten I'd changed it
when I was testing out ways to get it to work. I've tried creating the
document objects in the procedure, tried passing them, nothing seems to
work. VB even autocompletes while I'm typing the ListGalleries line in,
it just comes up with that error when I'm done entering it.

Are there any good resources out there for MS Word to VB.Net coding? A
book, or website, or something? I've been crawing along with what
Microsoft has for online help, but it isn't really comprehensive enough
for me.

Thanks again,

MW
 
S

Siv

Mason,

I had a go at what you were trying to achieve as far as the renumbering bit is concerned. I have posted the application at http://www.sivill.com/Downloads/WordMacrosVBNet/ you can upload the various files and copy them to your machine and see if it works. What I have done is create a reference to the Word 10 object as that is what I have on my machine, you may need to change the reference on your machine to Office 9 to get it to work. I then create a variable that references the Word.application:

Inherits System.Windows.Forms.Form

Private Wd As Word.Application 'Reference the instance of Word which is added as a Project Reference.

Public Const wdParagraph As Word.WdUnits = 4 'Constant used by the Wd.Selection.MoveUp method - get by pressing F2 to get at the Word Objects and constants.

Public Const wdExtend As Word.WdMovementType = 1 'As above

The main routine that is called by pressing the button is as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim MyNewDoc As Word.Document, n As Integer

Wd = New Word.Application

Wd.Visible = True

MyNewDoc = Wd.Documents.Add("Normal.dot", , , True)

For n = 1 To 5

Wd.Selection.TypeText("This is paragraph " & n & "." & vbCrLf)

Next n

Wd.Selection.MoveUp(Unit:=wdParagraph, Count:=5, Extend:=wdExtend)

Wd.Run("Temp") 'Runs a Macro called Temp in Normal.dot

If MessageBox.Show("OK All Done - Yes to exit and close Word Window, No to leave Word open!", "Word Macro Test App By Siv", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then

MyNewDoc.Close(False) 'Don't save document (you probably would want to in your code).

Wd.Quit() 'Shutdown word

Wd = Nothing 'free up memory.

End If

End



'The following is what should be in your "Temp" macro this was in my Normal.dot

'With ListGalleries(wdNumberGallery).ListTemplates(5).ListLevels(1)

' .NumberFormat = "%1)"

' .TrailingCharacter = wdTrailingTab

' .NumberStyle = wdListNumberStyleLowercaseLetter

' .NumberPosition = CentimetersToPoints(0.63)

' .Alignment = wdListLevelAlignLeft

' .TextPosition = CentimetersToPoints(1.27)

' .TabPosition = CentimetersToPoints(1.27)

' .ResetOnHigher = 0

' .StartAt = 1

' With .Font

' .Bold = wdUndefined

' .Italic = wdUndefined

' .StrikeThrough = wdUndefined

' .Subscript = wdUndefined

' .Superscript = wdUndefined

' .Shadow = wdUndefined

' .Outline = wdUndefined

' .Emboss = wdUndefined

' .Engrave = wdUndefined

' .AllCaps = wdUndefined

' .Hidden = wdUndefined

' .Underline = wdUndefined

' .Color = wdUndefined

' .Size = wdUndefined

' .Animation = wdUndefined

' .DoubleStrikeThrough = wdUndefined

' .Name = ""

' End With

' .LinkedStyle = ""

'End With

'ListGalleries(wdNumberGallery).ListTemplates(5).Name = ""

'Selection.Range.ListFormat.ApplyListTemplate(ListTemplate:=ListGalleries( _

' wdNumberGallery).ListTemplates(5), ContinuePreviousList:=False, ApplyTo:= _

' wdListApplyToWholeList, DefaultListBehavior:=wdWord10ListBehavior)

'Selection.EndKey(Unit:=wdStory)



End Sub

The rem at the end of the sub is what I used as my "Temp" macro in Word in Normal.dot. If you are using a different template than "Normal.dot" you need to change the:



"MyNewDoc = Wd.Documents.Add("Normal.dot", , , True)"



bit so that your version references "xxyy.dot" or wahetever your's is called.

Needless to sat this code has no error trapping or anything!!

I hope this helps,

Siv
 
M

Mason Wood

That worked beautifully, thanks a ton Siv. I didn't know how to make
VB.Net force Word to run a macro - knowing how is an enormous help.

I can't thank you enough,

MW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro Error 3
add new section in the report in VBA 1

Top