My issue involves a word file which has a macro embedded from another company which we have to remove and then set a new data source path (xlsx spreadsheet). The macro must be removed because it already sets the data source which is not available anymore when we get the file. Now I'm looking for a possibility to change the data source (path) of said word file programmatically but can't find any that does the job only VB Code. We don't want to write another macro and inject it we only want to change the data source path before the user downloads the file. I'm free to use any library.
Can anyone please suggest me a way to change the data source of a word file programmatically?
Update:
I'm now working with the MS Office Interop Word Library and achieved what I was looking for (changing data source) but unfortunately the excel file is damaged afterwards and can't be opened anymore saying "invalid data format".
Code below:
Application app = new Application();
Document officeDoc = app.Documents.Open(toBeMerged);
var srcFile = @"C:\DataSource.xlsx";
FileInfo fileInfo = new FileInfo(srcFile)
{
IsReadOnly = false
};
officeDoc.MailMerge.CreateDataSource(srcFile);
officeDoc.Save();
officeDoc.Close();
app.Quit();
Update 2
I've set all the needed parameters.
officeDoc.MailMerge.CreateDataSource(missing, missing, missing, missing, missing, missing, missing, missing, srcFile);
The parameters are as following: CreateDataSource(Name, PasswordDocument, WritePasswordDocument, HeaderRecord, MSQuery, SQLStatement, SQLStatement1, Connection, LinkToSource)
This throws a System.Runtime.InteropServices.COMException 0x80020005 (DISP_E_TYPEMISMATCH) exception but the word gets successfully generated. When I put the srcFile variable at the begining where it should belong the excel file gets like above mentioned damaged despite giving all the optional parameters.
Thanks in advance helping me solving my last issue.
User contributions licensed under CC BY-SA 3.0