在Visual Basic

时间:2016-03-23 09:24:08

标签: vb.net visual-studio excel-interop

我正在尝试编辑Excel工作表以删除一些重复数据。我的宏在Excel中工作正常。

当我尝试在visual studio 2008中做同样的事情时,我面临一个问题:

代码为:

' Microsoft SQL Server Integration Services脚本任务 '使用Microsoft Visual Basic 2008编写脚本。 ' ScriptMain是脚本的入口点类。

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.Office.Interop

<System.AddIn.AddIn("ScriptMain", Version:="1.0", Publisher:="", Description:="")> _
<System.CLSCompliantAttribute(False)> Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum


    ' The execution engine calls this method when the task executes.
    ' To access the object model, use the Dts property. Connections, variables, events,
    ' and logging features are available as members of the Dts property as shown in the following examples.
    '
    ' To reference a variable, call Dts.Variables("MyCaseSensitiveVariableName").Value
    ' To post a log entry, call Dts.Log("This is my log text", 999, Nothing)
    ' To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, True)
    '
    ' To use the connections collection use something like the following:
    ' ConnectionManager cm = Dts.Connections.Add("OLEDB")
    ' cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"
    '
    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
    ' 
    ' To open Help, press F1.

    Public Sub Main()

        Dim oExcel As Excel.ApplicationClass
        Dim oBook As Excel.WorkbookClass
        Dim oBooks As Excel.Workbooks

        Dim i As Long
        Dim Col As Long
        Dim setErrorFilePath As String
        Dim setErrorFileName As String

        '  Dim xlTmp As Excel.Application
        'Set(xlTmp = New Excel.Application)
        '  xlTmp.Workbooks.Open("C:\Book1.xls")


        setErrorFilePath = Dts.Variables("ErrorFilePath").Value.ToString
        setErrorFileName = Dts.Variables("ErrorFileName").Value.ToString
        'Start Excel and open the workbook.
        oExcel = CreateObject("Excel.Application")
        oExcel.Visible = True
        oBooks = oExcel.Workbooks
        oBook = oBooks.Open(setErrorFilePath + setErrorFileName) ' Change your variable name here.

        'Macro to format Excel
        'Deleting the first four rows
        oExcel.Rows("1:6").EntireRow.Delete()

        'Unmerging The Cells
        oExcel.Cells.UnMerge()
        'Deleting Empy Columns after unmerging
        With oExcel.ActiveSheet.UsedRange
            Col = .Columns(.Columns.Count).Column
        End With
        For i = Col To 1 Step -1
            If oExcel.Application.CountA(oExcel.Columns(i)) = 0 Then
                oExcel.Columns(i).Delete()
            End If
        Next
        'Run the macros.

        'Clean-up: Close the workbook and quit Excel.
        oBook.Save()
        oExcel.Quit()
        Dts.TaskResult = ScriptResults.Success
    End Sub

End Class

我收到以下错误:

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Microsoft.Office.Interop.Excel.Range.set__Default(Object RowIndex, Object ColumnIndex, Object )
   at ST_7bdc1448ef3b4c8db7fb8d5574f78c18.vbproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

我搜索了其他地方,提到的解决方案是在c->windows->system32->config等创建一个文件夹。

但是我没有管理员权限,我无法获得。

我该如何解决这个问题?请帮忙

0 个答案:

没有答案