VBA - 将内置方法设置为变量 [英] VBA - Set inbuilt Method to a Variable

查看:100
本文介绍了VBA - 将内置方法设置为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 


嗨朋友,



我正在调查这个不寻常的任务。


 


我想知道我是否可以执行以下操作

 
Sub ParagraphMethodTest()


Dim myMethod As String


myMethod = InsertBefore
'myMethod = InsertAfter

Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs

oPara.myMethod"你好"


下一个
结束子


将内置方法设置为变量,以便稍后我可以更改它,当我需要一个不同的方法时,abit就像一个case语句


 


这可能吗?






干杯丹:)

解决方案

您可以将变量标注为特定方法(与自动完成一起使用),但最简单的方法是使用If Then(Else)语句:



子测试()

    ParagraphMethodTest" InsertBefore"

End Sub



Sub ParagraphMethodTest(myMethod As String)

   昏暗的oPara作为段落

    

    For Each OPara In ActiveDocument.Paragraphs

       如果myMethod =" InsertBefore"然后

            oPara.InsertBefore" Hello"

       否则

            oPara.InsertAfter"Hello"&b
       结束如果

   下一个

结束次级






 

Hi friends,

I am investigating this unusual task.

 

I wanted to know if i can do the below

 
   Sub ParagraphMethodTest()
 

    Dim myMethod As String
  
    
    myMethod = InsertBefore
   'myMethod = InsertAfter
    
    Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
 
    oPara.myMethod "Hello"
    
 
    Next
End Sub

Set a inbuilt method as a variable so i can later change it as and when I need a different method, abit like a case statement

 

Is this possible at all?



Cheers Dan :)

解决方案

You can dimension a variable as a specific method (for use with autocomplete), but probably the simplest is to just use If Then (Else) statements:

Sub Test()
    ParagraphMethodTest "InsertBefore"
End Sub

Sub ParagraphMethodTest(myMethod As String)
    Dim oPara As Paragraph
    
    For Each oPara In ActiveDocument.Paragraphs
        If myMethod = "InsertBefore" Then
            oPara.InsertBefore "Hello"
        Else
            oPara.InsertAfter "Hello"
        End If
    Next
End Sub



这篇关于VBA - 将内置方法设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆