PDA

View Full Version : Help converting vba command to vb.net



Dim n As Rob
12-02-2009, 07:28 AM
Hey Guys,

I am writing a Word doc from VB.net, but I need to make the text box's in the word doc transparent. In the word macro recorder I get this:


Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Transparency = 0#


Any one know how this can be done in VB.net 2005 Visual Studio??

Thanks.

fumei
12-03-2009, 11:43 AM
Properly done (i.e. fully qualified) , this should be the same in VB.
ApplicationObject.Selection.ShapeRange.Fill.Transparency = 0#

With ApplicationObject being your actual named instance of the application.

Dim n As Rob
12-03-2009, 12:18 PM
Still no luck, I have somthing hosed up. Here is the Sub & declarations.



Option Explicit On
Option Compare Binary
Imports System.Math
Imports System.IO
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms
Imports msoffice = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
Imports Excel = Microsoft.Office.Interop.Excel



Public Sub ftext()
'left,top
'wordapp.ShapeRange.Fill.Transparency = 0.0#
' wordapp.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
'wordapp.ActiveDocument.Background.Fill.Transparency = 0.0#
'wordapp.Selection.ShapeRange.Fill.Visible = False
'wordapp.Selection.ShapeRange.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
'wordapp.Selection.ShapeRange.Fill.Solid()
'wordapp.Selection.ShapeRange.Fill.Transparency = 0.0#
'wordapp.Selection.ShapeRange.Line.Transparency = 0.0#
'wordapp.ActiveDocument.Selection.ShapeRange.Fill.Transparency = 0.0#
wordapp.ActiveDocument.Shapes.AddTextbox(msoffice.MsoTextOrientation.msoTex tOrientationHorizontal, a, b, c, d).Select()
wordapp.Selection.ShapeRange.TextFrame.TextRange.Select()
wordapp.Selection.Collapse()
wordapp.Selection.Font.Name = sfont
wordapp.Selection.Font.Size = nsize
wordapp.Selection.Font.Bold = sbold
wordapp.Selection.Font.Color = Word.WdColorIndex.wdBlack
wordapp.Selection.TypeText(Text:=stext)
wordapp.Selection.EndKey(Unit:=Word.WdUnits.wdLine) 'for barcode
wordapp.Selection.Font.Name = "Courier New" 'for barcode
wordapp.Selection.ShapeRange.Line.Visible = 0 'msoffice.msofalse
End Sub

fumei
12-03-2009, 02:48 PM
1. Please use the underscore character ( _ ) to break up your code lines. Otherwise it makes it very very difficult for me to read your code.

2.. I suggest you use With statements:
With wordapp
.ActiveDocument.Shapes.AddTextbox & _
(msoffice.MsoTextOrientation.msoTextOrientationHorizontal, a, b, c, d) & _
.Select()
With .Selection
.ShapeRange.TextFrame.TextRange.Select()
.Collapse()
.Font.Name = sfont
.Font.Size = nsize
.Font.Bold = sbold
.Font.Color = Word.WdColorIndex.wdBlack
.TypeText(Text:=stext)
.EndKey(Unit:=Word.WdUnits.wdLine) 'for barcode
.Font.Name = "Courier New" 'for barcode
.ShapeRange.Line.Visible = 0 'msoffice.msofalse
End With
End With


3. I noticwe you use a explicit numeric (0) instead of the named constant here:
.ShapeRange.Line.Visible = 0 'msoffice.msofalse

but not here:
Word.WdColorIndex.wdBlack
This could be an issue. If
Imports Word = Microsoft.Office.Interop.Word
essentially acts an an early-binding process, then you do not need numeric vales and can use Word named constant. If it does not, then you DO need to use numeric values.

4. You have:
wordapp.Selection.Font.Name = sfont
wordapp.Selection.Font.Size = nsize
but these (sfont, nsize) do not appear to be declared. Are they declared elsewhere as Public variables?

What - exactly -is wrong? Do you get error messages? if so, what are they?

Dim n As Rob
12-04-2009, 08:43 AM
2.. I suggest you use With statements:
With wordapp
.ActiveDocument.Shapes.AddTextbox & _
(msoffice.MsoTextOrientation.msoTextOrientationHorizontal, a, b, c, d) & _
.Select()
With .Selection
.ShapeRange.TextFrame.TextRange.Select()
.Collapse()
.Font.Name = sfont
.Font.Size = nsize
.Font.Bold = sbold
.Font.Color = Word.WdColorIndex.wdBlack
.TypeText(Text:=stext)
.EndKey(Unit:=Word.WdUnits.wdLine) 'for barcode
.Font.Name = "Courier New" 'for barcode
.ShapeRange.Line.Visible = 0 'msoffice.msofalse
End With
End With




Oh yes I like the way you did that! I was able to do it by adding this:


.ShapeRange.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse



Thanks,
Rob