巧妙使用word vba 转换数据给vb
vb经常没有这样那样的常量定义,非得参看c或者别的定义。研究powerpoint的时候,经常发现powerpoint的vba文档并没有列出常量的详细值,常常需要自己动手到网上找,而找到的大多是c或者别的语言的。于是就有了下面这步:
找到的定义经常类似这样的:
msoBlackWhiteAutomatic = 1,
我要获得的是:
function functioname(selectname as long) as string
select case selectname
case 1:functioname="msoBlackWhiteAutomatic"
end select
end function
有时候,常量实在太多了,手工转的,那个叫累呀。于是有了下面的vba:
Sub MakeSelect()
Dim Selectname As String
Dim Functionname As String
Functionname = InputBox("输入函数名称", "函数名称", "")
Selectname = InputBox("输入类型名称", "类型名称", "")
Dim Replacestr
Replacestr = Array(",", "?", ";")
Dim i As Long, j As Long
Dim substr As String
Dim dpoint As Integer
Dim substr1 As String, substr2 As String
Open "d:/select.txt" For Output As #1
Print #1, "function " & Functionname & "(" & Selectname & " as long) as string"
Print #1, "select case " & Selectname
For i = 1 To ThisDocument.Paragraphs.Count
substr = Trim(ThisDocument.Paragraphs(i).Range.Text)
For j = 0 To UBound(Replacestr)
substr = Replace(substr, Replacestr(j), "")
Next
substr = Replace(substr, Chr(13), "")
If substr <> "" Then
dpoint = InStr(substr, "=")
If dpoint <> 0 Then
substr1 = Trim(Left(substr, dpoint - 1))
substr2 = Trim(Right(substr, (Len(substr) - dpoint)))
Print #1, vbTab & "case " & substr2 & " : " & Functionname & "=" & Chr(34) & substr1 & Chr(34)
End If
End If
Next
Print #1,"end select"
Print #1, "end function"
Close #1
End Sub
将数据粘贴到word里面,然后就可以使用以上的vba了。
不错吧? 挺简单的,挺方便的,挺实用的,也许你还有更好的改进。
尝试一下吧。
更多推荐


所有评论(0)