首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求大神将WORD的vba代码通译为C

2013-11-08 
求大神将WORD的vba代码翻译为C#Sub 宏1() 宏1 宏Selection.Find.ClearFormattingSelection.Find.Repla

求大神将WORD的vba代码翻译为C#


Sub 宏1()
'
' 宏1 宏
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "aa"
        .Replacement.Text = "^c"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

这个是vba代码


object FindText, Replace, ReplaceWith;
             object MissingValue = Type.Missing; 
            wordDoc.Content.Find.ClearFormatting();
            wordDoc.Content.Find.Replacement.ClearFormatting();
            FindText = "aa";
            ReplaceWith = "^c";
            wordDoc.Content.Find.Forward = true;
            wordDoc.Content.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
            wordDoc.Content.Find.Format = false;
            wordDoc.Content.Find.MatchCase = false;
            wordDoc.Content.Find.MatchWholeWord = false;
            wordDoc.Content.Find.MatchByte = false;
            wordDoc.Content.Find.MatchWildcards = false;
            wordDoc.Content.Find.MatchSoundsLike = false;
            wordDoc.Content.Find.MatchAllWordForms = false;
            Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
            wordDoc.Content.Find.Execute(ref FindText, ref MissingValue,
                 ref MissingValue, ref MissingValue,
                 ref MissingValue, ref MissingValue,
                 ref MissingValue, ref MissingValue, ref MissingValue,
                 ref ReplaceWith, ref Replace,
                 ref MissingValue, ref MissingValue,
                 ref MissingValue, ref MissingValue);

这个是我试着翻译的C#代码,vba代码执行的是将WORD的一段字符串替换成了图片,前提操作是把图片ctrl+c复制到剪切板,我翻译的没有实现,求大神帮帮忙
[解决办法]

            object FindText, Replace, ReplaceWith;
            object MissingValue = Type.Missing;
            Clipboard.Clear();
            Clipboard.SetDataObject(Image.FromFile(@"F:\img" + i.ToString() + ".jpg"), true);
            wordDoc.Content.Find.ClearFormatting();
            wordDoc.Content.Find.Replacement.ClearFormatting();
            FindText = strOldText;


            ReplaceWith = "^c";
            Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
            wordDoc.Content.Find.Execute(ref FindText, ref MissingValue,
                 ref MissingValue, ref MissingValue,
                 ref MissingValue, ref MissingValue,
                 ref MissingValue, ref MissingValue, ref MissingValue,
                 ref ReplaceWith, ref Replace,
                 ref MissingValue, ref MissingValue,
                 ref MissingValue, ref MissingValue);
            wordDoc.Content.ShapeRange.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTopBottom;

热点排行