2015-05-05 44 views
-3

我正在使用此VBA代码将covnvert文本框文本转换为常规文本。但它通过shp.TypesString = Left(shp.TextFrame.TextRange.Text, _ shp.TextFrame.TextRange.Characters.Count - 1)错误,而我在VB编译。如何修改VB的VBA代码

我应该在VB的代码中改变什么?

这是VBA代码:

Sub ConvertTextBoxToText() 
    Dim shp As Shape 
    Dim oRngAnchor As Range 
    Dim sString As String 

    For Each shp In ActiveDocument.Shapes 

     If shp.Type = msoTextBox Then 
      ' copy text to string, without last paragraph mark 
      sString = Left(shp.TextFrame.TextRange.Text, _ 
        shp.TextFrame.TextRange.Characters.Count - 1) 

      If Len(sString) > 0 Then 
       ' set the range to insert the text 
       Set oRngAnchor = shp.Anchor.Paragraphs(1).Range 
       ' insert the textbox text before the range object 
       oRngAnchor.InsertBefore sString 
      End If 

      shp.Delete 

     End If 

    Next shp 

    'Strip out beginning and ending textbox markers 
    Selection.HomeKey unit:=wdStory 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "Textbox start << " 
     .Replacement.Text = "" 
     .Forward = True 
     ' .Wrap = wdFindContinue 
     .Format = False 
     .MatchCase = True 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 

    Selection.Find.Execute Replace:=wdReplaceAll 

    With Selection.Find 
     .Text = ">> Textbox end" 
     .Replacement.Text = "" 
     .Forward = True 
     .wrap = wdFindContinue 
     .Format = False 
     .MatchCase = True 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
End Sub 

VB代码:?

可以吗?

+0

什么是错误在哪里? – Gareth

+0

'shp​​.Type'和'sString = Left(shp.TextFrame.TextRange.Text,_ shp.TextFrame.TextRange.Characters.Count - 1)' – Pandu

+0

什么是错误 – Gareth

回答

1

VB使用VBA作为它的语言。所以不需要转换。 VB是托管VBA的应用程序对象和表单包,如Word是承载VBA的字处理器。

在您的代码中,您不能连接到Word。在Word中,一些对象会自动提供。在Word之外,你必须连接到它们。

Set xlBook = GetObject("C:\Users\User\Documents\Super.xls") 
For each wsheet in xlbook.worksheets 
    msgbox wsheet.name 
    wsheet.printOut 
next 

set xlapp = createobject("Excel.Application") 
xlapp.Workbooks.Open "C:\Users\User\Documents\Super.xls" 
'43 is 95/97 look up xlExcel9795 in object browser to see other options 
xlapp.ActiveWorkbook.SaveAs "C:\Users\User\Documents\Super.xls", 43 

Set GetExcelApp = GetObject("", "Excel.Application") 
Msgbox GetExcelApp