简体   繁体   中英

Vba-how to open csv file among several csv files and make this file active from the sharepoint site(.aspx not sharedrive) for next steps

I have three files one is a excel file enabled with macro where my macro is(1), the csv file to run the macro on(2). The new csv file that would be opened(3)

I am new to userform I created a web browser control and was able to initialize in the userform and added the code

  Private Sub UserForm_Initialize()
  Me.WebBrowser1.Navigate "http://sharepoint_site.aspx"   
  End Sub 

now when I click on the required csv file I get file download. There how do I just open the file and make this newly opened csv file as active? There are many csv files on the sharepoint site.The user selects a specific file and gets a file download box where it should jusst open that csv file. The reason for using userform as suggested by @David was to better control the newly opened CSV file and have the name of the file stored to perform the next steps of the code rather than file 2 where the macro is run to be the active workbook.Below code was my previous code which was part of a case statement.

Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
With IE
    .Visible = True
   .navigate ("https://site.aspx")
  MsgBox "Select the file and click open file"

Here obviously activated the file (2) where the macro ran but wanted to activate the newly opened file. Any help on this is greatly appreciated & thank you in advance.

Try something like this instead of the WebBrowser control, my apologies for pointing you in the wrong direction:

Sub foo()
Dim SummaryWB As Workbook
Dim vrtSelectedItem As Variant

With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "https:\\your_sharepoint\team\folder" & "\"
    .AllowMultiSelect = False
    .Show
    For Each vrtSelectedItem In .SelectedItems
        Set SummaryWB = Workbooks.Open(vrtSelectedItem)
    Next
End With

If SummaryWB Is Nothing Then Exit Sub

Call SomeOtherMacro(SummaryWB)

End Sub

So then you have some other macro that will process this workbook , you send the workbook to it like the above Call statement, and make sure that the other procedure accepts a Workbook :

Sub SomeOtherMacro(wb as Workbook)

' This macro will do something to the workbook
    wb.Worksheets(1).Select
    MsgBox wb.Name & " sheet 1 is now selected!"
End Sub

Modified from this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM