PDA

View Full Version : Help needed for Passing Parms to a sub-routine or function


Tharre
03-17-2010, 02:19 PM
Hello all -

I have been converting a VBA Access program to vb.net and have run into a snag.

I don't like putting a header in each function so I am calling the function from another function. I'm trying to pass the appWord to the sub-routine that handles the Headers and Footers but the sub-routine does not recognize the appWord at all. I've tried ByVal, ByRef as String and as Object. I know it's got to be something silly as this worked in VBA before. Here's what I've got so far. Seems like everything works without an error until I try to use the builtin document property "TITLE " this is where I get the error msg 'Range' is not a by reference property.

Dim appWord As Word.Application
Dim docWord As Word.Document
Dim rangeheader As Word.Range
Dim rangeMerge As Word.Range
Dim rangeWord As Word.Range
Dim rangeBreak As Word.Range
Dim tblWord As Word.Table
Dim tblHeader As Word.Table

'Set WORD variables
appWord = New Word.Application
docWord = appWord.Documents.Add

'
'Do headers and Footers '
sDocName = docWord.Name()
If sUserApplet <> "" Then
sReportTitle = sUserApplet
Else
sReportTitle = sUserProject
End If
Call Title(docWord, sReportTitle, "Control/List Column Layout")
Call Header(appWord)
Call Footer(appWord)

Public Function Header(ByVal appWord As Object)
'
' Header Macro
'

On Error GoTo Err_Header
appWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader
'Set the tabs for the header
appWord.Selection.ParagraphFormat.TabStops.ClearAll()
appWord.Selection.ParagraphFormat.TabStops.Add(Position:=appWord.InchesToPoints(5), _
Alignment:=Word.WdAlignmentTabAlignment.wdCenter, Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
appWord.Selection.ParagraphFormat.TabStops.Add(Position:=appWord.InchesToPoints(10), _
Alignment:=Word.WdAlignmentTabAlignment.wdRight, Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
With appWord.Selection.Font
.Name = "Bookman Old Style"
.Size = 16
.Italic = True
.SmallCaps = True
End With
'add Company Logo
If Form1.gsCompanyLogo <> "" Then
appWord.Selection.InlineShapes.AddPicture(FileName:= _
Form1.gsCompanyLogo, _
LinkToFile:=False, SaveWithDocument:=True)
End If
appWord.Selection.TypeText(Text:=vbTab)
' the title stuff goes here
appWord.Selection.Fields.Add(Range:=appWord.Selection.Range, Type:=Word.WdFieldType.wdFieldEmpty, Text:= _
"TITLE ", PreserveFormatting:=True)
'Set the Client Logo
appWord.Selection.TypeText(Text:=vbTab)
If Form1.gsClientLogo <> "" Then
appWord.Selection.InlineShapes.AddPicture(FileName:= _
Form1.gsClientLogo, _
LinkToFile:=False, SaveWithDocument:=True)
End If
appWord.Selection.TypeParagraph()
appWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
'Set the tabs for the header
appWord.Selection.ParagraphFormat.TabStops.ClearAll()
appWord.Selection.ParagraphFormat.TabStops.Add(Position:=appWord.InchesToPoints(5), _
Alignment:=Word.WdAlignmentTabAlignment.wdRight, Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
appWord.Selection.ParagraphFormat.TabStops.Add(Position:=appWord.InchesToPoints(10), _
Alignment:=Word.WdAlignmentTabAlignment.wdRight, Leader:=Word.WdTabLeader.wdTabLeaderSpaces)
appWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
With appWord.Selection.ParagraphFormat
With .Borders(Word.WdBorderType.wdBorderBottom)
.LineStyle = Word.WdLineStyle.wdLineStyleThinThickSmallGap
.LineWidth = Word.WdLineWidth.wdLineWidth300pt
.Color = Word.WdColor.wdColorAutomatic
End With
End With

With appWord.Selection.Font
.Name = "Bookman Old Style"
.Size = 12
.Italic = True
.SmallCaps = True
End With
appWord.Selection.Fields.Add(Range:=appWord.Selection.Range, Type:=Word.WdFieldType.wdFieldEmpty, Text:= _
appWord.wdbuiltinProperty.wdPropertySubject.value.ToString())
'"SUBJECT ", PreserveFormatting:=True)
appWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument

Exit_Header:

Exit Function

Err_Header:
'DoCmd.Hourglass(False)
MsgBox(Err.Description)
Resume Exit_Header
End Function

Tharre
03-18-2010, 02:34 PM
For those that need to know how to do this it was something that didn't make sense at the time but now it does. Here's the sub parameter line of code to make it work.

Public Sub CreateHeader(ByVal appWord As Microsoft.Office.Interop.Word.Application)